Flutter for ble 之set_notification_error, could not locate CCCD descriptor for characteristic分析(原生角度)

一、背景

本文主要给前端同学提供建议,对于android的原生开发者可能过于简单哦。

库:flutter_blue

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
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值