Improve USB connection handling by preventing connection attempts when already connected

chore/offband-rebrand
just_stuff_tm 7 months ago committed by just-stuff-tm
parent 9a0572e8e4
commit 115689ad95

@ -238,75 +238,95 @@ class MainActivity : FlutterActivity() {
baudRate: Int, baudRate: Int,
result: MethodChannel.Result, result: MethodChannel.Result,
) { ) {
try { usbIoExecutor.execute {
closeUsbConnection() try {
closeUsbConnection()
val driver = UsbSerialProber.getDefaultProber().probeDevice(device)
if (driver == null) { val driver = UsbSerialProber.getDefaultProber().probeDevice(device)
result.error("usb_driver_missing", "No USB serial driver for ${device.deviceName}", null) if (driver == null) {
return mainHandler.post {
} result.error(
"usb_driver_missing",
"No USB serial driver for ${device.deviceName}",
null,
)
}
return@execute
}
val connection = usbManager.openDevice(device) val connection = usbManager.openDevice(device)
if (connection == null) { if (connection == null) {
result.error( mainHandler.post {
"usb_open_failed", result.error(
"UsbManager could not open ${device.deviceName}", "usb_open_failed",
null, "UsbManager could not open ${device.deviceName}",
) null,
return )
} }
return@execute
}
val port = firstPort(driver) val port = firstPort(driver)
if (port == null) { if (port == null) {
connection.close() connection.close()
result.error("usb_port_missing", "No USB serial port exposed by ${device.deviceName}", null) mainHandler.post {
return result.error(
} "usb_port_missing",
"No USB serial port exposed by ${device.deviceName}",
null,
)
}
return@execute
}
port.open(connection) port.open(connection)
port.setParameters( port.setParameters(
baudRate, baudRate,
8, 8,
UsbSerialPort.STOPBITS_1, UsbSerialPort.STOPBITS_1,
UsbSerialPort.PARITY_NONE, UsbSerialPort.PARITY_NONE,
) )
port.rts = false port.rts = false
port.dtr = true port.dtr = true
usbConnection = connection usbConnection = connection
usbPort = port usbPort = port
connectedDeviceName = device.deviceName connectedDeviceName = device.deviceName
ioManager = ioManager =
SerialInputOutputManager( SerialInputOutputManager(
port, port,
object : SerialInputOutputManager.Listener { object : SerialInputOutputManager.Listener {
override fun onNewData(data: ByteArray) { override fun onNewData(data: ByteArray) {
mainHandler.post { mainHandler.post {
eventSink?.success(data) eventSink?.success(data)
}
} }
}
override fun onRunError(e: Exception) {
override fun onRunError(e: Exception) { mainHandler.post {
mainHandler.post { eventSink?.error(
eventSink?.error( "usb_io_error",
"usb_io_error", e.message ?: "USB serial I/O error",
e.message ?: "USB serial I/O error", null,
null, )
) }
closeUsbConnection()
} }
closeUsbConnection() },
} ).also { manager ->
}, manager.start()
).also { manager -> }
manager.start()
}
result.success(null) mainHandler.post {
} catch (error: Exception) { result.success(null)
closeUsbConnection() }
result.error("usb_connect_failed", error.message, null) } catch (error: Exception) {
closeUsbConnection()
mainHandler.post {
result.error("usb_connect_failed", error.message, null)
}
}
} }
} }

@ -425,6 +425,13 @@ class _UsbScreenState extends State<UsbScreen> {
if (selectedPort == null || selectedPort.isEmpty) { if (selectedPort == null || selectedPort.isEmpty) {
return; return;
} }
if (_connector.state != MeshCoreConnectionState.disconnected) {
setState(() {
_isConnecting = false;
_errorText = null;
});
return;
}
final rawPortName = normalizeUsbPortName(selectedPort); final rawPortName = normalizeUsbPortName(selectedPort);
setState(() { setState(() {

Loading…
Cancel
Save

Powered by TurnKey Linux.