Flutter for ble 之set_notification_error, could not locate CCCD descriptor for characteristic分析

一喂饥、背景
本文主要給前端同學(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)有也很佛系的。

image.png

image.png

這里給大家看兩張圖峻贮,圖一的就是不正常的席怪,characteristic是支持notify的,但是沒(méi)有descriptor纤控。圖二就是正常的挂捻,有descriptor。

三船万、解決方案
1刻撒、讓ble模塊的供應(yīng)商把descriptor寫進(jìn)去(推薦)。

2唬涧、修改一下flutter_blue插件疫赎,不讓它那么苛刻嚴(yán)格。

3碎节、試一試其他插件捧搞。pub倉(cāng)庫(kù)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市狮荔,隨后出現(xiàn)的幾起案子胎撇,更是在濱河造成了極大的恐慌,老刑警劉巖殖氏,帶你破解...
    沈念sama閱讀 221,198評(píng)論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件晚树,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡雅采,警方通過(guò)查閱死者的電腦和手機(jī)爵憎,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)婚瓜,“玉大人宝鼓,你說(shuō)我怎么就攤上這事“涂蹋” “怎么了愚铡?”我有些...
    開(kāi)封第一講書人閱讀 167,643評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我沥寥,道長(zhǎng)碍舍,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書人閱讀 59,495評(píng)論 1 296
  • 正文 為了忘掉前任邑雅,我火速辦了婚禮片橡,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘蒂阱。我一直安慰自己锻全,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,502評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布录煤。 她就那樣靜靜地躺著,像睡著了一般荞胡。 火紅的嫁衣襯著肌膚如雪妈踊。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書人閱讀 52,156評(píng)論 1 308
  • 那天泪漂,我揣著相機(jī)與錄音廊营,去河邊找鬼。 笑死萝勤,一個(gè)胖子當(dāng)著我的面吹牛露筒,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播敌卓,決...
    沈念sama閱讀 40,743評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼慎式,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了趟径?” 一聲冷哼從身側(cè)響起瘪吏,我...
    開(kāi)封第一講書人閱讀 39,659評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎蜗巧,沒(méi)想到半個(gè)月后掌眠,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,200評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡幕屹,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,282評(píng)論 3 340
  • 正文 我和宋清朗相戀三年蓝丙,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片望拖。...
    茶點(diǎn)故事閱讀 40,424評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡渺尘,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出靠娱,到底是詐尸還是另有隱情沧烈,我是刑警寧澤,帶...
    沈念sama閱讀 36,107評(píng)論 5 349
  • 正文 年R本政府宣布像云,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏委乌。R本人自食惡果不足惜咐蚯,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,789評(píng)論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望绩衷。 院中可真熱鬧,春花似錦、人聲如沸等脂。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 32,264評(píng)論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)上遥。三九已至,卻和暖如春争涌,著一層夾襖步出監(jiān)牢的瞬間粉楚,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,390評(píng)論 1 271
  • 我被黑心中介騙來(lái)泰國(guó)打工亮垫, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留模软,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,798評(píng)論 3 376
  • 正文 我出身青樓饮潦,卻偏偏與公主長(zhǎng)得像燃异,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子继蜡,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,435評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容