waitForData method

Future<Uint8List> waitForData({
  1. Duration? timeout,
})

Waits for any data from the device.

Implementation

Future<Uint8List> waitForData({Duration? timeout}) async {
  StreamSubscription<Uint8List>? subscription;
  Completer<Uint8List> completer = Completer();
  _log.fine("Waiting for any data");
  try {
    subscription =
        dataResponse.timeout(timeout ?? defaultTimeout).listen((event) {
      _log.fine("Received misc data: ${event.length} bytes");
      subscription?.cancel();
      completer.complete(event);
    });
  } on TimeoutException {
    _log.warning("Timeout while waiting for data.");
    if (subscription != null) {
      subscription.cancel();
    }
    if (!completer.isCompleted) {
      completer.completeError(const BrilliantBluetoothException(
          "Timeout while waiting for data"));
    }
    return Future.error(
        const BrilliantBluetoothException("Timeout while waiting for data"));
  }
  return completer.future;
}