2

here I'm trying to read a characteristics from esp32 driver and I'm getting these issues. "PlatformException(read_characteristic_error,unknown reason,may occur if readCharacteristic was called before last read finished, null, null)". here below I'm attaching the code used for reading characteristics.

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

here I'm listing low-level functions that I'm used to reading characteristics from the above code.

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

Martin Ille
  • 6,747
  • 9
  • 44
  • 63
Nns_ninteyFIve
  • 439
  • 4
  • 12
  • @Martin Ille can i do the same thing for write issues like " PlatformException(write_characteristic_error,writecharacteristic failed,null,null)" . – Nns_ninteyFIve Apr 02 '23 at 10:50

0 Answers0