2012-02-27 520 views
8

我想聽一些特定的藍牙設備的連接/斷開連接,這些設備的MAC地址我知道,但不一定配對(我不想混淆用戶的配對設備列表,反之亦然)。我只想發現他們的存在,而不是與他們溝通。如何識別*哪個藍牙設備導致ACTION_ACL_CONNECTED廣播?

這對我的代碼很好用!但我的問題是,我無法確定哪個特定設備正在連接/斷開連接,只是發生在其中某個設備上。我怎樣才能找出行動關注的是哪一個?

首先,我實例化對象爲我的兩個特定的物理藍牙設備,並將其添加到我的意圖過濾器:

BluetoothDevice myPinkHeadset = mBluetoothAdapter.getRemoteDevice("18:17:0C:EB:9C:81"); 
    BluetoothDevice myPcBluetoothDongle = mBluetoothAdapter.getRemoteDevice("5A:7A:CC:4B:C5:08"); 

    IntentFilter intentFilter = new IntentFilter(); 
    intentFilter.addAction(myPinkHeadset.ACTION_ACL_CONNECTED); 
    intentFilter.addAction(myPinkHeadset.ACTION_ACL_DISCONNECTED); 
    intentFilter.addAction(myPcBluetoothDongle.ACTION_ACL_CONNECTED); 
    intentFilter.addAction(myPcBluetoothDongle.ACTION_ACL_DISCONNECTED); 

後來我聽的廣播對他們:

final BroadcastReceiver intentReceiver = new BroadcastReceiver() { 
     public void onReceive(Context context, Intent intent) { 
      String action = intent.getAction(); 

現在我想找到哪一個已經連接和/或斷開連接,我不明白我能做到這一點。

要麼1)我直接使用「BluetoothDevice」。它對廣播做出了反應,但它並不告訴我這兩個物理設備中的哪一個是關注的。他們有辦法找出答案嗎? Bluetooth.getName()不被允許,因爲它不是靜態類。

if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) { 
     } 

或2)我聽兩種設備的動作。

 if (myPinkHeadset .ACTION_ACL_CONNECTED.equals(action)) { 
      Log.v(TAG, "Connected to myPinkHeadset "); 
     } 
     else if (myPinkHeadset .ACTION_ACL_DISCONNECTED.equals(action)) { 
      Log.v(TAG, "Disconnected from myPinkHeadset "); 
     } 
     else if (myPcBluetoothDongle .ACTION_ACL_CONNECTED.equals(action)) { 
      Log.v(TAG, "Connected to myPcBluetoothDongle "); 
     } 
     else if (myPcBluetoothDongle .ACTION_ACL_DISCONNECTED.equals(action)) { 
      Log.v(TAG, "Disconnected from myPcBluetoothDongle "); 

但隨後它會記錄它與myPinkHeadset連接哪怕是myPvBluetoothDongle我激活身體。它總是適用於如果測試的第一個。它只關心行爲本身,而不關心它關心的對象。

我看到EXTRA_DEVICE被「用作這個類廣播的每一個意圖中的Parcelable BluetoothDevice額外字段」。但它只返回null對我說:

String extra = intent.getStringExtra(BluetoothDevice.EXTRA_DEVICE); 

回答

14

這使得連接到設備:

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

作爲一個新手,我誤解了parcelable概念。 EXTRA_DEVICE是一個字符串,但它只是對象的標籤。所以不需要註冊或收聽BluetoothDevice的各個實例。當廣播一個動作時,意圖會告訴哪個物理設備導致了它。 (我可以自己+1本:-D)

+0

與parcelable做了同樣的錯誤,字符串混淆 – NikkyD 2012-11-07 13:12:50

1
intentFilter.addAction(myPinkHeadset.ACTION_ACL_CONNECTED); 
intentFilter.addAction(myPcBluetoothDongle.ACTION_ACL_CONNECTED); 

intentFilter.addAction(myPinkHeadset.ACTION_ACL_DISCONNECTED); 
intentFilter.addAction(myPcBluetoothDongle.ACTION_ACL_DISCONNECTED); 

是相同的值。它是靜態值。 BluetoothDevice.ACTION_ACL_CONNECTED和BluetoothDeviceACTION_ACL_DISCONNECTED

private void register() { 
    context.registerReceiver(bluetoothBroadCast, new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED)); 
    context.registerReceiver(bluetoothBroadCast, new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED)); 
} 

private final BroadcastReceiver bluetoothBroadCast = new BroadcastReceiver() { 
@Override 
public void onReceive(Context context, Intent intent) { 
    String action = intent.getAction(); 
    switch (action) { 
     case BluetoothDevice.ACTION_ACL_CONNECTED: { 
      BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
      if(device.getAddress().equals(myPinkHeadset.getAddress)) { 
       //Do what you want 
      } 
      break; 
     } 

     case BluetoothDevice.ACTION_ACL_DISCONNECTED: { 
      BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
      break; 
     } 
    } 
} 

};

我希望這可以幫助你