Search code examples
androidbluetoothbroadcastreceiver

Android Bluetooth Broadcast Receiver Issues with ACL_CONNECTED and AC_DISCONNECTED


I am writing an app that detects whether or not a bluetooth device is connected. After doing some research I found that the best way to do it was to use a broadcast receiver with some bluetooth related intent filters.

<receiver android:name=".BTReceiver" >
            <intent-filter>
            <action android:name="android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED" />
            <action android:name="android.bluetooth.BluetoothDevice.ACTION_ACL_DISCONNECTED" />
            <action android:name="android.bluetooth.BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED" />
            <action android:name="android.bluetooth.a2dp.profile.action.CONNECTION_STATE_CHANGED" />             
            </intent-filter>  
        </receiver>

And here is my bluetooth receiver class.

@Override
    public void onReceive(Context context, Intent intent) {     
        String action = intent.getAction();
        if(action.equals("android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED") || action.equals("android.bluetooth.a2dp.profile.action.CONNECTION_STATE_CHANGED") ){
            Log.d("Z","Received: Bluetooth Connected");
        }
        if(action.equals("android.bluetooth.BluetoothDevice.ACTION_ACL_DISCONNECTED") ||action.equals("android.bluetooth.BluetoothDevice.ACTION_ACL_DISCONNECTED_REQUESTED")){
            Log.d("Z","Received: Bluetooth Disconnected");
        }
        Log.d("Z",action);
}

When I turn on my bluetooth headset and it connects to my phone, I receive "android.bluetooth.a2dp.profile.action.CONNECTION_STATE_CHANGED" twice. When I disconnect the bluetooth headset, the broadcast receiver doesn't run or receive anything. So I never receive my Bluetooth Disconnected Log message. I am using the two bluetooth permissions that I thought were necessary to get this to work.

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

I could really use any information on what is wrong here. Thank you.


Solution

  • You got your action wrong.

    <receiver android:name=".BTReceiver" >
            <intent-filter>
            <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
            <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
            <action android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED" />           
            </intent-filter>  
        </receiver>
    

    Remove the

    || action.equals("android.bluetooth.a2dp.profile.action.CONNECTION_STATE_CHANGED")  
    

    since CONNECTION_STATE_CHANGED could be connected, disconnected etc.. so that your first if is always true. You do not need to register for android.bluetooth.a2dp.profile.action.CONNECTION_STATE_CHANGED unless you do anything specific with a2dp.

    Thus, it should be

    if(action.equals("android.bluetooth.device.action.ACL_CONNECTED") {
            Log.d("Z","Received: Bluetooth Connected");
        }
        if(action.equals("android.bluetooth.device.action.ACL_DISCONNECTED") ||action.equals("android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED")){
            Log.d("Z","Received: Bluetooth Disconnected");
        }