我正在开发一个 Android 应用程序,它应该订阅多个 BLE 特性。
但无论我做什么,我都只收到一个特征的更新值。
这是代码:
BluetoothGattCharacteristic characteristicVel = gatt.getService(BleDefinedUUIDs.Service.KOMMMODUL_SERVICE).getCharacteristic(BleDefinedUUIDs.Characteristic.VELOCITY);
gatt.setCharacteristicNotification(characteristicVel, true);
BluetoothGattDescriptor descriptorVel = characteristicVel.getDescriptor(
BleDefinedUUIDs.Descriptor.CHAR_CLIENT_CONFIG);
descriptorVel.setValue(BleDefinedUUIDs.Descriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptorVel);
BluetoothGattCharacteristic characteristicAcc = gatt.getService(BleDefinedUUIDs.Service.KOMMMODUL_SERVICE).getCharacteristic(BleDefinedUUIDs.Characteristic.ACCELERATION);
gatt.setCharacteristicNotification(characteristicAcc, true);
BluetoothGattDescriptor descriptorAcc = characteristicAcc.getDescriptor(
BleDefinedUUIDs.Descriptor.CHAR_CLIENT_CONFIG);
descriptorAcc.setValue(BleDefinedUUIDs.Descriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptorAcc);
无论我做什么,我都只能得到速度数据。如果我更改两个块的顺序,我只会获得加速度,但不会获得更多速度数据。
我该怎么做才能同时订阅多个特性?
提前致谢
雷托
要让描述符一个接一个地写入,请等待描述符写入的回调,然后再开始下一个描述符。
对于所有未来的读者,以下是如何操作:
List<BluetoothGattCharacteristic> characteristics = GetCharacteristicsWithNotifications(gatt);
subscribeToCharacteristics(gatt);
private void subscribeToCharacteristics(BluetoothGatt gatt) {
if(characteristics.size() == 0) return;
BluetoothGattCharacteristic characteristic = notifyCharacteristics.get(0);
gatt.setCharacteristicNotification(characteristic, true);
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
UUID uuid = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(uuid);
if(descriptor != null) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
characteristics.remove(0);
subscribeToCharacteristics(gatt);
}
您可以这样做:
override fun onDescriptorWrite(gatt: BluetoothGatt, descriptor: BluetoothGattDescriptor, status: Int) {
if (BleConstant.DESCRIPTOR_UUID == descriptor.uuid.toString().lowercase(Locale.getDefault())) {
Log.e(TAG,"Descriptor回调状态码:$status")
if (status == BluetoothGatt.GATT_SUCCESS) {
//移除第一个特征,开启第二个特征的通知
characteristicNotify.removeAt(0)
if(characteristicNotify.size != 0){
var characteristic = characteristicNotify[0]
if (!BleHelper.enableIndicateNotification(gatt, characteristic)){
gatt.disconnect()
} else {Log.e(TAG,"2000")}
}else{
gatt.apply {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) readPhy()
readDescriptor(descriptor)
readRemoteRssi() }
}
}
}
}
enableIndicateNotification 内容如下:
fun enableIndicateNotification(gatt: BluetoothGatt,characteristicUUID:String): Boolean{
//查找service uuid
var mService = gatt.getService(UUID.fromString(BleConstant.SERVICE_UUID))
if(mService == null){
Log.e(TAG,"service is null")
gatt.disconnect() //gatt断开连接
gatt.close() //关闭gatt连接
return false
}else {
Log.e(TAG,"service is connected")
}
//订阅特征的通知 并使能 成功onDescriptorWrite将回调
//查找 characteristic uuid
var mCharacteristic = mService.getCharacteristic(UUID.fromString(characteristicUUID))
if(gatt.setCharacteristicNotification(mCharacteristic,true))
//查找descriptor uuid,开启通知
var descriptor=mCharacteristic.getDescriptor(UUID.fromString(BleConstant.DESCRIPTOR_UUID))
descriptor.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
if(gatt.writeDescriptor(descriptor)) Log.e(TAG,"descriptor is connected")
return true
}