0

I need to detect when wired headset or a bluetooth one is plugged/connected so I create and register two BroadcastReceiver(s) like the following:

file ReceiverHeadsetWired.java

public class ReceiverHeadsetWired extends BroadcastReceiver {
    ActivityMain main;

    public ReceiverHeadsetWired(
        ActivityMain activityMain
    ){
        main = activityMain;
    }

    @Override
    public void onReceive(
        Context context,
        Intent intent
    ) {
        if (TextUtils.isEmpty(intent.getAction())) { return; }

        if (Objects.equals(intent.getAction(), "android.intent.action.HEADSET_PLUG")) {
            Log.d("[ReceiverHeadsetWired]", "onReceive()");
            
            ...
        }
    }
}

file ReceiverHeadsetBluetooth.java

public class ReceiverHeadsetBluetooth extends BroadcastReceiver {
    ActivityMain main;

    public ReceiverHeadsetBluetooth(
        ActivityMain activityMain
    ){
        main = activityMain;
    }

    @Override
    public void onReceive(
        Context context,
        Intent intent
    ) {
        if (TextUtils.isEmpty(intent.getAction())) { return; }

        if (Objects.equals(intent.getAction(), "android.bluetooth.headset.profile.action.CONNECTION_STATE_CHANGED")) {
            Log.d("[ReceiverHeadsetBluetooth]", "onReceive()");
            
            ...
        }
    }
} 

Both of them are dynamically registered in onCreate method of MainActivity's Fragment and unregisterd onDestroy.

@Override
public void onCreate(
    Bundle savedInstanceState
) {
    super.onCreate(savedInstanceState);
    
    ActivityMain main = (ActivityMain) getActivity();

    ...
    
    registerHeadsetWiredReceiver();

    registerHeadsetBluetoothReceiver();

    ...     

}


private void registerHeadsetWiredReceiver() {
    wiredHeadsetReceiver = new ReceiverHeadsetWired(main);
    IntentFilter hwFilter = new IntentFilter("android.intent.action.HEADSET_PLUG");
    main.registerReceiver(wiredHeadsetReceiver, hwFilter);
}

private void registerHeadsetBluetoothReceiver() {
    bluetoothHeadsetReceiver = new ReceiverHeadsetBluetooth(main);
    IntentFilter hbFilter = new IntentFilter("android.bluetooth.headset.profile.action.CONNECTION_STATE_CHANGED");
    main.registerReceiver(bluetoothHeadsetReceiver, hbFilter);
}

Now the point is that at launch only the onReceive of ReceiverHeadsetWired is called (Logcat shows the textline), but after app started both of them work as expected except a strange behaviour: the first time I connect a Bluetooth headset the related Log is written twice.

In other words when app is launched if a wired headset is plugged it will be detected but a connected bluetooth one won't.

Does anybody knows what's the problem?

Thanks in advance

r08y
  • 125
  • 1
  • 13
  • 1
    [Detect if bluetooth headset connected](https://stackoverflow.com/a/45629557/10534012) – Darkman Jul 14 '21 at 17:15
  • @Darkman thanks, it's ok it works but I'm also interested in understanding why my solution has that strange bahaviour; any idea? thanks – r08y Jul 15 '21 at 09:09
  • oops, ctrl+c ctrl+v is often tricky, actually in the "real world" there's *android.bluetooth.headset.profile.action.CONNECTION_STATE_CHANGED*, just made mistake writing SO question – r08y Jul 15 '21 at 13:24
  • question edited – r08y Jul 15 '21 at 13:33
  • Try using a diff filter like `android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED`. All filters are available [here](https://gist.github.com/pwittchen/1aba5f9a1f71d7770f76). If it doesn't work, then you're of out luck. – Darkman Jul 15 '21 at 17:25

0 Answers0