writeValueWithResponse method

Future<void> writeValueWithResponse(
  1. Uint8List data
)

Write a new value to the characteristic with waiting for a response from the Bluetooth device.

This means that the the Future will only return if the device has acknowledged that it has received the data. (or a timeout is reached). It's a bit like TCP.

  • May throw NotSupportedError if the operation is not allowed. Check properties to see if it is supported.

  • May throw NetworkError if the device is not connected or if there is an error with the communication.

  • May throw StateError if the characteristic is null.

ignore: deprecated_member_use_from_same_package

See: writeValueWithoutResponse.

Implementation

Future<void> writeValueWithResponse(final Uint8List data) async {
  try {
    if (_characteristic.hasWriteValueWithResponse()) {
      return _characteristic.writeValueWithResponse(data);
    }
    webBluetoothLogger.info(
        "WriteValueWithResponse not supported in this browser. "
        "Using writeValue instead",
        null,
        StackTrace.current);
    // ignore: deprecated_member_use_from_same_package
    return _characteristic.writeValue(data);
  } catch (e) {
    final error = e.toString().trim();
    if (error.startsWith("NotSupportedError")) {
      throw NotSupportedError(uuid);
    } else if (error.startsWith("NetworkError")) {
      throw NetworkError.withUUid(uuid);
    } else if (error.startsWith("InvalidStateError")) {
      throw StateError("Characteristic is null");
    }
    rethrow;
  }
}