|
|
|
|
@ -238,30 +238,45 @@ class MainActivity : FlutterActivity() {
|
|
|
|
|
baudRate: Int,
|
|
|
|
|
result: MethodChannel.Result,
|
|
|
|
|
) {
|
|
|
|
|
usbIoExecutor.execute {
|
|
|
|
|
try {
|
|
|
|
|
closeUsbConnection()
|
|
|
|
|
|
|
|
|
|
val driver = UsbSerialProber.getDefaultProber().probeDevice(device)
|
|
|
|
|
if (driver == null) {
|
|
|
|
|
result.error("usb_driver_missing", "No USB serial driver for ${device.deviceName}", null)
|
|
|
|
|
return
|
|
|
|
|
mainHandler.post {
|
|
|
|
|
result.error(
|
|
|
|
|
"usb_driver_missing",
|
|
|
|
|
"No USB serial driver for ${device.deviceName}",
|
|
|
|
|
null,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
return@execute
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val connection = usbManager.openDevice(device)
|
|
|
|
|
if (connection == null) {
|
|
|
|
|
mainHandler.post {
|
|
|
|
|
result.error(
|
|
|
|
|
"usb_open_failed",
|
|
|
|
|
"UsbManager could not open ${device.deviceName}",
|
|
|
|
|
null,
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
return@execute
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val port = firstPort(driver)
|
|
|
|
|
if (port == null) {
|
|
|
|
|
connection.close()
|
|
|
|
|
result.error("usb_port_missing", "No USB serial port exposed by ${device.deviceName}", null)
|
|
|
|
|
return
|
|
|
|
|
mainHandler.post {
|
|
|
|
|
result.error(
|
|
|
|
|
"usb_port_missing",
|
|
|
|
|
"No USB serial port exposed by ${device.deviceName}",
|
|
|
|
|
null,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
return@execute
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
port.open(connection)
|
|
|
|
|
@ -303,12 +318,17 @@ class MainActivity : FlutterActivity() {
|
|
|
|
|
manager.start()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mainHandler.post {
|
|
|
|
|
result.success(null)
|
|
|
|
|
}
|
|
|
|
|
} catch (error: Exception) {
|
|
|
|
|
closeUsbConnection()
|
|
|
|
|
mainHandler.post {
|
|
|
|
|
result.error("usb_connect_failed", error.message, null)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun firstPort(driver: UsbSerialDriver): UsbSerialPort? {
|
|
|
|
|
return driver.ports.firstOrNull()
|
|
|
|
|
|