如何识别*哪个蓝牙设备导致ACTION_ACL_CONNECTED广播?

问题描述:

我想听一些特定的蓝牙设备的连接/断开连接,这些设备的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); 

这使得连接到设备:

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

作为一个新手,我误解了parcelable概念。 EXTRA_DEVICE是一个字符串,但它只是对象的标签。所以不需要注册或收听BluetoothDevice的各个实例。当广播一个动作时,意图会告诉哪个物理设备导致了它。 (我可以自己+1本:-D)

+0

与parcelable做了同样的错误,字符串混淆 – NikkyD 2012-11-07 13:12:50

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; 
     } 
    } 
} 

};

我希望这可以帮助你