Android BLE (二)—连接
1、连接前准备
首先需要在扫描设备的时候拿到BluetoothDevice对象,Context(在Activity或者Application中获取到)和 连接回调接口BluetoothGattCallback(需要自己定义和编写代码) BluetoothGattCallback接口写法
private BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
@Override
public void onPhyUpdate(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
super.onPhyUpdate(gatt, txPhy, rxPhy, status);
}
@Override
public void onPhyRead(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
super.onPhyRead(gatt, txPhy, rxPhy, status);
}
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
}
@Override
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorRead(gatt, descriptor, status);
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
}
@Override
public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
super.onReliableWriteCompleted(gatt, status);
}
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
super.onReadRemoteRssi(gatt, rssi, status);
}
@Override
public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
super.onMtuChanged(gatt, mtu, status);
}
@Override
public void onServiceChanged(@NonNull BluetoothGatt gatt) {
super.onServiceChanged(gatt);
}
};
2、准备后就可以直接连接了
BluetoothDevice连接直接调用connect()方法就可以,connect方法有多个带有不一样参数的,我这里使用的是带三个参数的,第一个参数是上下文(Context),第二个参数是Boolean类型,主要是断开是否回连的标志,第三个就是连接回调接口BluetoothGattCallback,该返回BluetoothGatt对象,Bluetooth对象可以断开连接(disconnect方法)和回收资源(close方法)
mBluetoothDevice.connectGatt(mContext, false, gattCallback);
3、处理BluetoothGattCallback中的onConnectionStateChange方法
BluetoothDevice调用connect方法连接后,不管成功与否,都会调用onConnectionStateChange方法,我们在这个方法中处理:成功执行下一步,失败继续连接或者处理其他的,需要更改MTU的成功时调用gatt.requestMtu(80);,不需要则调用gatt.discoverServices();发现服务
@SuppressLint("MissingPermission")
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
if(status == BluetoothGatt.GATT_SUCCESS){
if(newState == BluetoothProfile.STATE_CONNECTED){
gatt.requestMtu(80);
}else if(newState == BluetoothProfile.STATE_DISCONNECTED){
gatt.close();
}
}else{
gatt.close();
}
}
4、处理gatt.requestMtu(80);的请求回调onMtuChanged
@SuppressLint("MissingPermission")
@Override
public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
super.onMtuChanged(gatt, mtu, status);
if(status == BluetoothGatt.GATT_SUCCESS){
gatt.discoverServices();
}else{
gatt.disconnect();
}
}
5、处理gatt.discoverServices();的回调方法onServicesDiscovered
在这个回调方法中,我们可以获取蓝牙服务Bluetooth、特征和描述符
@SuppressLint("MissingPermission")
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
List<BluetoothGattService> services = gatt.getServices();
if(services != null && services.size() > 0){
for(BluetoothGattService service:services){
List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
if(characteristics != null && characteristics.size() > 0){
for(BluetoothGattCharacteristic characteristic:characteristics){
if(gatt.setCharacteristicNotification(characteristic,true)){
List<BluetoothGattDescriptor> descriptors = characteristic.getDescriptors();
if(descriptors != null && descriptors.size() > 0){
for(BluetoothGattDescriptor descriptor:descriptors){
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}
}
}
}
}
}
}
}
6、使能特征后,就可以收到信息了,有信息接收到就会回调onCharacteristicChanged
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
byte[] bytes = characteristic.getValue();
}
7、写数据
使能号相关的特征值BluetoothGattCharacteristic后,保留该特征值对象
@SuppressLint("MissingPermission")
private void writeData(byte[] bytes){
if(mWriteCharacteristic != null ){
mWriteCharacteristic.setValue(bytes);
mBluetoothGatt.writeCharacteristic(mWriteCharacteristic);
}
}
8、断开连接
断开连接有两种方法,第一种BluetoothGatt调用disconnet方法,设备会断开连接,并回调onConnectionStateChange方法,但是不会回收资源;第二种BluetoothGatt调用close方法,直接断开连接并且回收资源,不会回调onConnectionStateChange方法。
|