How to get the connection status of a Bluetooth headset when launching an application using Android2.2?

How to get the connection status of a Bluetooth headset (connected / disconnected) using the 8th level airline api when the application starts? Are there any sticky broadcast intentions in android2.2? Is there an API to retrieve the initial state of a Bluetooth headset? Is there access to a crawl?

0
source share
3 answers

I understood the solution:

private static final String ACTION_BT_HEADSET_STATE_CHANGED = "android.bluetooth.headset.action.STATE_CHANGED"; private static final int STATE_CONNECTED = 0x00000002; private static final int STATE_DISCONNECTED = 0x00000000; private static final String EXTRA_STATE = "android.bluetooth.headset.extra.STATE"; private BroadcastReceiver mBlueToothHeadSetEventReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { try { String action = intent.getAction(); if(action == null) return; if(action.equals(ACTION_BT_HEADSET_STATE_CHANGED)){ int extraData = intent.getIntExtra(EXTRA_STATE , STATE_DISCONNECTED); if(extraData == STATE_CONNECTED ){ //TODO : }else if(extraData == STATE_DISCONNECTED){ //TODO: } } } catch (Exception e) { //TODO: } } }; 
+3
source

This will not work when the application starts, but as intent filters are automatically registered, you can have a valid value after the application starts:

Announce the next intent filter

  <intent-filter > <action android:name="android.bluetooth.headset.action.AUDIO_STATE_CHANGED" /> </intent-filter> 

and in your receiver in onReceive check:

 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 sound is connected (1) / muted (0). Not beautiful, but it does its job.

Also check: https://github.com/android/platform_frameworks_base/blob/gingerbread/core/java/android/bluetooth/BluetoothHeadset.java

0
source

from packages / applications / Phone / src / com / android / phone / PhoneGlobals.java: 1449

  } else if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) { mBluetoothHeadsetState = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_DISCONNECTED); if (VDBG) Log.d(LOG_TAG, "mReceiver: HEADSET_STATE_CHANGED_ACTION"); if (VDBG) Log.d(LOG_TAG, "==> new state: " + mBluetoothHeadsetState); updateBluetoothIndication(true); // Also update any visible UI if necessary 
0
source

All Articles