2012-02-15 3 views
13

Как проверить, есть ли на данный момент гарнитуры Bluetooth, подключенные к Android?Как проверить, подключены ли Bluetooth-гарнитуры на Android?

Как:

  • Если есть гарнитура проведён, звук обязательно маршрут к гарнитуре

  • Но если там не наушники, звук должен остаться на динамик

  • Это необходимо проверить во время приложения, потому что если аккумулятор гарнитуры отключается, он должен отправить звук на спикер.

Решение ниже/

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

Решено !!!!!!! –

+0

Я только что очистил ваш код и немного обобщил его для других пользователей, имеющих такую ​​же проблему. Не размещайте несвязанные вещи, такие как «android.bluetooth.headset.action.FORCE_ON» или «android.bluetooth.headset.action.FORCE_OFF» (которые относятся к вашим самосозданным действиям). Также не используйте комментарии на иностранном языке, пожалуйста. :) Кстати, спасибо за код. :) – DragonWork

+0

Самое лучшее и самое полное решение, которое я нашел! –

ответ

0

Перемещение это ответ так, что дальнейшие комментарии могут сделаны. Прекрасно работает!

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