开发者

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开发者_StackOverflow社区 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


You have to setup a BroadcastReceiver for android.bluetooth.headset.action.STATE_CHANGED action. The Intent's extra android.bluetooth.headset.extra.STATE contains current state (disconnected, connecting, connected). More info the Android source code

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜