Flutter Blue Read特性返回错误

时间:2019-12-22 17:24:43

标签: android flutter bluetooth

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

  

未处理的异常:PlatformException(read_characteristic_error,未知原因,如果在上一次读取完成之前调用readCharacteristic,则可能会发生。)

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

对这里发生的事情有任何想法吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

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