Home » Android » android – How identify *which* bluetooth device causes an ACTION_ACL_CONNECTED broadcast?

android – How identify *which* bluetooth device causes an ACTION_ACL_CONNECTED broadcast?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I want to listen for connection/disconnection with a number of specific bluetooth devices whose MAC addresses I know, but which are not necessarily paired (I don’t want to mess with the user’s list of paired devices and vice versa). I’m only interested in discovering their presence, not communicating with them.

This works very well with my code below! But my problem is that I cannot find out which specific device is connecting/disconnecting, only that it happens to someone of them. How can I find out which one the action concerns?

First I instantiate 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 for broadcasts about them:

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

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

Either 1) I use “BluetoothDevice” directly. It reacts to the broadcast alright, but it doesn’t tell me which of the two physical devices the action concerns. Is their a way to find out? Bluetooth.getName() is not allowed because it’s not a static class.

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

or 2) I listen for 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 logs that it connects with myPinkHeadset even if it is myPvBluetoothDongle I activate physically. It always goes for the one which comes first of the if tests. It cares only about the action itself, not about which object it concerns.

I saw that EXTRA_DEVICE is “Used as a Parcelable BluetoothDevice extra field in every intent broadcast by this class.” But it only returns null to me:

String extra = intent.getStringExtra(BluetoothDevice.EXTRA_DEVICE);
How to&Answers:

This gives the device connected to:

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

As a newbie, I misunderstood the parcelable concept. EXTRA_DEVICE is a String, but it’s just a tag for the object. So there’s no need to register or listen to individual instances of BluetoothDevice. When an action is broadcasted, the intent will tell which physical device caused it. (Can I +1 myself for this :-D)

Answer:

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

are the same value. It’s 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;
        }
    }
}

};

I hope this can help you