Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 2.1 Detect Bluetooth audio connect/disconnect

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.

like image 609
rustyshelf Avatar asked Jul 08 '11 02:07

rustyshelf


3 Answers

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.

like image 140
Dennis Mathews Avatar answered Nov 14 '22 00:11

Dennis Mathews


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.

like image 45
A. Abiri Avatar answered Nov 14 '22 01:11

A. Abiri


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

like image 35
icyerasor Avatar answered Nov 14 '22 01:11

icyerasor