2012-04-04 45 views

回答

4

我不知道有什麼辦法讓當前連接的設備的清單,但你可以聽使用ACL_CONNECTED意圖的新連接: http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#ACTION_ACL_CONNECTED

這INTE nt在連接所在的遠程設備中包含一個額外的域。

在Android上,所有藍牙連接都是ACL連接,因此註冊此意向會爲您提供所有新連接。

所以,你的接收器將是這個樣子:

public class ReceiverBlue extends BroadcastReceiver { 
    public final static String CTAG = "ReceiverBlue"; 
    public Set<BluetoothDevice> connectedDevices = new HashSet<BluetoothDevice>(); 

    public void onReceive(Context ctx, Intent intent) { 

    final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 

    if (BluetoothDevice.ACTION_ACL_CONNECTED.equalsIgnoreCase(action)) { 
     Log.v(CTAG, "We are now connected to " + device.getName()); 
     if (!connectedDevices.contains(device)) 
     connectedDevices.add(device); 
    } 

    if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equalsIgnoreCase(action)) { 
     Log.v(CTAG, "We have just disconnected from " + device.getName()); 
     connectedDevices.remove(device); 
    } 
    } 
} 
+0

我試過這個。每當我連接設備它顯示「ACTION_ACL_CONNECTED」..但它立即顯示「ACTION_ACL_DISCONNECTED」..我做錯了什麼? – 2013-08-26 11:12:18

+0

這很奇怪。這聽起來像連接只是短暫的,然後丟失。我不知道你爲什麼這樣做。 – Tom 2013-08-26 16:28:29

+0

'在Android上,所有藍牙連接都是ACL連接'你確定嗎?有一個鏈接呢?! – Soheil 2013-09-25 18:18:28

0

我認爲getBondedDevices()將幫助你:)

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
// If there are paired devices 
if (pairedDevices.size() > 0) { 
// Loop through paired devices 
for (BluetoothDevice device : pairedDevices) { 
    // Add the name and address to an array adapter to show in a ListView 
    mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 
} 
} 

謝謝:)

+8

getBondedDevices()將讓你配對的設備,沒有連接的。 – Tom 2012-04-04 23:49:06

+0

一個詭異的方法是嘗試連接到所有人,看他們是否連接,缺點是這是在主線程上完成的。 – jobbert 2016-12-16 08:42:04