From fc5902c528f9ff747ffffd7bfca242ca254de421 Mon Sep 17 00:00:00 2001 From: Gideon Okuro Date: Tue, 3 Dec 2024 14:55:15 +0300 Subject: [PATCH 1/4] explicitly handle the case when read() returns -1 --- .../remote/network/BluetoothChatService.kt | 45 ++++++++++++++----- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/app/src/main/kotlin/io/treehouses/remote/network/BluetoothChatService.kt b/app/src/main/kotlin/io/treehouses/remote/network/BluetoothChatService.kt index 7588291c4..6cf04e98c 100644 --- a/app/src/main/kotlin/io/treehouses/remote/network/BluetoothChatService.kt +++ b/app/src/main/kotlin/io/treehouses/remote/network/BluetoothChatService.kt @@ -147,12 +147,19 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a } private fun connectionLost() { - callHandler("Device connection was lost") stopForeground(true) val preferences = PreferenceManager.getDefaultSharedPreferences(applicationContext) + if (mDevice != null && !bNoReconnect && preferences.getBoolean("reconnectBluetooth", true)) { - connect(mDevice, true) + try { + connect(mDevice, true) + } catch (e: Exception) { + e.printStackTrace() + state = Constants.STATE_NONE + updateUserInterfaceTitle() + start() + } } else { state = Constants.STATE_NONE updateUserInterfaceTitle() @@ -208,17 +215,30 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a override fun run() { val buffer = ByteArray(10000) var bytes: Int - var out: String - while (true) { - try { - bytes = mmInStream?.read(buffer) ?: 0 - out = String(buffer, 0, bytes) - mHandler?.obtainMessage(Constants.MESSAGE_READ, bytes, -1, out)?.sendToTarget() - } catch (e: IOException) { - e.printStackTrace() - connectionLost() - break + + try { + while (true) { + try { + if ((mmInStream?.available() ?: 0) > 0) { + bytes = mmInStream?.read(buffer) ?: -1 + if (bytes == -1) { + break + } + + val out = String(buffer, 0, bytes) + mHandler?.obtainMessage(Constants.MESSAGE_READ, bytes, -1, out)?.sendToTarget() + } else { + sleep(50) + } + } catch (e: IOException) { + e.printStackTrace() + break + } } + } catch (e: Exception) { + e.printStackTrace() + } finally { + connectionLost() } } @@ -228,6 +248,7 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a mHandler?.obtainMessage(Constants.MESSAGE_WRITE, -1, -1, buffer)?.sendToTarget() } catch (e: IOException) { e.printStackTrace() + connectionLost() } } From 6691273e245368cf99df4e6432361aef601cfccf Mon Sep 17 00:00:00 2001 From: Gideon Okuro Date: Tue, 3 Dec 2024 14:57:48 +0300 Subject: [PATCH 2/4] Update BluetoothChatService.kt --- .../io/treehouses/remote/network/BluetoothChatService.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/src/main/kotlin/io/treehouses/remote/network/BluetoothChatService.kt b/app/src/main/kotlin/io/treehouses/remote/network/BluetoothChatService.kt index 6cf04e98c..c262564db 100644 --- a/app/src/main/kotlin/io/treehouses/remote/network/BluetoothChatService.kt +++ b/app/src/main/kotlin/io/treehouses/remote/network/BluetoothChatService.kt @@ -147,6 +147,7 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a } private fun connectionLost() { + callHandler("Device connection was lost") stopForeground(true) val preferences = PreferenceManager.getDefaultSharedPreferences(applicationContext) @@ -275,4 +276,4 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a this@BluetoothChatService.updateUserInterfaceTitle() } } -} \ No newline at end of file +} From 3a45e5b79f556ecc3aac9801ae3c01f126c4afc3 Mon Sep 17 00:00:00 2001 From: Gideon Okuro Date: Fri, 13 Dec 2024 17:41:40 +0300 Subject: [PATCH 3/4] refactore changes --- .kotlin/errors/errors-1733306496264.log | 4 ++++ .../remote/network/BluetoothChatService.kt | 14 ++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) create mode 100644 .kotlin/errors/errors-1733306496264.log diff --git a/.kotlin/errors/errors-1733306496264.log b/.kotlin/errors/errors-1733306496264.log new file mode 100644 index 000000000..1219b509f --- /dev/null +++ b/.kotlin/errors/errors-1733306496264.log @@ -0,0 +1,4 @@ +kotlin version: 2.0.21 +error message: The daemon has terminated unexpectedly on startup attempt #1 with error code: 0. The daemon process output: + 1. Kotlin compile daemon is ready + diff --git a/app/src/main/kotlin/io/treehouses/remote/network/BluetoothChatService.kt b/app/src/main/kotlin/io/treehouses/remote/network/BluetoothChatService.kt index c262564db..b3da4ed1d 100644 --- a/app/src/main/kotlin/io/treehouses/remote/network/BluetoothChatService.kt +++ b/app/src/main/kotlin/io/treehouses/remote/network/BluetoothChatService.kt @@ -157,17 +157,19 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a connect(mDevice, true) } catch (e: Exception) { e.printStackTrace() - state = Constants.STATE_NONE - updateUserInterfaceTitle() - start() + resetConnectionState() } } else { - state = Constants.STATE_NONE - updateUserInterfaceTitle() - start() + resetConnectionState() } } + private fun resetConnectionState() { + state = Constants.STATE_NONE + updateUserInterfaceTitle() + start() + } + private inner class ConnectThread(private val mmDevice: BluetoothDevice?, secure: Boolean) : Thread() { private val mmSocket: BluetoothSocket? private val mSocketType: String From e0cd3dd794325cf03598155c4d9265edf454b39e Mon Sep 17 00:00:00 2001 From: Gideon Okuro Date: Tue, 17 Dec 2024 13:18:44 +0300 Subject: [PATCH 4/4] Delete .kotlin/errors/errors-1733306496264.log --- .kotlin/errors/errors-1733306496264.log | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 .kotlin/errors/errors-1733306496264.log diff --git a/.kotlin/errors/errors-1733306496264.log b/.kotlin/errors/errors-1733306496264.log deleted file mode 100644 index 1219b509f..000000000 --- a/.kotlin/errors/errors-1733306496264.log +++ /dev/null @@ -1,4 +0,0 @@ -kotlin version: 2.0.21 -error message: The daemon has terminated unexpectedly on startup attempt #1 with error code: 0. The daemon process output: - 1. Kotlin compile daemon is ready -