diff --git a/app/src/main/java/com/example/hpostesting/domain/di/AppModule.kt b/app/src/main/java/com/example/hpostesting/domain/di/AppModule.kt index a84da0a..d7babfd 100644 --- a/app/src/main/java/com/example/hpostesting/domain/di/AppModule.kt +++ b/app/src/main/java/com/example/hpostesting/domain/di/AppModule.kt @@ -22,6 +22,8 @@ import com.example.hpostesting.domain.LogFileManager import com.example.hpostesting.domain.LogFileManagerImpl import com.example.hpostesting.domain.SaveRawData import com.example.hpostesting.domain.SaveRawDataTest +import com.example.hpostesting.presentation.UsbServiceListener +import com.example.hpostesting.presentation.UsbServiceListenerImpl import com.example.hpostesting.util.PropertyProviderImpl import dagger.Module import dagger.Provides @@ -179,4 +181,10 @@ object AppModule { fun provideLocalFileDataSource(): LocalFileDataSource { return LocalFileDataSourceImpl() } + + @Provides + @Singleton + fun provideUsbServiceListener(context: Context): UsbServiceListener { + return UsbServiceListenerImpl(context) + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/hpostesting/presentation/UsbServiceListenerImpl.kt b/app/src/main/java/com/example/hpostesting/presentation/UsbServiceListenerImpl.kt new file mode 100644 index 0000000..6ffd14c --- /dev/null +++ b/app/src/main/java/com/example/hpostesting/presentation/UsbServiceListenerImpl.kt @@ -0,0 +1,26 @@ +package com.example.hpostesting.presentation + +import android.content.Context +import android.util.Log +import android.widget.Toast + +class UsbServiceListenerImpl(private val context: Context): UsbServiceListener { + override fun onUsbRead(data: ByteArray?) { + if (data != null) { + val receivedData = String(data) + logData(receivedData) + } + } + + override fun onUsbError(e: Exception?) { + showToast("USB Error: ${e?.message}") + } + + private fun showToast(message: String) { + Toast.makeText(context, message, Toast.LENGTH_SHORT).show() + } + + private fun logData(data: String) { + Log.d("UsbServiceListener", "Received data from USB: $data") + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/hpostesting/presentation/testRight/UsbService.kt b/app/src/main/java/com/example/hpostesting/presentation/testRight/UsbService.kt index cf240ab..16cf5d5 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/testRight/UsbService.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/testRight/UsbService.kt @@ -33,32 +33,53 @@ class UsbService : Service() { var bus: UsbServiceListener? = null fun connect(driver: UsbSerialDriver, connection: UsbDeviceConnection) { - mPort = driver.ports[0] // Most devices have just one port (port 0) - mPort.open(connection) - mPort.setParameters(9600, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE) - isUsbConnected = true - Log.d(TAG, "My Usb Connected ${mPort.driver}") + try { + mPort = driver.ports[0] + mPort.open(connection) - val usbIoManager = SerialInputOutputManager(mPort, - object : SerialInputOutputManager.Listener { - override fun onNewData(data: ByteArray?) { - listener?.onUsbRead(data) - } + if (mPort.device.vendorId == 6790 && mPort.device.productId == 29987) + mPort.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE) + else + mPort.setParameters(9600, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE) - override fun onRunError(e: Exception?) { - Log.e(TAG, "onRunError() called inside eventDrivenWrite()") - listener?.onUsbError(e) - } + isUsbConnected = true + Log.d(TAG, "Usb Connected ${mPort.driver}") - }) - usbIoManager.start(); + val usbIoManager = SerialInputOutputManager(mPort, + object : SerialInputOutputManager.Listener { + override fun onNewData(data: ByteArray?) { + listener?.onUsbRead(data) + } + + override fun onRunError(e: Exception?) { + Log.e(TAG, "onRunError() called") + listener?.onUsbError(e) + } + }) + + 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() { - if (isUsbConnected) { - mPort.close() - isUsbConnected = false; - Log.d(TAG, "My Usb disconnected:: ${mPort.driver}") + try { + if (isUsbConnected) { + mPort.close() + isUsbConnected = false + 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) } }