fix: Garmin connection method rebuilt
This commit is contained in:
parent
df78dfe7ce
commit
c7fc9e9c09
2 changed files with 23 additions and 14 deletions
|
@ -17,6 +17,7 @@ import java.io.IOException
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import java.util.concurrent.Executors
|
import java.util.concurrent.Executors
|
||||||
import java.util.concurrent.SynchronousQueue
|
import java.util.concurrent.SynchronousQueue
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean
|
||||||
|
|
||||||
@SuppressLint("MissingPermission")
|
@SuppressLint("MissingPermission")
|
||||||
class Garmin(
|
class Garmin(
|
||||||
|
@ -185,6 +186,7 @@ class Garmin(
|
||||||
private inner class ConnectThread : Thread() {
|
private inner class ConnectThread : Thread() {
|
||||||
|
|
||||||
private val queue: SynchronousQueue<IntArray> = SynchronousQueue()
|
private val queue: SynchronousQueue<IntArray> = SynchronousQueue()
|
||||||
|
private val state = AtomicBoolean(false)
|
||||||
private var current: IntArray = intArrayOf()
|
private var current: IntArray = intArrayOf()
|
||||||
|
|
||||||
private val socket: BluetoothSocket? by lazy(LazyThreadSafetyMode.NONE) {
|
private val socket: BluetoothSocket? by lazy(LazyThreadSafetyMode.NONE) {
|
||||||
|
@ -192,18 +194,31 @@ class Garmin(
|
||||||
device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"))
|
device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"))
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun run() {
|
fun tryConnect() {
|
||||||
// Cancel discovery because it otherwise slows down the connection.
|
|
||||||
context.checkBt()
|
context.checkBt()
|
||||||
|
// Cancel discovery because it otherwise slows down the connection.
|
||||||
adapter.cancelDiscovery()
|
adapter.cancelDiscovery()
|
||||||
try {
|
try {
|
||||||
socket?.connect()
|
socket?.connect()
|
||||||
context.setConnectionStatus(true)
|
context.toastConnectionStatus(true)
|
||||||
sleep(3000)
|
sleep(3000)
|
||||||
readAll()
|
readAll()
|
||||||
send(intArrayOf(0x07, 0x01)) // Set GPS to true
|
send(intArrayOf(0x07, 0x01)) // Set GPS to true
|
||||||
send(intArrayOf(0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)) // Clear screen
|
send(intArrayOf(0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)) // Clear screen
|
||||||
while (true) {
|
state.set(true)
|
||||||
|
} catch (e: IOException) {
|
||||||
|
Log.d(TAG, "Not connected", e)
|
||||||
|
context.toastConnectionStatus(false)
|
||||||
|
state.set(false)
|
||||||
|
}
|
||||||
|
queue.clear()
|
||||||
|
current = intArrayOf()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun run() {
|
||||||
|
tryConnect()
|
||||||
|
while (true) {
|
||||||
|
if (state.get()) {
|
||||||
val newCurrent = queue.poll()
|
val newCurrent = queue.poll()
|
||||||
if (newCurrent == null) {
|
if (newCurrent == null) {
|
||||||
Log.d(TAG, "run (${currentThread().name}): Sleep...")
|
Log.d(TAG, "run (${currentThread().name}): Sleep...")
|
||||||
|
@ -212,15 +227,9 @@ class Garmin(
|
||||||
current = newCurrent
|
current = newCurrent
|
||||||
}
|
}
|
||||||
send(current)
|
send(current)
|
||||||
}
|
} else {
|
||||||
} catch (e: IOException) {
|
queue.clear()
|
||||||
Log.d(TAG, "Not connected", e)
|
sleep(2000)
|
||||||
context.setConnectionStatus(false)
|
|
||||||
while (true) {
|
|
||||||
// Just dequeue
|
|
||||||
// TODO: Add option to reconnect
|
|
||||||
queue.poll()
|
|
||||||
sleep(900)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -124,7 +124,7 @@ class MainActivity : AppCompatActivity() {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setConnectionStatus(success: Boolean) {
|
fun toastConnectionStatus(success: Boolean) {
|
||||||
this.runOnUiThread {
|
this.runOnUiThread {
|
||||||
if (success) {
|
if (success) {
|
||||||
Toast.makeText(this, "Garmin connected", Toast.LENGTH_LONG).show()
|
Toast.makeText(this, "Garmin connected", Toast.LENGTH_LONG).show()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue