java - 如何一次从两个服务中读取值
问题描述
我正在使用 BLE 开发一个项目,并希望一次从两个服务中读取值,但我只从第一个服务中获取值
我的 onServicesDiscovered 代码是
public void onServicesDiscovered(BluetoothGatt gatt, int status)
{
for (BluetoothGattService service : gatt.getServices()) {
if ((service == null) || (service.getUuid() == null)) {
continue;
}
if (BleUuid.SERVICE_DEVICE_INFORMATION.equalsIgnoreCase(service
.getUuid().toString()))
{
mConnGatt.readCharacteristic(service.getCharacteristic(UUID.fromString(BleUuid.CHAR_MANUFACTURER_NAME_STRING)));
}
if (BleUuid.CHAR_ALERT_LEVEL.equalsIgnoreCase(service
.getUuid().toString()))
{
mConnGatt.readCharacteristic(service.getCharacteristic(UUID.fromString(BleUuid.CHAR_SERIAL_NUMBEAR_STRING)));
}
}
解决方案
一次只能有一个未完成的 GATT 操作(readCharacteristic、writeCharacteristic、readDescriptor、writeDescriptor、requestMtu)。您需要等待相应的回调(onCharacteristicRead 等),直到您可以执行新的回调。
推荐阅读
- python - 使用单个“gspread”命令从多个谷歌工作表中获取值
- python - 使用python计算平均总季节性降水量
- python - 如果列重复并且只取嵌套字典中的第一个列,则删除键值
- vue.js - 将图像 vuejs 传递给控制器 laravel
- apache-spark - 将 Spark 2.4 更新到 Spark 3.1 后出现 Spark/Kafka 错误
- python-3.x - 单元测试中的第二个功能没有运行
- wordpress - 为什么转换在我的情况下不起作用
- networking - 在 Mininet 中如何模拟 UDP 泛洪导致的丢包?
- node.js - Next.js API 路由 + Node-APN:无法获取 .p8 Apple 密钥文件的路径以在部署到 Vercel 后发送推送通知
- go-cd - 在所有其他任务完成后运行 GoCD 任务?