add exception handling
This commit is contained in:
@@ -33,7 +33,8 @@ class UsbService : Service() {
|
|||||||
var bus: UsbServiceListener? = null
|
var bus: UsbServiceListener? = null
|
||||||
|
|
||||||
fun connect(driver: UsbSerialDriver, connection: UsbDeviceConnection) {
|
fun connect(driver: UsbSerialDriver, connection: UsbDeviceConnection) {
|
||||||
mPort = driver.ports[0] // Most devices have just one port (port 0)
|
try {
|
||||||
|
mPort = driver.ports[0]
|
||||||
mPort.open(connection)
|
mPort.open(connection)
|
||||||
mPort.setParameters(9600, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE)
|
mPort.setParameters(9600, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE)
|
||||||
isUsbConnected = true
|
isUsbConnected = true
|
||||||
@@ -49,16 +50,31 @@ class UsbService : Service() {
|
|||||||
Log.e(TAG, "onRunError() called inside eventDrivenWrite()")
|
Log.e(TAG, "onRunError() called inside eventDrivenWrite()")
|
||||||
listener?.onUsbError(e)
|
listener?.onUsbError(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
usbIoManager.start();
|
|
||||||
|
usbIoManager.start()
|
||||||
|
} catch (ioException: IOException) {
|
||||||
|
Log.e(TAG, "IOException during USB connection: ${ioException.message}", ioException)
|
||||||
|
listener?.onUsbError(ioException)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e(TAG, "Error connecting USB: ${e.message}", e)
|
||||||
|
listener?.onUsbError(e)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun disconnect() {
|
fun disconnect() {
|
||||||
|
try {
|
||||||
if (isUsbConnected) {
|
if (isUsbConnected) {
|
||||||
mPort.close()
|
mPort.close()
|
||||||
isUsbConnected = false;
|
isUsbConnected = false
|
||||||
Log.d(TAG, "My Usb disconnected:: ${mPort.driver}")
|
Log.d(TAG, "USB Port closed successfully:: ${mPort.driver}")
|
||||||
|
} else {
|
||||||
|
Log.d(TAG, "USB Port is not connected")
|
||||||
|
}
|
||||||
|
} catch (e: IOException) {
|
||||||
|
Log.e(TAG, "Error closing USB Port: ${e.message}", e)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e(TAG, "An unexpected error occurred: ${e.message}", e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user