一,蓝牙设备的连接,在广播注册之前连接:
1. 判断耳机的连接状态,我们比较常用的是广播的方式,但是在安卓8.0以后,如果耳机在注册广播之前连接,那么在注册广播,无法监听到耳机的状态,于是我们只能换一种方式去处理,代码如下:
AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
//获取当前使用的麦克风,设置媒体播放麦克风
if (mAudioManager.isWiredHeadsetOn()) {
//todo 有线耳机已连接,声音内放,从耳机输出
}else{
//todo 有线耳机未连接,声音外放,
}
2. 那么问题来了,如果连接了蓝牙设备呢,显然从广播的方式处理已经解决不了问题:
if (BluetoothProfile.STATE_CONNECTED == adapter.getProfileConnectionState(BluetoothProfile.HEADSET)) {
//todo 蓝牙设备已连接,声音内放,从蓝牙设备输出
} else if (BluetoothProfile.STATE_DISCONNECTED == adapter.getProfileConnectionState(BluetoothProfile.HEADSET)) {
//todo 蓝牙设备未连接,声音外放,
} else {
//todo 蓝牙设备未连接,声音外放,
}
二,广播注册之后,耳机的连接状态发生改变,通过广播监听即可:
广播代码:
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothProfile;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import tv.buka.roomSdk.util.LogUtil;
/**
* 耳机状态监听
* <p>
* Created by hwk on 2017/11/2.
*/
public class HeadsetPlugReceiver extends BroadcastReceiver {
private static final String TAG = "HeadsetPlugReceiver";
private HeadsetPlugListener mHeadsetPlugListener;
public HeadsetPlugReceiver(HeadsetPlugListener headsetPlugListener) {
this.mHeadsetPlugListener = headsetPlugListener;
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
LogUtil.e(TAG, action);
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
int state = adapter.getProfileConnectionState(BluetoothProfile.HEADSET);
if (BluetoothProfile.STATE_CONNECTED == state) {
mHeadsetPlugListener.onHeadsetPlug(true);
}
if (BluetoothProfile.STATE_DISCONNECTED== state) {
mHeadsetPlugListener.onHeadsetPlug(false);
}
}
} else if (Intent.ACTION_HEADSET_PLUG.equals(action)) {
if (intent.hasExtra("state")) {
if (intent.getIntExtra("state", 0) == 0) {
//外放
mHeadsetPlugListener.onHeadsetPlug(true);
} else if (intent.getIntExtra("state", 0) == 1) {
//耳机
mHeadsetPlugListener.onHeadsetPlug(false);
}
}
}
}
public interface HeadsetPlugListener {
void onHeadsetPlug(boolean isPlug);//true说明没有耳机 false说明有耳机
}
}
实现代码:
//耳机植入监听
mHeadsetPlugReceiver = new HeadsetPlugReceiver(new HeadsetPlugReceiver.HeadsetPlugListener() {
@Override
public void onHeadsetPlug(boolean isPlug) {
if (isPlug) {
if (BluetoothProfile.STATE_CONNECTED == adapter.getProfileConnectionState(BluetoothProfile.HEADSET)) {
//蓝牙设备输出
} else if (BluetoothProfile.STATE_DISCONNECTED == adapter.getProfileConnectionState(BluetoothProfile.HEADSET)) {
//外放
} else {
//外放
}
} else {
//外放
}
}
});
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_HEADSET_PLUG);
intentFilter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
registerReceiver(mHeadsetPlugReceiver, intentFilter);
三,声音内放、和外放的处理
//外放
public void loudSpeaker(Activity context) {
AudioManager audioManager = (AudioManager)context.getSystemService("audio");
audioManager.setSpeakerphoneOn(true);
context.setVolumeControlStream(0);
audioManager.setMode(0);
}
//内放
public void microSpeaker(Activity context) {
AudioManager audioManager = (AudioManager)context.getSystemService("audio");
audioManager.setSpeakerphoneOn(false);
context.setVolumeControlStream(0);
audioManager.setMode(0);
}
总结:研究微信、qq开启视频通话的时候,有线耳机、蓝牙耳机的连接与断开,发现如果同时连接,声音从最后发生改变的那个设备输出。