android - Android BLE BluetoothGattDescriptor writeDescriptor 问题
问题描述
我正在构建一个与 BLE 通信的应用程序。我需要向 BluetoothGattDescriptor 写入一个 int 值。如果我在一切正常时执行此操作,但是如果我想为每个人写入 BluetoothGattDescriptor(在单独的特征中),我会在方法中收到一个错误值,mBluetoothGatt.writeDescriptor(descriptor)
但仅针对第二个特征,这是我第一次得到真实. 我注意到,如果我在它们之间设置大约 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);
}
解决方案
在再次写入之前,您需要等待描述符回调。这可以在调用connectGatt时使用的BluetoothGattCallback#onDescriptorWrite中找到。
推荐阅读
- python - 添加两个列表的值并将总和为零
- modelica - 在 Modelica 中,如何在不连接 2 个块的情况下调用不同块中的变量?
- java - 创建了带有多个复选框的 Jpanel 并呈现到 JTable 单元格导致问题
- node.js - 在 core-js@2.6.12 安装后脚本失败。当我尝试在 Linux Vps 中上传 MEAN 应用程序时。我在 Windows 操作系统上制作这个项目
- javascript - 构建 REACT 项目错误消息(“index.html”= index.html 的入口点 html-webpack-plugin)
- amazon-web-services - 使用 terraform 对产品进行 AWS Service Catalog 预置
- c# - 在页面上显示数据时获取 System.Collections.Generic.List`1
- ios - 我在哪里可以使用 Apple 的 SF 符号?
- google-cloud-platform - How can I add labels to SQL Cloud snapshot and SQL Cloud network traffic in GCP?
- ruby-on-rails - Rails 路由:json 端点命名约定