I'm scratching my head trying to find a way to detect bluetooth headset connect and disconnect events for Android 2.1. I see in API Level 11 that there are some explicit ones, but how do I do it in API level 7? I just want to know when the user connects or disconnects a headset or car stereo capable of playing audio, so that I can pause the sounds I'm playing.
There is no public APIs, This answer might help where the author used private APIs using reflections.
The author has also posted a comment on how he got it to work.
This looks like a good option to detect bluetooth connect/disconnect.
If that didn't work, another good option is to set a timer in a service that calls AudioManager.isBluetoothA2dpOn()
to check if the bluetooth is connected or disconnected.
Not sure if this works in 2.1, but it works in 2.2 and 2.3.
It will capture Bluetooth-Headset connection state changes:
Declare the following intent-filter
<intent-filter >
<action android:name="android.bluetooth.headset.action.AUDIO_STATE_CHANGED" />
</intent-filter>
and in your Receiver in onReceive check for:
if ("android.bluetooth.headset.action.AUDIO_STATE_CHANGED".equals(intent.getAction())) {
headsetAudioState = intent.getIntExtra("android.bluetooth.headset.extra.AUDIO_STATE", -2);
}
and save the int as a static variable. Access it anytime you want to know if BT audio is connected(1) / disconnected(0). Not pretty, but gets the job done.
Also check out: https://github.com/android/platform_frameworks_base/blob/gingerbread/core/java/android/bluetooth/BluetoothHeadset.java
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With