0
0
mirror of https://github.com/markusfisch/BinaryEye.git synced 2024-09-19 19:42:18 +02:00

Fix failing send due to invalid bluetooth connection

Try reconnect to socket once before failing.
This commit is contained in:
KamaleiZestri 2024-03-21 15:19:51 -05:00 committed by GitHub
parent 2dcf7772f2
commit 554f5a5e73
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -21,10 +21,10 @@ fun Scan.sendBluetoothAsync(
val connected = if (isConnected) {
true
} else {
connect(host)
connect(host,true)
}
val sent = if (connected) {
send(content)
send(content,host,true)
} else {
false
}
@ -62,7 +62,7 @@ private val uuid = UUID.fromString(
private var isConnected = false
private fun connect(deviceName: String): Boolean = try {
private fun connect(deviceName: String, onceMore: Boolean): Boolean = try {
val device = findByName(deviceName) ?: throw RuntimeException(
"Bluetooth device not found"
)
@ -72,11 +72,14 @@ private fun connect(deviceName: String): Boolean = try {
isConnected = true
true
} catch (e: Exception) {
close()
if (onceMore)
connect(deviceName, false)
else
close()
false
}
private fun send(message: String): Boolean = try {
private fun send(message: String, host: String, onceMore: Boolean): Boolean = try {
writer?.apply {
write(message)
write("\n")
@ -85,7 +88,11 @@ private fun send(message: String): Boolean = try {
true
} catch (e: Exception) {
close()
false
if (connect(host, false))
send(message, host, false)
else
false
}
private fun close() {