在Android上禁用蓝牙可发现模式

时间:2010-05-30 11:33:34

标签: android bluetooth

我在Android文档中发现了如何打开蓝牙可发现模式:

Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);

这将使设备可被发现300秒(documentation)。

我的问题是:如何在此超时发生之前关闭可发现性?我想在“设置|无线和网络|蓝牙设置小程序”中复制相应的设置,以便通过单击打开和关闭可发现性。

任何帮助?

3 个答案:

答案 0 :(得分:9)

只需发送持续时间为1的新可发现请求(或者0甚至可能有效):

Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 1);
startActivity(discoverableIntent);

答案 1 :(得分:1)

cancelDiscovery()不适用于此。此方法可用于停止扫描其他蓝牙设备的设备。与此不同的是,设备不可见。

答案 2 :(得分:0)

使用此方法时要小心,因为隐藏它可能很容易改变。

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();  
try {
    Method method = BluetoothAdapter.class.getMethod("setScanMode", int.class);
    method.invoke(bluetoothAdapter, BluetoothAdapter.SCAN_MODE_CONNECTABLE);
} catch (NoSuchMethodException | IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
    Log.e(TAG, "Failed to turn off bluetooth device discoverability.", e);
}

也适用于SCAN_MODE_NONESCAN_MODE_CONNECTABLE_DISCOVERABLE(使用默认持续时间)

Source