23

BluetoothLeGattAndroid BLE 示例包含以下代码:

public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
                                          boolean enabled) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

    // This is specific to Heart Rate Measurement.
    if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
                UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        mBluetoothGatt.writeDescriptor(descriptor);
    }
}

我的问题基本上是,为什么标记代码特定于心率测量?似乎拥有客户端特征配置描述符(CCCD)特征是控制特征通知的标准方式,那么为什么不setCharacteristicNotification()考虑写入它呢?既然它不这样做,那么setCharacteristicNotification()实际上做了什么?

我对 BLE 很陌生,互联网上没有任何关于它的解释,不假设你已经理解了这一切!所以不要以为我知道什么是 CCCD 或其他什么!很难找出 CCCD 甚至代表什么!

编辑:另请参阅此答案,它支持我对 CCCD 的理解(并且让我继续想知道为什么当有一个看起来应该为您执行此操作的功能时,您必须在 Android 中手动写入它们): https://devzone .nordicsemi.com/index.php/what-does-cccd-mean

4

6 回答 6

15

我认为给出答案有点晚了,但今天我有同样的疑问,我找到了一个明确的答案。使用setCharacteristicNotification()您在本地启用通知(在 android 设备上)并将 CCC 描述符设置为ENABLE_NOTIFICATION_VALUE您在 ble 外围设备上启用通知。事实上,为了启用 CCC 通知,您必须使用setValue()这些writeDescriptor()方法用于将特征(在本例中为特征描述符)写入远程设备。我发现这个: http: //processors.wiki.ti.com/index.php/SensorTag_User_Guide

于 2014-10-09T15:38:06.693 回答
13

以下是 O'Reilly 的《蓝牙低功耗入门》一书的节选:

要在 Android 上启用通知,您通常必须在本地启用您感兴趣的特定特性的通知。

完成后,您还必须通过写入设备的客户端特征配置描述符 (CCCD) 来启用对等设备上的通知

我相信这回答了你的问题。

所以

mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); 

指第一部分

mBluetoothGatt.writeDescriptor(descriptor); 

指2号。

于 2016-08-06T19:16:57.573 回答
4

对于遇到此问题的未来人们,这是我能找到的最佳答案:

通过写入客户端特征配置描述符,您(客户端)告诉 BLE 服务器切换配置。(这对我来说最初也没有意义,但是用英语:)

这告诉 BLE 设备切换模式(配置)以主动收集和报告对此特性的更改,而不是更改并仅在请求时报告。

它的名字很糟糕,但是通过文档挖掘它似乎也将用于客户可能要求的其他可能的特征更改:因此名称令人困惑。

https://developer.bluetooth.org/gatt/descriptors/Pages/DescriptorViewer.aspx?u=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml

这就引出了一个问题,BluetoothGatt.setCharacteristicNotification()如果我们只是通过修改描述符来重复我们的工作,为什么还要打电话?!挖掘BluetoothGatt源代码向我们展示了 setCharacteristicNotification 仅准备本地服务以接收通知,而不是启用持久更新。

于 2016-04-19T21:30:49.650 回答
0

我知道这看起来很傻,但是设置 CCCD 值是告诉 API 是否要打开通知或指示的唯一方法。

目前没有 setCharacteristicIndication。要启用指示,您必须调用 setCharacteristicNotification(混淆),然后将 BluetoothGattDescriptor.ENABLE_INDICATION_VALUE 写入 CCCD,类似于启用通知的操作。

于 2014-04-16T10:53:57.387 回答
0

所有其他答案并没有真正回答这个问题。

我的猜测是 Android BLE 团队假设一个应用程序可能有多个BluetoothGattCallback().

通过将通知启用(和禁用)分为两个步骤,它将允许BluetoothGattCallback观察者侦听 GATT 通知(仅调用setCharacteristicNotification()) - 并且只留下一个BluetoothGattCallback实现对 GATT 服务器(即 BLE 外围设备)执行写入操作。

于 2021-02-13T20:00:31.453 回答
-4

“客户端特征配置描述符定义了特定客户端如何配置特征。”

于 2015-10-06T01:14:16.417 回答