Search code examples
androidbluetoothbroadcastreceiverandroid-broadcastreceiver

Issue handling lost Bluetooth connections using BroadcastReceiver when connected to two devices


I have the following code to handle lost Bluetooth connections.

public class BluetoothReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();


        if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
            //call method to cancel connection thread
        }
    }
}

However, I have another app that uses Bluetooth connected to a different device running in the background. If I lose connection to that device, I also lose the connection to the device within this app.

I was wondering, is there any way to prevent this?


Solution

  • As described here you are able to get the device instance from the intent with intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) If the device is NOT the one you are interested in, just DO NOT call:

      //call method to cancel connection thread