Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Verify whether bluetooth headsets are connected on Android?

Tags:

How to verify if there is any bluetooth headsets conected to android at the moment?

Like:

  • If there is a headset conected, the sound must route to this headset

  • But if there isnt a headset, the sound must stay on speaker

  • This must check during the application because if the battery of the headset goes off it must send sound to speaker back

Solution below /

public class BluetoothReceiver extends BroadcastReceiver {     private AudioManager localAudioManager;     private static final int STATE_DISCONNECTED  = 0x00000000;     private static final String EXTRA_STATE = "android.bluetooth.headset.extra.STATE";     private static final String TAG = "BluetoothReceiver";     private static final String ACTION_BT_HEADSET_STATE_CHANGED  = "android.bluetooth.headset.action.STATE_CHANGED";     private static final String ACTION_BT_HEADSET_FORCE_ON = "android.bluetooth.headset.action.FORCE_ON";     private static final String ACTION_BT_HEADSET_FORCE_OFF = "android.bluetooth.headset.action.FORCE_OFF";      @Override     public void onReceive(final Context context, final Intent intent) {         Log.i(TAG,"onReceive - BluetoothBroadcast");         localAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);         final String action = intent.getAction();         if (action.equals(ACTION_BT_HEADSET_STATE_CHANGED)) {             final int extraData = intent.getIntExtra(EXTRA_STATE, STATE_DISCONNECTED);             if (extraData == STATE_DISCONNECTED) {                 localAudioManager.setBluetoothScoOn(false);                 localAudioManager.stopBluetoothSco();                 localAudioManager.setMode(AudioManager.MODE_NORMAL);                 Log.i(TAG, "Bluetooth Headset Off " + localAudioManager.getMode());                 Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall());             } else {                             localAudioManager.setMode(0);                 localAudioManager.setBluetoothScoOn(true);                 localAudioManager.startBluetoothSco();                 localAudioManager.setMode(AudioManager.MODE_IN_CALL);                 Log.i(TAG, "Bluetooth Headset On " + localAudioManager.getMode());                 Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall());             }         }             if (action.equals(ACTION_BT_HEADSET_FORCE_ON)) {             localAudioManager.setMode(0);             localAudioManager.setBluetoothScoOn(true);             localAudioManager.startBluetoothSco();             localAudioManager.setMode(AudioManager.MODE_IN_CALL);             Log.i(TAG, "Bluetooth Headset On " + localAudioManager.getMode());             Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall());         }          if (action.equals(ACTION_BT_HEADSET_FORCE_OFF)) {             localAudioManager.setBluetoothScoOn(false);             localAudioManager.stopBluetoothSco();             localAudioManager.setMode(AudioManager.MODE_NORMAL);             Log.i(TAG, "Bluetooth Headset Off " + localAudioManager.getMode());             Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall());         }     } } 
like image 828
Arthur Jardim Giareta Conti Avatar asked Feb 15 '12 15:02

Arthur Jardim Giareta Conti


People also ask

How do I know if my Bluetooth headset is connected Android?

Using Bluetooth is safe by touching it and holding it. Tap a smartphone with an attached Bluetooth device on the list of paired devices. Once the phone and Bluetooth device are connected, the connected vehicle is labeled connected.

How do you check Bluetooth is connected or not?

Make sure Bluetooth is turned on. Touch and hold Bluetooth . In the list of paired devices, tap a paired but unconnected device. When your phone and the Bluetooth device are connected, the device shows as "Connected."


1 Answers

Moving this to an answer so that further comments can made. Works great!

public class BluetoothReceiver extends BroadcastReceiver {     private AudioManager localAudioManager;     private static final int STATE_DISCONNECTED  = 0x00000000;     private static final String EXTRA_STATE = "android.bluetooth.headset.extra.STATE";     private static final String TAG = "BluetoothReceiver";     private static final String ACTION_BT_HEADSET_STATE_CHANGED  = "android.bluetooth.headset.action.STATE_CHANGED";     private static final String ACTION_BT_HEADSET_FORCE_ON = "android.bluetooth.headset.action.FORCE_ON";     private static final String ACTION_BT_HEADSET_FORCE_OFF = "android.bluetooth.headset.action.FORCE_OFF";      @Override     public void onReceive(final Context context, final Intent intent) {         Log.i(TAG,"onReceive - BluetoothBroadcast");         localAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);         final String action = intent.getAction();         if (action.equals(ACTION_BT_HEADSET_STATE_CHANGED)) {             final int extraData = intent.getIntExtra(EXTRA_STATE, STATE_DISCONNECTED);             if (extraData == STATE_DISCONNECTED) {                 localAudioManager.setBluetoothScoOn(false);                 localAudioManager.stopBluetoothSco();                 localAudioManager.setMode(AudioManager.MODE_NORMAL);                 Log.i(TAG, "Bluetooth Headset Off " + localAudioManager.getMode());                 Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall());             } else {                             localAudioManager.setMode(0);                 localAudioManager.setBluetoothScoOn(true);                 localAudioManager.startBluetoothSco();                 localAudioManager.setMode(AudioManager.MODE_IN_CALL);                 Log.i(TAG, "Bluetooth Headset On " + localAudioManager.getMode());                 Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall());             }         }             if (action.equals(ACTION_BT_HEADSET_FORCE_ON)) {             localAudioManager.setMode(0);             localAudioManager.setBluetoothScoOn(true);             localAudioManager.startBluetoothSco();             localAudioManager.setMode(AudioManager.MODE_IN_CALL);             Log.i(TAG, "Bluetooth Headset On " + localAudioManager.getMode());             Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall());         }          if (action.equals(ACTION_BT_HEADSET_FORCE_OFF)) {             localAudioManager.setBluetoothScoOn(false);             localAudioManager.stopBluetoothSco();             localAudioManager.setMode(AudioManager.MODE_NORMAL);             Log.i(TAG, "Bluetooth Headset Off " + localAudioManager.getMode());             Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall());         }     } } 
like image 178
StephenG Avatar answered Oct 09 '22 03:10

StephenG