merge
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user