一、背景
本文主要给前端同学提供建议,对于android的原生开发者可能过于简单哦。
flutter下使用该库开发ble应用,在调用如下方法时:
await mCharacteristic.setNotifyValue(true);
出现:
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的报错信息:could not locate CCCD descriptor for characteristic 中可以看出报错的原因是没有找到cccd的描述符。
首先我们在github的Issues中找找,找到如下的修复信息:
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中,有个特征值的描述Descriptor(uuid:00002902-0000-1000-8000-00805f9b34fb),这个值由蓝牙硬件出厂的时候写入,主要作用就是characteristic的权限控制,例如,是否开启等。在Android中的构造方法如下:
public BluetoothGattDescriptor(UUID uuid, int permissions) {
initDescriptor(null, uuid, 0, permissions);
}
一般来说uuid如上,是固定的,参数为其读写等权限。BluetoothGattDescriptor详细介绍
那么为什么插件会报这个错呢,我们进入其源码看一下,在bluetooth_characteristic.dart文件中,可以看到报错代码如下:
await FlutterBlue.instance._channel
.invokeMethod('setNotific