Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bluetooth: smoother connections (fixes #2160) #2161

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,16 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a
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()
Okuro3499 marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
state = Constants.STATE_NONE
updateUserInterfaceTitle()
Expand Down Expand Up @@ -208,17 +216,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()
}
}

Expand All @@ -228,6 +249,7 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a
mHandler?.obtainMessage(Constants.MESSAGE_WRITE, -1, -1, buffer)?.sendToTarget()
} catch (e: IOException) {
e.printStackTrace()
connectionLost()
}
}

Expand All @@ -254,4 +276,4 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a
[email protected]()
}
}
}
}
Loading