sendData method

Future<Uint8List?> sendData(
  1. Uint8List data, {
  2. bool awaitResponse = false,
  3. Duration? timeout,
})

Sends data to the device.

Implementation

Future<Uint8List?> sendData(Uint8List data,
    {bool awaitResponse = false, Duration? timeout}) async {
  try {
    _log.info("Sending ${data.length} bytes of plain data");
    _log.fine(data);

    if (state != BrilliantConnectionState.connected) {
      throw ("Device is not connected");
    }

    if (data.length > maxDataLength!) {
      throw ("Payload exceeds allowed length of $maxDataLength");
    }

    var finalData = data.toList()..insert(0, _frameDataPrefix);

    await _txChannel!.write(finalData, withoutResponse: true);

    if (!awaitResponse) {
      return null;
    }

    return await waitForData(timeout: timeout);
  } catch (error) {
    _log.warning("Couldn't send data. $error");
    return Future.error(BrilliantBluetoothException(error.toString()));
  }
}