【发布时间】:2019-03-02 09:23:03
【问题描述】:
我是 Android 开发新手,不明白如何正确打开所有 ble 通知并获取所有通知。
我尝试遍历所有服务
for(BluetoothGattService service : gatt.getServices()){
if( service.getUuid().equals(Step_UUID)) {
BluetoothGattCharacteristic characteristicData = service.getCharacteristic(Step_UUID);
for (BluetoothGattDescriptor descriptor : characteristicData.getDescriptors()) {
descriptor.setValue( BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
gatt.writeDescriptor(descriptor);
}
gatt.setCharacteristicNotification(characteristicData, true);
}
}
但我没有收到任何通知。 请帮助我不明白我做错了什么..
编辑
现在我使用这种方法在发现服务后启用通知:
public void setCharacteristicNotification(BluetoothGattCharacteristic 特征,启用布尔值) {
// Setting default write type according to CDT 222486
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
String serviceUUID = characteristic.getService().getUuid().toString();
String serviceName = GattAttributes.lookupUUID(characteristic.getService().getUuid(), serviceUUID);
String characteristicUUID = characteristic.getUuid().toString();
String characteristicName = GattAttributes.lookupUUID(characteristic.getUuid(), characteristicUUID);
String descriptorUUID = GattAttributes.CLIENT_CHARACTERISTIC_CONFIG;
String descriptorName = GattAttributes.lookupUUID(UUIDDatabase.UUID_CLIENT_CHARACTERISTIC_CONFIG, descriptorUUID);
if (characteristic.getDescriptor(UUID.fromString(GattAttributes.CLIENT_CHARACTERISTIC_CONFIG)) != null) {
if (enabled == true) {
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(GattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
boolean aaa = mBluetoothGatt.writeDescriptor(descriptor);
aaa = mBluetoothGatt.writeDescriptor(descriptor);
aaa = false;
} else {
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(GattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
}
}
boolean aaaa = mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
aaaa = false;
}
问题是第一个特征可以很好地通知,但是我尝试启用的所有其他特征都失败了
mBluetoothGatt.writeDescriptor(descriptor);
不知道怎么办……
【问题讨论】:
-
在您的代码中启用了指示,但您说您想要通知?此外,您一次只能有一个未完成的 GATT 操作(请参阅您的 writeDescriptor 调用)。您需要等待 onDescriptorWrite 才能继续。
-
您一次只能有一个未完成的 GATT 操作(请参阅您的 writeDescriptor 调用)。您需要等待 onDescriptorWrite 才能继续。如果你不这样做,writeDescriptor 将返回 false。
-
Emil 感谢您提供钥匙!
标签: java android bluetooth notifications bluetooth-lowenergy