2012-02-15 13 views
13

¿Cómo verificar si hay auriculares bluetooth conectados a Android en este momento?¿Cómo verificar si los auriculares bluetooth están conectados en Android?

igual:

  • Si hay un auricular conected, la ruta debe sonar a este auricular

  • Pero si no hay una auriculares, el sonido debe permanecer en el altavoz

  • Esto debe verificar durante la aplicación, ya que si la batería del auricular se apaga, debe enviar sonido a la parte posterior del altavoz

solución por debajo/

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()); 
     } 
    } 
} 
+1

Resuelto !!!!!!! –

+0

Acabo de limpiar el código y lo generalicé un poco para que otros usuarios tengan el mismo problema. No publique elementos no relacionados como "android.bluetooth.headset.action.FORCE_ON" o "android.bluetooth.headset.action.FORCE_OFF" (que se refieren a las acciones creadas por usted mismo). Además, no use comentarios en idiomas extranjeros, por favor. :) Por cierto, gracias por el código. :) – DragonWork

+0

La mejor y más completa solución que encontré! –

Respuesta

0

se mueve este a una respuesta de manera que más observaciones se pueden hacer. ¡Funciona genial!

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()); 
     } 
    } 
} 
Cuestiones relacionadas