Garmin queue (DIRTY)

This commit is contained in:
Piotr Dec 2023-08-10 01:19:27 +02:00
parent 0c6e305aea
commit 307343c2eb

View file

@ -5,8 +5,10 @@ import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothSocket
import android.util.Log
import me.trevi.navparser.lib.NavigationData
import java.io.IOException
import java.util.*
import java.util.concurrent.SynchronousQueue
@SuppressLint("MissingPermission")
@ -23,6 +25,14 @@ class Garmin(
thread.start()
}
fun send(data: IntArray) {
thread.enqueue(data)
}
fun send(data: NavigationData) {
Log.d("SEND_DATA", "$data.remainingDistance.distance")
}
fun close() {
thread.close()
}
@ -34,6 +44,13 @@ class Garmin(
device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"))
}
private val queue: SynchronousQueue<IntArray> = SynchronousQueue()
fun enqueue(data: IntArray) {
// queue.clear() // for immediate run
queue.put(data)
}
override fun run() {
// Cancel discovery because it otherwise slows down the connection.
context.checkBt()
@ -41,10 +58,9 @@ class Garmin(
socket?.connect()
sleep(3000)
readAll()
send(intArrayOf(1, 1, 0, 16))
send(intArrayOf(3, 0, 1, 10, 0, 10, 3))
send(intArrayOf(3, 0, 0, 9, 0, 9, 3))
send(intArrayOf(0x04, 0x01))
while (true) {
queue.poll()?.let { send(it) }
}
}
// Closes the client socket and causes the thread to finish.
@ -66,8 +82,8 @@ class Garmin(
}
}
fun send(hex: IntArray) {
sendRaw(prepareData(hex))
fun send(data: IntArray) {
sendRaw(prepareData(data))
}
private fun sendRaw(buff: IntArray) {