一喂饥、背景
本文主要給前端同學(xué)提供建議,對(duì)于android的原生開(kāi)發(fā)者可能過(guò)于簡(jiǎn)單哦肠鲫。
庫(kù):flutter_blue
flutter下使用該庫(kù)開(kāi)發(fā)ble應(yīng)用,在調(diào)用如下方法時(shí):
await mCharacteristic.setNotifyValue(true);
出現(xiàn):
E/flutter (18304): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(set_notification_error, could not locate CCCD descriptor for characteristic: xxxxx, null)
E/flutter (18304): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
E/flutter (18304): #1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:321:33)
二或粮、分析
我們可以從log的報(bào)錯(cuò)信息:could not locate CCCD descriptor for characteristic 中可以看出報(bào)錯(cuò)的原因是沒(méi)有找到cccd的描述符导饲。
首先我們?cè)趃ithub的Issues中找找,找到如下的修復(fù)信息:
chaochaox1990 commented on 27 Dec 2019
@chaochaox1990 same error, have you fixed it?
yes I already fixed. All I do just change the value of CCCD.
change the value of CCCD氯材!
那么CCCD是什么的渣锦,這里博主給大家介紹一下。在Android中的GATT中的可Notify的characteristic中氢哮,有個(gè)特征值的描述Descriptor(uuid:00002902-0000-1000-8000-00805f9b34fb)袋毙,這個(gè)值由藍(lán)牙硬件出廠的時(shí)候?qū)懭耄饕饔镁褪莄haracteristic的權(quán)限控制冗尤,例如听盖,是否開(kāi)啟等。在Android中的構(gòu)造方法如下:
public BluetoothGattDescriptor(UUID uuid, int permissions) {
initDescriptor(null, uuid, 0, permissions);
}
一般來(lái)說(shuō)uuid如上裂七,是固定的皆看,參數(shù)為其讀寫等權(quán)限。BluetoothGattDescriptor詳細(xì)介紹
那么為什么插件會(huì)報(bào)這個(gè)錯(cuò)呢背零,我們進(jìn)入其源碼看一下腰吟,在bluetooth_characteristic.dart文件中,可以看到報(bào)錯(cuò)代碼如下:
await FlutterBlue.instance._channel
.invokeMethod('setNotification', request.writeToBuffer());
這里還是看不出端倪來(lái)徙瓶,那我們?nèi)ithub項(xiàng)目中的源碼看一看毛雇,這里它調(diào)用的是setNotification這個(gè)方法。我們來(lái)到這個(gè)路徑:android/src/main/java/com/pauldemarco/flutter_blue/FlutterBluePlugin.java侦镇,查看這部分代碼:
case "setNotification":
{
byte[] data = call.arguments();
Protos.SetNotificationRequest request;
try {
request = Protos.SetNotificationRequest.newBuilder().mergeFrom(data).build();
} catch (InvalidProtocolBufferException e) {
result.error("RuntimeException", e.getMessage(), e);
break;
}
BluetoothGatt gattServer;
BluetoothGattCharacteristic characteristic;
BluetoothGattDescriptor cccDescriptor;
try {
gattServer = locateGatt(request.getRemoteId());
characteristic = locateCharacteristic(gattServer, request.getServiceUuid(), request.getSecondaryServiceUuid(), request.getCharacteristicUuid());
cccDescriptor = characteristic.getDescriptor(CCCD_ID);
if(cccDescriptor == null) {
throw new Exception("could not locate CCCD descriptor for characteristic: " +characteristic.getUuid().toString());
}
} catch(Exception e) {
result.error("set_notification_error", e.getMessage(), null);
return;
}
byte[] value = null;
if(request.getEnable()) {
boolean canNotify = (characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0;
boolean canIndicate = (characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0;
if(!canIndicate && !canNotify) {
result.error("set_notification_error", "the characteristic cannot notify or indicate", null);
return;
}
if(canIndicate) {
value = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE;
}
if(canNotify) {
value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE;
}
} else {
value = BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
}
if(!gattServer.setCharacteristicNotification(characteristic, request.getEnable())){
result.error("set_notification_error", "could not set characteristic notifications to :" + request.getEnable(), null);
return;
}
if(!cccDescriptor.setValue(value)) {
result.error("set_notification_error", "error when setting the descriptor value to: " + value, null);
return;
}
if(!gattServer.writeDescriptor(cccDescriptor)) {
result.error("set_notification_error", "error when writing the descriptor", null);
return;
}
result.success(null);
break;
}
這部分就是在Android環(huán)境下執(zhí)行的代碼灵疮。從:
cccDescriptor = characteristic.getDescriptor(CCCD_ID);
if(cccDescriptor == null) {
throw new Exception("could not locate CCCD descriptor for characteristic: " +characteristic.getUuid().toString());
}
static final private UUID CCCD_ID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
可以看出,只要characteristic中沒(méi)有uuid為"00002902-0000-1000-8000-00805f9b34fb"的descriptor虽缕,就會(huì)報(bào)錯(cuò)始藕,下面notify的關(guān)鍵代碼沒(méi)有執(zhí)行,因此notify失敗氮趋。
gattServer.setCharacteristicNotification(characteristic, request.getEnable())
這看似苛刻伍派,其實(shí)這就是標(biāo)準(zhǔn)∈P玻可以notify的characteristic必須要有uuid為:"00002902-0000-1000-8000-00805f9b34fb"的descriptor诉植。很多硬件都會(huì)有這個(gè)問(wèn)題呢?造成這樣的原因就是國(guó)內(nèi)藍(lán)牙廠商開(kāi)發(fā)標(biāo)準(zhǔn)參差不齊昵观,要不是沒(méi)有descriptor晾腔,就是descriptor的uuid不是標(biāo)準(zhǔn)的舌稀。那么為什么他們不統(tǒng)一標(biāo)準(zhǔn)的,因?yàn)樵谠_(kāi)發(fā)中灼擂,我們一般不這樣苛刻壁查,下面給大家看看我們?cè)话阍趺磳懙模?/p>
String bluetoothGattInfoKey = entry.getKey();
BluetoothGattChannel bluetoothGattInfoValue = entry.getValue();
if (bluetoothGatt != null && bluetoothGattInfoValue.getCharacteristic() != null) {
BreoLog.d("enable-" + bluetoothGattInfoValue.getCharacteristic().getUuid().toString());
success = bluetoothGatt.setCharacteristicNotification(bluetoothGattInfoValue.getCharacteristic(), enable);
}
BluetoothGattDescriptor bluetoothGattDescriptor = null;
if (bluetoothGattInfoValue.getCharacteristic() != null && bluetoothGattInfoValue.getDescriptor() != null) {
bluetoothGattDescriptor = bluetoothGattInfoValue.getDescriptor();
} else if (bluetoothGattInfoValue.getCharacteristic() != null && bluetoothGattInfoValue.getDescriptor() == null) {
if (bluetoothGattInfoValue.getCharacteristic().getDescriptors() != null
&& bluetoothGattInfoValue.getCharacteristic().getDescriptors().size() == 1) {
bluetoothGattDescriptor = bluetoothGattInfoValue.getCharacteristic().getDescriptors().get(0);
} else {
bluetoothGattDescriptor = bluetoothGattInfoValue.getCharacteristic()
.getDescriptor(UUID.fromString(BleConstant.CLIENT_CHARACTERISTIC_CONFIG));
}
}
if (bluetoothGattDescriptor != null) {
bluetoothGattInfoValue.setDescriptor(bluetoothGattDescriptor);
if (isIndication) {
if (enable) {
bluetoothGattDescriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
} else {
bluetoothGattDescriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
}
} else {
if (enable) {
bluetoothGattDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
} else {
bluetoothGattDescriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
}
}
if (bluetoothGatt != null) {
bluetoothGatt.writeDescriptor(bluetoothGattDescriptor);
}
}
看吧,這兩步操作是并行的剔应,有的話就設(shè)置一下睡腿,沒(méi)有也很佛系的。
這里給大家看兩張圖峻贮,圖一的就是不正常的席怪,characteristic是支持notify的,但是沒(méi)有descriptor纤控。圖二就是正常的挂捻,有descriptor。
三船万、解決方案
1刻撒、讓ble模塊的供應(yīng)商把descriptor寫進(jìn)去(推薦)。
2唬涧、修改一下flutter_blue插件疫赎,不讓它那么苛刻嚴(yán)格。
3碎节、試一試其他插件捧搞。pub倉(cāng)庫(kù)