android 连接蓝牙耳机 的判断代码,如何验证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());

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值