sendString method

Future<String?> sendString(
  1. String string, {
  2. bool awaitResponse = true,
  3. bool log = true,
})

Implementation

Future<String?> sendString(
  String string, {
  bool awaitResponse = true,
  bool log = true,
}) async {
  try {
    if (log) {
      _log.info(() => "Sending string: $string");
    }

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

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

    await _txChannel!.write(utf8.encode(string), withoutResponse: true);

    if (awaitResponse == false) {
      return null;
    }

    final response = await _rxChannel!.onValueReceived
        .timeout(const Duration(seconds: 10))
        .first;

    return utf8.decode(response);
  } catch (error) {
    _log.warning("Couldn't send string. $error");
    return Future.error(BrilliantBluetoothException(error.toString()));
  }
}