Updated USbListeners

This commit is contained in:
vsuryakumar
2023-01-25 11:27:53 +05:30
parent fe98f0e65d
commit 0b99ef9a18
2 changed files with 19 additions and 38 deletions

View File

@@ -1,9 +1,6 @@
package com.example.refactoredapp.presentation package com.example.refactoredapp.presentation
interface UsbServiceListener { interface UsbServiceListener {
fun onUsbConnect()
fun onUsbConnectError(e: Exception?)
fun onUsbRead(data: ByteArray?) fun onUsbRead(data: ByteArray?)
fun onUsbRead() fun onUsbError(e: Exception?)
fun onUsbIoError(e: Exception?)
} }

View File

@@ -7,7 +7,7 @@ import android.os.Binder
import android.os.IBinder import android.os.IBinder
import android.util.Log import android.util.Log
import com.example.refactoredapp.data.Constants 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.example.refactoredapp.presentation.UsbServiceListener
import com.hoho.android.usbserial.driver.UsbSerialDriver import com.hoho.android.usbserial.driver.UsbSerialDriver
import com.hoho.android.usbserial.driver.UsbSerialPort import com.hoho.android.usbserial.driver.UsbSerialPort
@@ -19,13 +19,8 @@ class UsbService : Service() {
// Binder to be given to clients // Binder to be given to clients
private val binder = UsbServiceBinder() private val binder = UsbServiceBinder()
private lateinit var mPort: UsbSerialPort private lateinit var mPort: UsbSerialPort
private var listener: UsbServiceListener? = null
private var isUsbConnected = false private var isUsbConnected = false
private val TAG = "UsbService" private val TAG = "UsbService"
inner class UsbServiceBinder : Binder() { inner class UsbServiceBinder : Binder() {
@@ -39,7 +34,6 @@ class UsbService : Service() {
} }
fun connect(driver: UsbSerialDriver, connection: UsbDeviceConnection) { fun connect(driver: UsbSerialDriver, connection: UsbDeviceConnection) {
mPort = driver.ports[0] // Most devices have just one port (port 0) mPort = driver.ports[0] // Most devices have just one port (port 0)
mPort.open(connection) mPort.open(connection)
mPort.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE) mPort.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE)
@@ -56,25 +50,15 @@ class UsbService : Service() {
} }
} }
fun addListener(listener: UsbServiceListener){ fun eventDrivenWrite(command: TestRightCommands, listener: UsbServiceListener) {
this.listener = listener
if(isUsbConnected)
listener.onUsbConnect()
}
fun removeListener(){
this.listener = null
}
fun eventDrivenWrite(command: TestRightCommands) {
val usbIoManager = SerialInputOutputManager(mPort, val usbIoManager = SerialInputOutputManager(mPort,
object : SerialInputOutputManager.Listener{ object : SerialInputOutputManager.Listener{
override fun onNewData(data: ByteArray?) { override fun onNewData(data: ByteArray?) {
listener?.onUsbRead(data) listener.onUsbRead(data)
} }
override fun onRunError(e: Exception?) { override fun onRunError(e: Exception?) {
Log.e(TAG, "onRunError() called inside eventDrivenWrite()") Log.e(TAG, "onRunError() called inside eventDrivenWrite()")
listener.onUsbError(e)
} }
}) })
@@ -83,23 +67,23 @@ class UsbService : Service() {
try { try {
mPort.write(command.command.toByteArray(), Constants.WRITE_TIMEOUT_MILLIS) mPort.write(command.command.toByteArray(), Constants.WRITE_TIMEOUT_MILLIS)
} catch (e: IOException) { } catch (e: IOException) {
listener?.onUsbIoError(e) listener.onUsbError(e)
} }
} }
fun write(command: TestRightCommands){ // fun write(command: TestRightCommands){
mPort.write(command.command.toByteArray(), Constants.WRITE_TIMEOUT_MILLIS) // mPort.write(command.command.toByteArray(), Constants.WRITE_TIMEOUT_MILLIS)
} // }
fun read(){ // fun read(){
val byteArray = ByteArray(3700) // val byteArray = ByteArray(3700)
val len = mPort.read(byteArray, Constants.READ_TIMEOUT_MILLIS) // val len = mPort.read(byteArray, Constants.READ_TIMEOUT_MILLIS)
//
Log.d(TAG, "length is $len") // Log.d(TAG, "length is $len")
val byteArrayCropped = ByteArray(len) // val byteArrayCropped = ByteArray(len)
System.arraycopy(byteArray, 0, byteArrayCropped, 0, len) // System.arraycopy(byteArray, 0, byteArrayCropped, 0, len)
//
listener?.onUsbRead(byteArrayCropped) // listener?.onUsbRead(byteArrayCropped)
} // }
} }