如何验证Android上是否连接了蓝牙耳机?

时间:2012-02-15 15:00:34

标签: android audio bluetooth headset

如何验证目前是否有任何蓝牙耳机连接到安卓?

像:

  • 如果有耳机连接,声音必须通过此耳机

  • 但如果没有耳机,声音必须留在扬声器上

  • 这必须在申请期间检查,因为如果耳机的电池熄灭,它必须向发言人发回声音

下面的解决方案/

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