Open guozhiqiang123 opened 6 years ago
源码是有使用CLIENT_CHARACTERISTIC_CONFIG的,在BleConnectWorker类中setCharacteristicNotification方法,你可以看看
@Override
public boolean setCharacteristicNotification(UUID service, UUID character, boolean enable) {
checkRuntime();
BluetoothLog.v(String.format("setCharacteristicNotification for %s, service = %s, character = %s, enable = %b",
getAddress(), service, character, enable));
BluetoothGattCharacteristic characteristic = getCharacter(service, character);
if (characteristic == null) {
BluetoothLog.e(String.format("characteristic not exist!"));
return false;
}
if (mBluetoothGatt == null) {
BluetoothLog.e(String.format("ble gatt null"));
return false;
}
if (!mBluetoothGatt.setCharacteristicNotification(characteristic, enable)) {
BluetoothLog.e(String.format("setCharacteristicNotification failed"));
return false;
}
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(Constants.CLIENT_CHARACTERISTIC_CONFIG);
if (descriptor == null) {
BluetoothLog.e(String.format("getDescriptor for notify null!"));
return false;
}
byte[] value = (enable ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
if (!descriptor.setValue(value)) {
BluetoothLog.e(String.format("setValue for notify descriptor failed!"));
return false;
}
if (!mBluetoothGatt.writeDescriptor(descriptor)) {
BluetoothLog.e(String.format("writeDescriptor for notify failed"));
return false;
}
return true;
}
有可能是设备硬件问题,硬件中没有默认的 CLIENT_CHARACTERISTIC_CONFIG,需要你自己去遍历查找,确定哪一个特征是你需要的
查了一下原因,大概如下: 通过serviceId获取到对应的service,并从service中获取到对应的BluetoothGattCharacteristic信息。根据BluetoothGattCharacteristic获取到对应的BluetoothGattDescriptor,但是这里每次获取都是为空的,所以无法进行设置,这样的话Android是无法收到蓝牙发送的消息的。
希望博主能修复这个问题: https://www.2cto.com/kf/201802/721207.html