framework提供了一种打开可见性的操作,就是通过向用户弹出一个提示框,来询问是否允许开启可见性。而且限制了最长时间为300秒,代码如下:
[java]view plaincopy
//启动修改蓝牙可见性的Intent
Intent intent =newIntent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
//设置蓝牙可见性的时间,方法本身规定最多可见300秒
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,300);
startActivity(intent);
但通过Android的自带的settings程序,我们可以直接开机蓝牙可见性。所以下载settings的源码,进行分析。找到了开启蓝牙可见性的代码,如下:
[java]view plaincopy
privatevoidsetEnabled(booleanenable) {
if(enable) {
inttimeout = getDiscoverableTimeout();
mLocalAdapter.setDiscoverableTimeout(timeout);
longendTimestamp = System.currentTimeMillis() + timeout * 1000L;
LocalBluetoothPreferences.persistDiscoverableEndTimestamp(mContext, endTimestamp);
mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, timeout);
updateCountdownSummary();
}else{
mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE);
}
}
这下就清楚了,是BluetoothAdapter 里面的setDiscoverableTimeout和setScanMode起到了关键性左右,再看BluetoothAdapter源码,发现这2个方法都被隐藏(hide)了。如何能访问到被隐藏的方法呢?自然是用强大的反射:
[java]view plaincopy
publicvoidsetDiscoverableTimeout(inttimeout) {
BluetoothAdapter adapter=BluetoothAdapter.getDefaultAdapter();
try{
Method setDiscoverableTimeout = BluetoothAdapter.class.getMethod("setDiscoverableTimeout",int.class);
setDiscoverableTimeout.setAccessible(true);
Method setScanMode =BluetoothAdapter.class.getMethod("setScanMode",int.class,int.class);
setScanMode.setAccessible(true);
setDiscoverableTimeout.invoke(adapter, timeout);
setScanMode.invoke(adapter, BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE,timeout);
}catch(Exception e) {
e.printStackTrace();
}
}
用这种方法开启的可见性,还有个附件的属性,timeout值并没有起到作用,可见性是一直保持的。可以通行下面类似的代码进行关闭:
[java]view plaincopy
publicvoidcloseDiscoverableTimeout() {
BluetoothAdapter adapter=BluetoothAdapter.getDefaultAdapter();
try{
Method setDiscoverableTimeout = BluetoothAdapter.class.getMethod("setDiscoverableTimeout",int.class);
setDiscoverableTimeout.setAccessible(true);
Method setScanMode =BluetoothAdapter.class.getMethod("setScanMode",int.class,int.class);
setScanMode.setAccessible(true);
setDiscoverableTimeout.invoke(adapter,1);
setScanMode.invoke(adapter, BluetoothAdapter.SCAN_MODE_CONNECTABLE,1);
}catch(Exception e) {
e.printStackTrace();
}
}
改变BluetoothAdapter.SCAN_MODE_CONNECTABLE是关键。
如果想实现超时后自动关闭可见性的效果,使用Handler
postDelayed(Runnable r, long delayMillis)
就可以轻松实现这个功能。