2012-02-15 119 views
13

如何验证当前是否有任何蓝牙耳机连接到android?如何验证蓝牙耳机是否在Android上连接?

像:

  • 如果有conected耳机,声音必须能够路由到这款耳机

  • 但如果心不是耳机,声音必须留在扬声器

  • 这必须在应用期间检查,因为如果耳机的电池熄灭,它必须向扬声器发回声音

解决以下/

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

已解决!!!!!!! – 2012-09-18 16:44:24

+0

我刚刚清理了您的代码,并将其推广到其他用户遇到同样问题。不要发布无关的东西,如“android.bluetooth.headset.action.FORCE_ON”或“android.bluetooth.headset.action.FORCE_OFF”(它指的是你自己创建的动作)。请不要使用外语评论。顺便说一句,谢谢你的代码。 :) – DragonWork 2013-01-16 18:45:09

+0

我发现最好和最完整的解决方案! – 2015-04-01 11:50:07

回答

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