Improve USB connection handling by preventing connection attempts when already connected

This commit is contained in:
just_stuff_tm
2026-03-02 03:09:50 -05:00
committed by just-stuff-tm
parent 9a0572e8e4
commit 115689ad95
2 changed files with 93 additions and 66 deletions
@@ -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) val driver = UsbSerialProber.getDefaultProber().probeDevice(device)
if (driver == null) { if (driver == null) {
result.error("usb_driver_missing", "No USB serial driver for ${device.deviceName}", null) mainHandler.post {
return result.error(
} "usb_driver_missing",
"No USB serial driver for ${device.deviceName}",
val connection = usbManager.openDevice(device) null,
if (connection == null) { )
result.error( }
"usb_open_failed", return@execute
"UsbManager could not open ${device.deviceName}",
null,
)
return
}
val port = firstPort(driver)
if (port == null) {
connection.close()
result.error("usb_port_missing", "No USB serial port exposed by ${device.deviceName}", null)
return
}
port.open(connection)
port.setParameters(
baudRate,
8,
UsbSerialPort.STOPBITS_1,
UsbSerialPort.PARITY_NONE,
)
port.rts = false
port.dtr = true
usbConnection = connection
usbPort = port
connectedDeviceName = device.deviceName
ioManager =
SerialInputOutputManager(
port,
object : SerialInputOutputManager.Listener {
override fun onNewData(data: ByteArray) {
mainHandler.post {
eventSink?.success(data)
}
}
override fun onRunError(e: Exception) {
mainHandler.post {
eventSink?.error(
"usb_io_error",
e.message ?: "USB serial I/O error",
null,
)
}
closeUsbConnection()
}
},
).also { manager ->
manager.start()
} }
result.success(null) val connection = usbManager.openDevice(device)
} catch (error: Exception) { if (connection == null) {
closeUsbConnection() mainHandler.post {
result.error("usb_connect_failed", error.message, null) result.error(
"usb_open_failed",
"UsbManager could not open ${device.deviceName}",
null,
)
}
return@execute
}
val port = firstPort(driver)
if (port == null) {
connection.close()
mainHandler.post {
result.error(
"usb_port_missing",
"No USB serial port exposed by ${device.deviceName}",
null,
)
}
return@execute
}
port.open(connection)
port.setParameters(
baudRate,
8,
UsbSerialPort.STOPBITS_1,
UsbSerialPort.PARITY_NONE,
)
port.rts = false
port.dtr = true
usbConnection = connection
usbPort = port
connectedDeviceName = device.deviceName
ioManager =
SerialInputOutputManager(
port,
object : SerialInputOutputManager.Listener {
override fun onNewData(data: ByteArray) {
mainHandler.post {
eventSink?.success(data)
}
}
override fun onRunError(e: Exception) {
mainHandler.post {
eventSink?.error(
"usb_io_error",
e.message ?: "USB serial I/O error",
null,
)
}
closeUsbConnection()
}
},
).also { manager ->
manager.start()
}
mainHandler.post {
result.success(null)
}
} catch (error: Exception) {
closeUsbConnection()
mainHandler.post {
result.error("usb_connect_failed", error.message, null)
}
}
} }
} }
+7
View File
@@ -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(() {