2010-05-30 118 views
12

我在Android文档中找到如何打开蓝牙可发现模式上:禁用蓝牙发现模式在Android

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

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

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

任何帮助?

+0

解决了在此线程反射:https://stackoverflow.com/a/47452626/5239473 – 2017-12-14 19:57:53

回答

9

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

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

洛尔的hackish ,但它有效(仅限1个)。 谢谢;) – Venator85 2010-06-25 12:49:41

+0

同意,但它是我能找到的最好/唯一的解决方案。那么你的应用做什么? – 2010-06-25 17:41:18

+0

这只是一个简单的小工具来启用/禁用发现性,没有什么奇特的:) – Venator85 2010-06-26 17:43:38

1

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

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