1、蓝牙连接\断开连接
private void getProfileProxy() {
mAdapter.getProfileProxy(mContext, new ProxyServiceListener(), BluetoothProfile.HEADSET_CLIENT);
}
private final class ProxyServiceListener implements BluetoothProfile.ServiceListener {
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
Log.d(TAG,"Bluetooth service connected profile == "+profile);
if (profile == BluetoothProfile.HEADSET_CLIENT) {
isHeadSetProfileReady = true;
mHeadsetClient = (BluetoothHeadsetClient)proxy;
}
}
@Override
public void onServiceDisconnected(int profile) {
if (profile == BluetoothProfile.HEADSET_CLIENT) {
isHeadSetProfileReady = false;
mHeadsetClient = null;
}
}
}
BluetoothHeadsetClient连接设备
//获取连接状态
public int getConnectionState() {
if (mHeadsetClient != null) {
List<BluetoothDevice> deviceList = mHeadsetClient.getConnectedDevices();
if (deviceList.isEmpty()) {
return BluetoothProfile.STATE_DISCONNECTED;
} else {
return mHeadsetClient.getConnectionState(deviceList.remove(0));
}
}
return BluetoothProfile.STATE_DISCONNECTED;
}
//连接远程设备
public boolean connect(BluetoothDevice device) {
if (null != mHeadsetClient) {
return mHeadsetClient.connect(device);
}
Log.i(TAG,"connect mHeadsetClient == "+mHeadsetClient);
return false;
}
//断开连接
public boolean disconnect(BluetoothDevice device) {
if (null != mHeadsetClient) {
return mHeadsetClient.disconnect(device);
}
Log.i(TAG, "disconnect mHeadsetClient == "+mHeadsetClient);
return false;
}
2、拨打电话
// 拨号
public void dial(String number) {
Log.d(TAG, "dial clicked mIsProfileReady:" + isHeadSetProfileReady + ", mHeadsetClient:" + mHeadsetClient);
if (!mAdapter.isEnabled() || mHeadsetClient == null) {
ToastUtil.showToast(mContext, mContext.getResources().getString(R.string.bt_disable));
return;
}
if (isHeadSetProfileReady && mHeadsetClient != null) {
List<BluetoothDevice> deviceList = mHeadsetClient.getConnectedDevices();
if (deviceList.isEmpty()) {
ToastUtil.showToast(mContext, mContext.getResources().getString(R.string.text_tips_disconnected));
}else {
BluetoothDevice mDevice = deviceList.remove(0);
setSpeakerPhoneOn(true);
mHeadsetClient.dial(mDevice, number);
}
}
}
3、接听电话
// 接听电话
public void acceptCall(int flag) {
Log.d(TAG, "hangup clicked isHeadsetReady:" + isHeadSetProfileReady + ", mHeadset:" + mHeadsetClient);
if (isHeadSetProfileReady && mHeadsetClient != null) {
List<BluetoothDevice> deviceList = mHeadsetClient.getConnectedDevices();
if (deviceList.isEmpty()) {
ToastUtil.showToast(mContext, mContext.getResources().getString(R.string.text_tips_disconnected));
} else {
setSpeakerPhoneOn(true);
BluetoothDevice mDevice = deviceList.remove(0);
mHeadsetClient.acceptCall(mDevice,flag);
}
}
}
4、拒接电话
如果来电没有接听直接挂断就是拒接。
//拒接电话
public void hangup() {
Log.d(TAG, "hangup clicked isHeadsetReady:" + isHeadSetProfileReady + ", mHeadset:" + mHeadsetClient);
if (isHeadSetProfileReady && mHeadsetClient != null) {
List<BluetoothDevice> deviceList = mHeadsetClient.getConnectedDevices();
if (deviceList.isEmpty()) {
ToastUtil.showToast(mContext, mContext.getResources().getString(R.string.text_tips_disconnected));
} else {
setSpeakerPhoneOn(false);
BluetoothDevice mDevice = deviceList.remove(0);
mHeadsetClient.rejectCall(mDevice);
}
}
}
5、挂断电话
// 挂断所有电话
public void terminateCall(int callNumber) {
Log.d(TAG, "terminate clicked isHeadsetReady:" + isHeadSetProfileReady + ", mHeadset:" + mHeadsetClient);
if (isHeadSetProfileReady && mHeadsetClient != null) {
List<BluetoothDevice> deviceList = mHeadsetClient.getConnectedDevices();
if (deviceList.isEmpty()) {
ToastUtil.showToast(mContext, mContext.getResources().getString(R.string.text_tips_disconnected));
} else {
setSpeakerPhoneOn(false);
BluetoothDevice mDevice = deviceList.remove(0);
List<BluetoothHeadsetClientCall> calls = mHeadsetClient.getCurrentCalls(mDevice);
if (!calls.isEmpty()) {
BluetoothHeadsetClientCall call = calls.remove(callNumber);
mHeadsetClient.terminateCall(mDevice,call);
}
}
}
}
6、切换声道
// 切换声道
// Initiates a connection of audio channel.
public boolean connectAudio() {
Log.d(TAG, "hangup clicked isHeadsetReady:" + isHeadSetProfileReady + ", mHeadset:" + mHeadsetClient);
if (isHeadSetProfileReady && mHeadsetClient != null) {
List<BluetoothDevice> deviceList = mHeadsetClient.getConnectedDevices();
if (deviceList.isEmpty()) {
return false;
} else {
setSpeakerPhoneOn(true);
BluetoothDevice mDevice = deviceList.remove(0);
return mHeadsetClient.connectAudio(mDevice);
}
}
return false;
}
// Disconnects audio channel.
public boolean disconnectAudio() {
Log.d(TAG, "hangup clicked isHeadsetReady:" + isHeadSetProfileReady + ", mHeadset:" + mHeadsetClient);
if (isHeadSetProfileReady && mHeadsetClient != null) {
List<BluetoothDevice> deviceList = mHeadsetClient.getConnectedDevices();
if (deviceList.isEmpty()) {
return false;
} else {
setSpeakerPhoneOn(false);
BluetoothDevice mDevice = deviceList.remove(0);
return mHeadsetClient.disconnectAudio(mDevice);
}
}
return false;
}
7、监听通话状态
//在代码中注册广播接收器并添加过滤器
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED); //连接状态改变
filter.addAction(BluetoothHeadsetClient.ACTION_CALL_CHANGED); //通话状态改变
filter.addAction(BluetoothHeadsetClient.ACTION_AUDIO_STATE_CHANGED); //音频状态改变
registerReceiver(broadcastReceiver, filter);
//在广播接收器中处理相应的广播
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
// 连接状态改变广播
int state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1);
if (state == BluetoothProfile.STATE_CONNECTED) {
// 已连接
} else if (state == BluetoothProfile.STATE_DISCONNECTED) {
// 已断开连接
}
} else if (BluetoothHeadsetClient.ACTION_CALL_CHANGED.equals(action)) {
// 通话状态改变广播
int callState = intent.getIntExtra(BluetoothHeadsetClient.EXTRA_CALL_STATE, -1);
if (callState == BluetoothHeadsetClient.CALL_STATE_TERMINATED) {
// 通话结束
} else if (callState == BluetoothHeadsetClient.CALL_STATE_ACTIVE) {
// 通话中
}
} else if (BluetoothHeadsetClient.ACTION_AUDIO_STATE_CHANGED.equals(action)) {
// 音频状态改变广播
int audioState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1);
if (audioState == BluetoothHeadsetClient.STATE_AUDIO_CONNECTED) {
// 音频连接
} else if (audioState == BluetoothHeadsetClient.STATE_AUDIO_DISCONNECTED) {
// 音频断开连接
}
}
}
};
8、获取信号强度
//获取信号强度
Bundle bundle = bluetoothHeadsetClient.getCurrentAgEvents(BluetoothDevice);
if (bundle != null && bundle.containsKey(BluetoothHeadsetClient.EXTRA_NETWORK_SIGNAL_STRENGTH))
{
return bundle.getInt(BluetoothHeadsetClient.EXTRA_NETWORK_SIGNAL_STRENGTH);
}