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
interface UsbServiceListener {
fun onUsbConnect()
fun onUsbConnectError(e: Exception?)
fun onUsbRead(data: ByteArray?)
fun onUsbRead()
fun onUsbIoError(e: Exception?)
fun onUsbError(e: Exception?)
}

View File

@@ -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)
// }
}