android - Flutter Blue Read 特性返回错误
问题描述
我只想读取我的服务特征的值。但是我收到了这个错误:[[ERROR:flutter/lib/ui/ui_dart_state.cc(148)]
未处理的异常:PlatformException(read_characteristic_error,未知原因,如果在上次读取完成之前调用 readCharacteristic 可能会发生。,null)]
我的代码:
discoverServices() async {
List<BluetoothService services = await device.discoverServices();
services.forEach((service) async {
// do something with service
if (service.uuid.toString() == SERVICE_UUID) {
var characteristics = await service.characteristics;
for(BluetoothCharacteristic c in characteristics) {
//if (characteristic.uuid.toString() == CHARACTERISTIC_UUID) {
targetCharacteristic = c;
List<int value = await c.read();
print(value);
//}
};
}
}); }
知道这里发生了什么吗?谢谢!!
解决方案
- 我会确保您的 GATT 服务正在通过您的服务具有所有唯一的 UUIDS。
我会给下面的代码一个镜头。到目前为止,我已经使用 Flutter Blue 完成了十几个应用程序,我看到的唯一问题是与读取和响应有关。
discoverServices() async { List<BluetoothService> services; BluetoothService myImportantService; List<BluetoothCharacteristic> characteristics; BluetoothCharacteristic myImportantCharacteristic; //Get your services from your device. services = await device.discoverServices(); //Find the service we are looking for. for(BluetoothService s in services) { //Would recommend to convert all to lowercase if comparing. if(s.uuid.toString().toLowerCase() == SERVICE_UUID) myImportantService = s; } //Get this services characteristics. characteristics = myImportantService.characteristics; //Find the characteristic we are looking for. for(BluetoothCharacteristic c in characteristics) { //Would recommend to convert all to lowercase if comparing. if(c.uuid.toString().toLowerCase() == CHARACTERISTIC_UUID) myImportantCharacteristic = c; } List<int> data = await myImportantCharacteristic.read(); }
推荐阅读
- c++ - 错误:表达式不能用作函数 - operator()
- java - 如何在 Talend Java 中将带小数的字符串转换为 BigDecimal 并保留小数?
- php - 路由不返回索引页面
- python - 迁移 Django-Oscar-Accounts 会出现错误消息
- ruby-on-rails - 当不在括号内时,Ruby 三元条件似乎被忽略了?
- azure - 部署 Azure 容器实例失败:OsVersionNotSupported
- c# - 使用 XmlSerializer C# 将数据从 XML 加载到 Class
- c# - GeckoFX 打印 PDF C#
- firebase - 如何为 Firestore 数据库的 Cloud Functions 中的每个集合插入、更新添加时间戳
- c++ - 我的 QList 类列表在这里存储了什么