【问题标题】:Flutter Blue Read characteristic is returning errorFlutter Blue Read 特性返回错误
【发布时间】:2019-12-22 17:24:43
【问题描述】:

我只想读取我的服务特征的值。但我收到了这个错误: [[错误:flutter/lib/ui/ui_dart_state.cc(148)]

未处理的异常:PlatformException(read_characteristic_error, unknown reason, if readCharacteristic is called before last read finished., null)]

1

我的代码:

 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);
           //}
         };
       }
     });   }

code

知道这里发生了什么吗? 谢谢!!

【问题讨论】:

    标签: android flutter bluetooth


    【解决方案1】:
    1. 我会确保您的 GATT 服务正在通过您的服务具有所有唯一的 UUID。
    2. 我会试一下下面的代码。到目前为止,我已经使用 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();
      }
      

    【讨论】:

    • 感谢您的回答。代码很清楚但是我得到了同样的错误:Unhandled Exception: PlatformException(read_characteristic_error, unknown reason, may occur if readCharacteristic was called before last read finished., null)'. Any suggestion?
    • 封装这些函数的代码是什么?您是在“轮询”此功能还是任何其他读取? GATTServer 使用什么硬件/软件?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-24
    • 1970-01-01
    • 1970-01-01
    • 2020-10-16
    • 2021-01-28
    • 1970-01-01
    • 2019-03-21
    相关资源
    最近更新 更多