作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在构建一个与 BLE 通信的应用程序。我需要向 BluetoothGattDescriptor 写入一个 int 值。如果我在一切正常时这样做,但如果我想为每个写入 BluetoothGattDescriptor(在单独的特征中),我会在方法 mBluetoothGatt.writeDescriptor(descriptor) 中收到一个 false 值
但仅针对第二个特征,我第一次得到 true。我注意到,如果我将它们之间的延迟设置为大约 1.3 秒,那么我在两次尝试中都会收到 true。有人遇到同样的问题吗?
这里我发送延迟通知的注册:
Handler handler = new Handler();
registeredToNotification(BLEGattAttributes.UUID_STERN_DATA__REMOTE_CONTROLS_SETTINGS_SERVICE,
BLEGattAttributes.UUID_STERN_DATA_SETTINGS_REMOTES_CONTROL_READ_REQUEST_DELAY_IN, SettingsProperties.DELAY_IN_REGISTER_NOTIFICATION_REQUEST);
handler.postDelayed(() -> registeredToNotification(BLEGattAttributes.UUID_STERN_DATA__REMOTE_CONTROLS_SETTINGS_SERVICE,
BLEGattAttributes.UUID_STERN_DATA_SETTINGS_REMOTES_CONTROL_READ_REQUEST_DELAY_OUT, SettingsProperties.DELAY_OUT_REGISTER_NOTIFICATION_REQUEST), 1300);
handler.postDelayed(() -> registeredToNotification(BLEGattAttributes.UUID_STERN_DATA__REMOTE_CONTROLS_SETTINGS_SERVICE,
BLEGattAttributes.UUID_STERN_DATA_SETTINGS_REMOTES_CONTROL_READ_REQUEST_SHORT_WASH, SettingsProperties.SHORT_FLUSH_REGISTER_NOTIFICATION_REQUEST), 2600);
handler.postDelayed(() -> registeredToNotification(BLEGattAttributes.UUID_STERN_DATA__REMOTE_CONTROLS_SETTINGS_SERVICE,
BLEGattAttributes.UUID_STERN_DATA_SETTINGS_REMOTES_CONTROL_READ_REQUEST_LONG_WASH, SettingsProperties.LONG_FLUSH_REGISTER_NOTIFICATION_REQUEST), 3900);
handler.postDelayed(() -> registeredToNotification(BLEGattAttributes.UUID_STERN_DATA__REMOTE_CONTROLS_SETTINGS_SERVICE,
BLEGattAttributes.UUID_STERN_DATA_SETTINGS_REMOTES_CONTROL_READ_REQUEST_SECURITY_TIME, SettingsProperties.SECURITY_TIME_REGISTER_NOTIFICATION_REQUEST), 5200);
这是这些命令的日志:
D/IDTEST: ............................setRegisterToNotification
D/IDTEST: ......setRegisterToNotification ID = 53
D/IDTEST: ............................Is descriptor registered? = true
D/IDTEST: ............................Is Notification registered? = true
D/IDTEST: ............................setRegisterToNotification
D/IDTEST: ......setRegisterToNotification ID = 56
D/IDTEST: ............................Is descriptor registered? = true
D/IDTEST: ............................Is Notification registered? = true
D/IDTEST: ............................setRegisterToNotification
D/IDTEST: ......setRegisterToNotification ID = 63
D/IDTEST: ............................Is descriptor registered? = true
D/IDTEST: ............................Is Notification registered? = true
D/IDTEST: ............................setRegisterToNotification
D/IDTEST: ......setRegisterToNotification ID = 60
D/IDTEST: ............................Is descriptor registered? = true
D/IDTEST: ............................Is Notification registered? = true
D/IDTEST: ............................setRegisterToNotification
D/IDTEST: ......setRegisterToNotification ID = 66
D/IDTEST: ............................Is descriptor registered? = true
D/IDTEST: ............................Is Notification registered? = true
setRegisterToNotification() 方法:
public void setRegisterToNotification(BLEDeviceConnectionManager.DataClass dataClass) {
Log.d("IDTEST", "............................setRegisterToNotification");
Log.d("IDTEST", "......setRegisterToNotification ID = " + dataClass.getRequestID());
BluetoothGattCharacteristic characteristic = mBluetoothGatt.getService(dataClass.getServiceUUid()).getCharacteristic(dataClass.getCharacteristicsUUid());
BluetoothGattDescriptor descriptor = characteristic.getDescriptors().get(0);
if (descriptor != null) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
descriptor.setValue(BleDataParser.getInstance().intTobyteArray(dataClass.getRequestID()));
boolean isRegistered = mBluetoothGatt.writeDescriptor(descriptor);
Log.d("IDTEST", "............................Is descriptor registered? = " + isRegistered);
}
boolean isRegistered = mBluetoothGatt.setCharacteristicNotification(characteristic, dataClass.isEnableNotification());
Log.d("IDTEST", "............................Is Notification registered? = " + isRegistered);
}
最佳答案
需要等待描述符回调后再写。这是在 BluetoothGattCallback#onDescriptorWrite 中找到的当您调用 connectGatt 时使用.
关于Android BLE BluetoothGattDescriptor writeDescriptor 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52208563/
我的 CLIENT_CHARACTERISTIC_CONFIG 是 public final static UUID CLIENT_CHARACTERISTIC_CONFIG = UUID.from
我正在构建一个与 BLE 通信的应用程序。我需要向 BluetoothGattDescriptor 写入一个 int 值。如果我在一切正常时这样做,但如果我想为每个写入 BluetoothGattDe
我是一名优秀的程序员,十分优秀!