【问题标题】:How to make Bluetooth Undiscoverable?如何使蓝牙不可发现?
【发布时间】:2017-11-23 18:37:45
【问题描述】:
我已经学习了使蓝牙在 Android 中可被发现的代码,例如:
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);` =
那么,一旦我使蓝牙可被发现,我如何才能人为地使其无法被发现?
感谢大家的帮助!
【问题讨论】:
标签:
android
android-bluetooth
【解决方案1】:
我找到了一个方法,虽然我还不明白,但它在我的项目中有效!
public void closeDiscoverableTimeout() {
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();
}
}