在这里,我试图从esp32驱动程序中读取一个特征,我得到了这些问题. "如果之前调用了ReadCharacteristic,则可能会发生PlatformException(READ_CHARACTIVE_ERROR,未知原因 上次读取完成,NULL,NULL)". 下面我附上了用于读取特征的代码.

 Future<void> readData async{
  BluetoothService? DataService =
  BLEComm().getService(DeviceServiceUUIDs.Service);
  if (null != DataService) {
  await Future.delayed(Duration(milliseconds:50),() async{
  BluetoothCharacteristic? DurationCharacteristic =
  BLEComm().getCharacteristic(
  DeviceServiceUUIDs.duration,
  DataService);
  List<int>? DurationResponse = await BLEComm()
      .readCharacteristic(DurationCharacteristic);
  String Duration = String.fromCharCodes(DurationResponse!);
  var Duration_Data = double.parse(Duration);
  });

在这里,我列出了我习惯于从上面的代码中读取特征的低级函数.

/// Get a specific service from the list of services.
  BluetoothService? getService(String uuid) {
    for (BluetoothService? service in _deviceServices) {
      if (service?.uuid.toString() == uuid) {
        return service;
      }
    }
    return null;
  }


 /// Get Characteristic object from a service
  BluetoothCharacteristic? getCharacteristic(
      String uuid, BluetoothService bleService) {
    var characteristics = bleService.characteristics;
    for (BluetoothCharacteristic characteristic in characteristics) {
      if (characteristic.uuid.toString() == uuid) {
        return characteristic;
      }
    }
    return null;
  }

  /// read value to a characteristic
  /// Throws Exceptions on Non readable characteristic and general exceptions.
  Future<List<int>?> readCharacteristic(
      BluetoothCharacteristic? characteristic) async {
    List<int>? value;
    try {
      if (null != characteristic && !characteristic.properties.read) {
        throw ("characteristic ${characteristic.uuid.toString()} is not readable");
      }
      value = await characteristic?.read();
    } catch (ex) {
      throw Exception(ex);
    }
    return value;
  }
}

推荐答案

在读取特征值时,您似乎遇到了"PlatformException"错误.发生此错误的原因可能是您试图在上一个读取操作完成之前读取特征.要解决这个问题,您可以实现一个简单的锁定机制,以确保您不会在前一个读取操作完成之前调用readCharacteristic.

修改BLEComm类以包括一个lock变量并更新readCharacteristic函数:

class BLEComm {
  // Add a lock variable to the class
  bool _isReading = false;

  // Other functions and variables in the class ...

  /// read value to a characteristic
  /// Throws Exceptions on Non-readable characteristic and general exceptions.
  Future<List<int>?> readCharacteristic(
      BluetoothCharacteristic? characteristic) async {
    List<int>? value;

    // If currently reading, wait for the previous read operation to complete
    while (_isReading) {
      await Future.delayed(Duration(milliseconds: 10));
    }

    try {
      if (null != characteristic && !characteristic.properties.read) {
        throw ("characteristic ${characteristic.uuid.toString()} is not readable");
      }

      // Set the lock to true before starting the read operation
      _isReading = true;
      value = await characteristic?.read();

      // Release the lock after the read operation is complete
      _isReading = false;

    } catch (ex) {
      // Release the lock in case of an exception
      _isReading = false;
      throw Exception(ex);
    }

    return value;
  }
}

通过实现此锁定机制,您可以确保readCharacteristic将在启动新的读取操作之前等待前一个读取操作完成.这应该可以解决您遇到的"PlatformException"错误.

Flutter相关问答推荐

如何使用Firebase Firestore来获取外部集合中的文档子集合中的文档

如何在WidgetRef引用外部的函数中使用Riverpod刷新数据

audioQuery不会等待判断存储权限

如何将Will POP示波器转换为POP数据抖动的POP示波器

就像Flutter 中的头文件一样?

Flutter 块消费者仅对特定的状态属性更改做出react

从Firestore流式传输单个文档还是整个集合更好?

Flutter记录返回类型问题

在 Flutter 中从 FireStore 获取数据并编译它们需要太多时间

在flutter中从图库或相机中 Select 图像的方法

Flutter中如何实现带有变化图标的浮动操作按钮?

使用堆栈的自定义按钮

如何在 flutter 中使用带有有效负载数据的重定向来执行 firebase 推送通知.?

在提供程序包更改通知程序中构建倒计时时钟

如何将 chrome web 模拟器添加到 vs code

如何将 void 函数放入单独的 dart 文件的单独页面中

如何在 flutter 中制作这种类型的扩展瓦片

从物理设备 Flutter 中移除 USB 后启动画面不可见?

Flutter 中父容器顶部的 Gridview 间距额外区域

解密 Modulr 安全令牌