***************************蓝牙有三中协议:
BluetoothA2dp //声音服务(蓝牙耳机) :车机向耳机传递声音数据
BluetoothA2dpSink //蓝牙音乐服务 (媒体音频) ; 有播放,上下一曲等功能,例如手机向车机传递 音乐数据
BluetoothHeadsetClient //蓝牙电话服务( 通话 );它的附属协议 pbap协议,里面包含有联系人等
初始化 BluetoothAdapter.getDefaultAdapter().getProfileProxy(BoardApplication.getContext(), new A2dpListener(), BluetoothProfile.A2DP);//耳机
private final class A2dpListener implements BluetoothProfile.ServiceListener { @Override public void onServiceConnected(int profile, BluetoothProfile proxy) {}//连接上蓝牙服务
@Override public void onServiceDisconnected(int profile) {}
}
BluetoothAdapter.getDefaultAdapter().getProfileProxy(BoardApplication.getContext(), new A2dpSinkListener(), BluetoothProfile.A2DP_SINK); //蓝牙音乐
private final class A2dpSinkListener implements BluetoothProfile.ServiceListener { @Override public void onServiceConnected(int profile, BluetoothProfile proxy) {}//连接上蓝牙服务
@Override public void onServiceDisconnected(int profile) {}
}
BluetoothAdapter.getDefaultAdapter().getProfileProxy(BoardApplication.getContext(), new HeatsetClientListener(), BluetoothProfile.HEADSET_CLIENT);//蓝牙电话
private final class HeatsetClientListener implements BluetoothProfile.ServiceListener { @Override public void onServiceConnected(int profile, BluetoothProfile proxy) {}//连接上蓝牙服务
@Override public void onServiceDisconnected(int profile) {}
}
蓝牙开关打开之后, 已配对列表: BluetoothAdapter.getBondedDevices
***************************蓝牙的广播
BluetoothAdapter.ACTION_STATE_CHANGED //蓝牙开关状态
(BluetoothAdapter.EXTRA_STATE)
BluetoothAdapter.STATE_TURNING_OFF //正在关闭状态
BluetoothAdapter.STATE_OFF //关闭
BluetoothAdapter.STATE_TURNING_ON //正在开启状态
BluetoothAdapter.STATE_ON //开启
BluetoothDevice.ACTION_PAIRING_REQUEST //请求配对
BluetoothDevice.ACTION_BOND_STATE_CHANGED //配对状态变化
BluetoothDevice.ACTION_PAIRING_CANCEL //取消配对
BluetoothDevice.ACTION_ACL_DISCONNECTED //远程的断开状态 ,蓝牙距离远导致的断开.(注:系统因为距断开蓝牙依旧会发送断开蓝牙的广播)
BluetoothDevice.ACTION_ACL_CONNECTED //远程的连接状态
BluetoothHeadsetClient.ACTION_AG_EVENT //手机蓝牙连接之后,获取的手机电量
BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED //电话连接状态更改
BluetoothA2dpSink.ACTION_CONNECTION_STATE_CHANGED //音乐连接状态更改
BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED //耳机连接状态更改
BluetoothAdapter.ACTION_SCAN_MODE_CHANGED //蓝牙的扫描模式
BluetoothAdapter.ACTION_DISCOVERY_STARTED//开始扫描
BluetoothAdapter.ACTION_DISCOVERY_FINISHED//扫描结束
BluetoothDevice.ACTION_FOUND //发现设备
BluetoothDevice.ACTION_NAME_CHANGED//本机蓝牙名称变更
BluetoothDevice.ACTION_UUID//支持carplay
***************************获取蓝牙状态
mBluetoothAdapter.getProfileConnectionState(BluetoothProfile.A2DP);//可操控蓝牙设备,如带播放暂停功能的蓝牙耳机
bluetoothAdapter.getProfileConnectionState(BluetoothProfile.A2DP_SINK);//媒体协议
bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET_CLIENT);//通话协议
BluetoothA2dpSink/BluetoothHeadsetClient/BluetoothA2dp .disconnect 断开该服务的连接