BluetoothLeGatt
Android 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
最佳答案
我认为给出答案有点晚了,但今天我有同样的疑问,我找到了一个明确的答案。
使用 setCharacteristicNotification()
启用本地通知(在 android 设备上)并将 CCC 描述符设置为 ENABLE_NOTIFICATION_VALUE
启用 ble 外围设备上的通知。事实上,要启用 CCC 通知,您必须使用 setValue()
和 writeDescriptor()
,它们是用于将特征(在本例中为特征描述符)写入远程设备的方法。
我发现这个:http://processors.wiki.ti.com/index.php/SensorTag_User_Guide
关于android - 为什么 setCharacteristicNotification() 实际上没有启用通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22817005/