I'm having a native Android App which is a RfidScanner. It works fine. But when I import the Kotlin files as a Cordova plugin, it only works when the hardware is connected to the debugger, so when I'm debugging. Could this be a thread issue because the timings are different? Again, it doesn't happen in native Android.
inputStream.read returns 1 without debugger
inputStream.read returns 13 with debugger (=correct)
The documentation explicitly mentions using the ExecutorService
for running background threads.
fun start() {
_isRunning = true
cordovaInterface.threadPool.submit {
try {
Log.d("JIM", "executorService.submit ${Thread.currentThread().name}")
while (_isRunning) {
Thread.sleep(100)
Log.d("JIM", "Thread.sleep(100) inside")
val buffer = ByteArray(256)
try {
val available = inputStream.read(buffer, 0, buffer.size)
Log.d("JIM", "inputStream.read(buffer")
if (available > 0) {
val data = ByteArray(available)
System.arraycopy(buffer, 0, data, 0, available)
Log.d("JIM", "available = $available > data = ${data.size}")
process(data)
}
} catch (e: Exception) {
Log.d("JIM", "Exception")
e.printStackTrace()
}
}
} catch (e: Exception) {
Log.d("JIM", "Exception")
e.printStackTrace()
}
}
Log.d("JIM", "Thread.sleep(100) before send ${Thread.currentThread().name}")
send(_scanCommand)
}
Without debugger:
2021-01-11 18:39:18.125 8836-8836/nl.rfidscanner D/JIM: Thread.sleep(100) before send main
2021-01-11 18:39:18.125 8836-8899/nl.rfidscanner D/JIM: executorService.submit pool-1-thread-1
2021-01-11 18:39:18.125 8836-8836/nl.rfidscanner D/JIM: send [B@a8fa829
2021-01-11 18:39:18.225 8836-8899/nl.rfidscanner D/JIM: Thread.sleep(100) inside
2021-01-11 18:39:18.226 8836-8899/nl.rfidscanner D/JIM: inputStream.read(buffer
2021-01-11 18:39:18.226 8836-8899/nl.rfidscanner D/JIM: available = 1 > data = 1
2021-01-11 18:39:18.226 8836-8899/nl.rfidscanner D/JIM: data != null
2021-01-11 18:39:18.226 8836-8899/nl.rfidscanner D/JIM: byte data size = 1
2021-01-11 18:39:18.226 8836-8899/nl.rfidscanner D/JIM: send [B@a8fa829
2021-01-11 18:39:18.327 8836-8899/nl.rfidscanner D/JIM: Thread.sleep(100) inside
2021-01-11 18:39:19.353 8836-8836/nl.rfidscanner D/JIM: 4. onStopScan
With debugger (available = 13 and this is correct):
2021-01-11 18:39:54.185 9013-9013/nl.rfidscanner D/JIM: Thread.sleep(100) before send main
2021-01-11 18:39:54.185 9013-9084/nl.rfidscanner D/JIM: executorService.submit pool-1-thread-1
2021-01-11 18:39:55.262 9013-9013/nl.rfidscanner D/JIM: send [B@3f1554d
2021-01-11 18:39:57.009 9013-9084/nl.rfidscanner D/JIM: Thread.sleep(100) inside
2021-01-11 18:39:57.009 9013-9084/nl.rfidscanner D/JIM: inputStream.read(buffer
2021-01-11 18:39:59.060 9013-9084/nl.rfidscanner D/JIM: available = 13 > data = 13
2021-01-11 18:39:59.723 9013-9084/nl.rfidscanner D/JIM: data != null
2021-01-11 18:39:59.723 9013-9084/nl.rfidscanner D/JIM: byte data size = 13
2021-01-11 18:40:00.096 9013-9084/nl.rfidscanner D/JIM: byte data size = 13
2021-01-11 18:40:00.532 9013-9084/nl.rfidscanner D/JIM: byte != 0.toByte()
question from:
https://stackoverflow.com/questions/65672104/inputstream-read-returns-1-when-not-attached-to-debugger-in-cordova-plugin 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…