我正在开发一个包含Bluegiga BLE113模块和Android应用程序的项目。在Bluegiga模块中,我设置了几个特征。对于一个特征,我定义了一个描述符来激活clie enter code here
nt通知(在我的例子中,客户端是Android应用程序。)。 BLE113模块的特征定义如下:
<characteristic uuid="dcfa2671-974d-4e4a-96cd-6221afeabb62" id="ez1_flow_data_out">
<!-- org.bluetooth.descriptor.gatt.characteristic_user_description -->
<description>Data to transmit to Android device</description>
<properties write="true" read="true" />
<value variable_length="true" length="255" type="hex" />
<!-- org.bluetooth.descriptor.gatt.client_characteristic_configuration -->
<descriptor uuid="2902">
<properties write="true" read="true" const="false" />
<!-- Bit 0 = 1: Notifications enabled -->
<value type="hex">0001</value>
</descriptor>
</characteristic>
在Android方面,我根据Android蓝牙低功耗指南在onServicesDiscovered(...)回调中设置了通知:
characteristic=gatt.getService(BLEuuids.DATAFLOW_SERVICE).getCharacteristic(BLEuuids.DATAFLOW_OUT_CHARACT);
//Enable local notifications
gatt.setCharacteristicNotification(characteristic, true);
//Enabled remote notifications
BluetoothGattDescriptor desc = characteristic.getDescriptor(BLEuuids.DATAFLOW_OUT_DESCRIPT);
boolean test;
test = desc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); // return value = true
test = gatt.readDescriptor(desc); // return value = true
test = gatt.writeDescriptor(desc); // return value = true
但是,如果我使用Bluegiga模块的串行接口更改DATAFLOW_OUT_CHARACT特征UUID的值,则不会在我的Android应用程序上触发onCharacteristicChanged(...)的预期回调。
我是否正确设置了描述符,或者我的Android代码是否失败?
答案 0 :(得分:0)
现在启用通知以回调onCharactersticsChanged()并设置属性ENABLE_NOTIFICATION_VALUE和ENABLE_INDICATION_VALUE
Log.d(TAG, "setCharacteristicNotification");
if (mBluetoothAdapter == null || customBluetoothGatt == null) {
Log.d(TAG, "BluetoothAdapter not initialized");
return;
}cx
customBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
boolean success = customBluetoothGatt.writeDescriptor(descriptor);