open method

  1. @override
Future<bool> open()

Opens the serial port for reading and writing.

Implementation

@override
Future<bool> open() async {
  if (_usbPort != null) {
    _usbPort!.close();
    _usbPort = null;
  }

  final androidUsbDevice =
      (await UsbSerial.listDevices()).firstWhere((d) => d.deviceName == name);
  _usbPort = await androidUsbDevice.create();
  if (_usbPort == null) {
    return false;
  }
  var opened = await _usbPort!.open();
  if (!opened) {
    _usbPort!.close();
    _usbPort = null;
    return false;
  }

  _usbPort!.setFlowControl(flowControl.intValue);
  _usbPort!.setPortParameters(
    baudRate.intValue,
    dataBits.intValue,
    stopBits.intValue,
    parity.intValue,
  );
  return true;
}