首页 > 解决方案 > Flutter Blue Read 特性返回错误

问题描述

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

未处理的异常:PlatformException(read_characteristic_error,未知原因,如果在上次读取完成之前调用 readCharacteristic 可能会发生。,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);
           //}
         };
       }
     });   }

代码

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

标签: androidflutterbluetooth

解决方案


  1. 我会确保您的 GATT 服务正在通过您的服务具有所有唯一的 UUIDS。
  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();
    }
    

推荐阅读