From 0b99ef9a18c45641af42f5145fd2fbee4181b242 Mon Sep 17 00:00:00 2001 From: vsuryakumar Date: Wed, 25 Jan 2023 11:27:53 +0530 Subject: [PATCH] Updated USbListeners --- .../presentation/UsbServiceListener.kt | 5 +- .../presentation/testRight/UsbService.kt | 52 +++++++------------ 2 files changed, 19 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/com/example/refactoredapp/presentation/UsbServiceListener.kt b/app/src/main/java/com/example/refactoredapp/presentation/UsbServiceListener.kt index 20c5c8d..15ea960 100644 --- a/app/src/main/java/com/example/refactoredapp/presentation/UsbServiceListener.kt +++ b/app/src/main/java/com/example/refactoredapp/presentation/UsbServiceListener.kt @@ -1,9 +1,6 @@ package com.example.refactoredapp.presentation interface UsbServiceListener { - fun onUsbConnect() - fun onUsbConnectError(e: Exception?) fun onUsbRead(data: ByteArray?) - fun onUsbRead() - fun onUsbIoError(e: Exception?) + fun onUsbError(e: Exception?) } \ No newline at end of file diff --git a/app/src/main/java/com/example/refactoredapp/presentation/testRight/UsbService.kt b/app/src/main/java/com/example/refactoredapp/presentation/testRight/UsbService.kt index 2ab17c1..f6a8237 100644 --- a/app/src/main/java/com/example/refactoredapp/presentation/testRight/UsbService.kt +++ b/app/src/main/java/com/example/refactoredapp/presentation/testRight/UsbService.kt @@ -7,7 +7,7 @@ import android.os.Binder import android.os.IBinder import android.util.Log import com.example.refactoredapp.data.Constants -import com.example.refactoredapp.data.model.TestRightCommands +import com.example.refactoredapp.data.constant.TestRightCommands import com.example.refactoredapp.presentation.UsbServiceListener import com.hoho.android.usbserial.driver.UsbSerialDriver import com.hoho.android.usbserial.driver.UsbSerialPort @@ -19,13 +19,8 @@ class UsbService : Service() { // Binder to be given to clients private val binder = UsbServiceBinder() - private lateinit var mPort: UsbSerialPort - - private var listener: UsbServiceListener? = null - private var isUsbConnected = false - private val TAG = "UsbService" inner class UsbServiceBinder : Binder() { @@ -39,7 +34,6 @@ class UsbService : Service() { } fun connect(driver: UsbSerialDriver, connection: UsbDeviceConnection) { - mPort = driver.ports[0] // Most devices have just one port (port 0) mPort.open(connection) mPort.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE) @@ -56,25 +50,15 @@ class UsbService : Service() { } } - fun addListener(listener: UsbServiceListener){ - this.listener = listener - if(isUsbConnected) - listener.onUsbConnect() - } - - fun removeListener(){ - this.listener = null - } - - fun eventDrivenWrite(command: TestRightCommands) { + fun eventDrivenWrite(command: TestRightCommands, listener: UsbServiceListener) { val usbIoManager = SerialInputOutputManager(mPort, object : SerialInputOutputManager.Listener{ override fun onNewData(data: ByteArray?) { - listener?.onUsbRead(data) + listener.onUsbRead(data) } - override fun onRunError(e: Exception?) { Log.e(TAG, "onRunError() called inside eventDrivenWrite()") + listener.onUsbError(e) } }) @@ -83,23 +67,23 @@ class UsbService : Service() { try { mPort.write(command.command.toByteArray(), Constants.WRITE_TIMEOUT_MILLIS) } catch (e: IOException) { - listener?.onUsbIoError(e) + listener.onUsbError(e) } } - fun write(command: TestRightCommands){ - mPort.write(command.command.toByteArray(), Constants.WRITE_TIMEOUT_MILLIS) - } +// fun write(command: TestRightCommands){ +// mPort.write(command.command.toByteArray(), Constants.WRITE_TIMEOUT_MILLIS) +// } - fun read(){ - val byteArray = ByteArray(3700) - val len = mPort.read(byteArray, Constants.READ_TIMEOUT_MILLIS) - - Log.d(TAG, "length is $len") - val byteArrayCropped = ByteArray(len) - System.arraycopy(byteArray, 0, byteArrayCropped, 0, len) - - listener?.onUsbRead(byteArrayCropped) - } +// fun read(){ +// val byteArray = ByteArray(3700) +// val len = mPort.read(byteArray, Constants.READ_TIMEOUT_MILLIS) +// +// Log.d(TAG, "length is $len") +// val byteArrayCropped = ByteArray(len) +// System.arraycopy(byteArray, 0, byteArrayCropped, 0, len) +// +// listener?.onUsbRead(byteArrayCropped) +// } } \ No newline at end of file