2010-05-30 78 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