A sticky broadcast intent for a bluetooth headset in android/or an alternative

2.2k views Asked by At

I am working on a app where I require to know if there is a bluetooth headset connected. I have created a broadcast receiver that receives the broadcast intents if we connect to the bluetooth headset while the app is on. on the other hand if a connect to the bluetooth device and then start the app I do not receive any broadcast for the bluetooth device connection. I would like to know if any bluetooth headset is connected on the app start. following is my broadcast receiver and manifest.

public class BluetoothDeviceConnectionReciver extends BroadcastReceiver {


    static OnBluetoothDeviceStateChangeLisener onBluetoothStateChangedListener;

    @Override
    public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    //int headsetAudioState = intent.getIntExtra("android.bluetooth.headset.extra.AUDIO_STATE", -2);

    //Toast.makeText(context, "New Test", Toast.LENGTH_SHORT).show();

    //BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    Log.i("headsetAudioState","in broadcast");


    if (BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
        int bluetoothHeadsetState = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE,
                BluetoothHeadset.STATE_DISCONNECTED);
        Log.i("headsetAudioState",bluetoothHeadsetState+" ");
        //Device found
        if (bluetoothHeadsetState == BluetoothHeadset.STATE_CONNECTED) {
            if (onBluetoothStateChangedListener != null)
                onBluetoothStateChangedListener.OnBluetoothHeadsetConnected();
            Log.i("headsetAudioState",bluetoothHeadsetState+" connected");
        }
        if(bluetoothHeadsetState == BluetoothHeadset.STATE_DISCONNECTED){
            if (onBluetoothStateChangedListener != null)
                onBluetoothStateChangedListener.OnBluetoothHeadsetDisconnected();
            Log.i("headsetAudioState",bluetoothHeadsetState+" disconnected");
        }
    }



    if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
        //Device is now connected
        if (onBluetoothStateChangedListener != null)
            onBluetoothStateChangedListener.OnBluetoothHeadsetConnected();
    } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
        //Done searching
    } else if (BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
        //Device is about to disconnect
    } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
        //Device has disconnected
        if (onBluetoothStateChangedListener != null)
            onBluetoothStateChangedListener.OnBluetoothHeadsetDisconnected();

    } 


    }
    public void setOnBluetoothStateChangeListener(OnBluetoothDeviceStateChangeLisener listener){
    onBluetoothStateChangedListener = listener;
    }

    public interface OnBluetoothDeviceStateChangeLisener{
    void OnBluetoothHeadsetConnected();
    void OnBluetoothHeadsetDisconnected();
    }
}

manifest.xml

   .....
   .....
   .....

   <receiver android:name=".BluetoothDeviceConnectionReciver">
        <intent-filter>
            <action android:name="android.bluetooth.headset.profile.action.CONNECTION_STATE_CHANGED"/>
            <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
            <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
        </intent-filter>

    </receiver>

   .....
   .....

I would like to know if there are any other intents that i should be listening to. Or please let me know what other options I have to achieve the intended goal.

P.S : the device connected should be a headset as I intend to use the mic of the bluetooth headset.

1

There are 1 answers

1
Avinash Joshi On BEST ANSWER

Well I digged a lot but wasn't able to find any broadcast that is sticky so that on the start of app I would get to know if the phone is connected to bluetooth headset as mentioned in the question.

The work arround that I implemented is to get the bluetooth adapter and check the connection status of the bluetooth profile as in the below code

    public boolean isBluetoothHeadsetConnected() {
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        boolean isConnected = mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()
                && mBluetoothAdapter.getProfileConnectionState(BluetoothHeadset.HEADSET) == BluetoothHeadset.STATE_CONNECTED;
        Log.i("bluetoothConnected",isConnected+"");
        return  isConnected;
    }

I call this code in onCreate() of the SplashScreenActivity of the app and set the corresponding flag. The changes in the bluetooth state after app start are then taken care by the Broadcast listeners that i have implemented as in the question. This code served my purpose very well.