How to determine which Bluetooth device is calling ACTION_ACL_CONNECTED?

I want to listen to the connection / disconnection with some specific Bluetooth devices whose MAC addresses I know, but which are not necessarily paired (I do not want to contact the list of users of paired devices and vice versa). I am only interested in their presence, not in communicating with them.

This works very well with my code below! But my problem is that I can’t find out which particular device is connecting / disconnecting, only that this happens to one of them. How can I find out what action it refers to?

First, I create objects for my two specific physical bluetooth devices and add them to my intent filter:

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

Then I listen about things about them:

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

Now I want to know which one has been connected and / or disconnected, and I don’t see how I can do this.

Or 1) I directly use "BluetoothDevice". It responds to the broadcast in the order, but it does not tell me which of the two physical devices has an effect. Their way to find out? Bluetooth.getName () is not allowed because it is not a static class.

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

or 2) I listen to both actions for both devices.

  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 "); 

But then it registers that it connects to myPinkHeadset, even if it is myPvBluetoothDongle, which I activate physically. It always goes to the one that comes first from the if tests. He cares only about the action itself, and not about which object it refers to.

I saw that EXTRA_DEVICE "is used as an additional additional BluetoothDevice field every time this class is launched." But it returns only null:

 String extra = intent.getStringExtra(BluetoothDevice.EXTRA_DEVICE); 
+8
android bluetooth broadcastreceiver
source share
2 answers

In this case, the device is connected to:

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

As a beginner, I misunderstood this concept. EXTRA_DEVICE is a String, but it is just a tag for an object. Therefore, there is no need to register or listen to individual instances of BluetoothDevice. When an action is broadcast, the intent will indicate which physical device caused it. (Can I do +1 for this: -D)

+14
source share
 intentFilter.addAction(myPinkHeadset.ACTION_ACL_CONNECTED); intentFilter.addAction(myPcBluetoothDongle.ACTION_ACL_CONNECTED); 

and

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

- one and the same meaning. This is a static value. BluetoothDevice.ACTION_ACL_CONNECTED and 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; } } } 

};

Hope this helps you.

+1
source share

All Articles