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.LogFileManagerImpl
|
||||||
import com.example.hpostesting.domain.SaveRawData
|
import com.example.hpostesting.domain.SaveRawData
|
||||||
import com.example.hpostesting.domain.SaveRawDataTest
|
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 com.example.hpostesting.util.PropertyProviderImpl
|
||||||
import dagger.Module
|
import dagger.Module
|
||||||
import dagger.Provides
|
import dagger.Provides
|
||||||
@@ -179,4 +181,10 @@ object AppModule {
|
|||||||
fun provideLocalFileDataSource(): LocalFileDataSource {
|
fun provideLocalFileDataSource(): LocalFileDataSource {
|
||||||
return LocalFileDataSourceImpl()
|
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
|
var bus: UsbServiceListener? = null
|
||||||
|
|
||||||
fun connect(driver: UsbSerialDriver, connection: UsbDeviceConnection) {
|
fun connect(driver: UsbSerialDriver, connection: UsbDeviceConnection) {
|
||||||
mPort = driver.ports[0] // Most devices have just one port (port 0)
|
try {
|
||||||
mPort.open(connection)
|
mPort = driver.ports[0]
|
||||||
mPort.setParameters(9600, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE)
|
mPort.open(connection)
|
||||||
isUsbConnected = true
|
|
||||||
Log.d(TAG, "My Usb Connected ${mPort.driver}")
|
|
||||||
|
|
||||||
val usbIoManager = SerialInputOutputManager(mPort,
|
if (mPort.device.vendorId == 6790 && mPort.device.productId == 29987)
|
||||||
object : SerialInputOutputManager.Listener {
|
mPort.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE)
|
||||||
override fun onNewData(data: ByteArray?) {
|
else
|
||||||
listener?.onUsbRead(data)
|
mPort.setParameters(9600, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE)
|
||||||
}
|
|
||||||
|
|
||||||
override fun onRunError(e: Exception?) {
|
isUsbConnected = true
|
||||||
Log.e(TAG, "onRunError() called inside eventDrivenWrite()")
|
Log.d(TAG, "Usb Connected ${mPort.driver}")
|
||||||
listener?.onUsbError(e)
|
|
||||||
}
|
|
||||||
|
|
||||||
})
|
val usbIoManager = SerialInputOutputManager(mPort,
|
||||||
usbIoManager.start();
|
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() {
|
fun disconnect() {
|
||||||
if (isUsbConnected) {
|
try {
|
||||||
mPort.close()
|
if (isUsbConnected) {
|
||||||
isUsbConnected = false;
|
mPort.close()
|
||||||
Log.d(TAG, "My Usb disconnected:: ${mPort.driver}")
|
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