connect method

Future<void> connect()

Implementation

Future<void> connect() async {
  final String? path = selectedPath;
  if (path == null) {
    error = 'No serial port selected.';
    _notify();
    return;
  }

  if (_subscription != null) {
    await disconnect();
  }
  loading = true;
  error = null;
  _notify();
  try {
    final Stream<Esp32Event> events = _api.connect(path);
    _subscription = events.listen(
      _onEvent,
      onError: (Object e) {
        error = 'Connection failed: $e';
        connected = false;
        loading = false;
        _notify();
      },
      onDone: () {
        connected = false;
        loading = false;
        _notify();
      },
    );
  } catch (e) {
    error = 'Could not open $path: $e';
  } finally {
    loading = false;
    _notify();
  }
}