2012-02-15 156 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()); 
     } 
    } 
}