WIP: WIP
This commit is contained in:
parent
49559993d8
commit
0a00d13706
6 changed files with 328 additions and 15 deletions
|
@ -5,8 +5,10 @@ import android.bluetooth.BluetoothAdapter
|
|||
import android.bluetooth.BluetoothDevice
|
||||
import android.bluetooth.BluetoothSocket
|
||||
import android.util.Log
|
||||
import com.mapbox.navigation.ui.maneuver.model.Maneuver
|
||||
import java.io.IOException
|
||||
import java.util.*
|
||||
import java.util.concurrent.SynchronousQueue
|
||||
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
|
@ -16,19 +18,79 @@ class Garmin(
|
|||
val adapter: BluetoothAdapter
|
||||
) {
|
||||
|
||||
private lateinit var thread: ConnectThread
|
||||
private lateinit var connection: ConnectThread
|
||||
private lateinit var processing: ProcessingThread
|
||||
private var stateCache: State = State()
|
||||
|
||||
fun start() {
|
||||
thread = ConnectThread()
|
||||
thread.start()
|
||||
connection = ConnectThread()
|
||||
connection.start()
|
||||
}
|
||||
|
||||
fun close() {
|
||||
thread.close()
|
||||
connection.close()
|
||||
}
|
||||
|
||||
fun process(maneuver: Maneuver) {
|
||||
processing = ProcessingThread(maneuver)
|
||||
processing.start()
|
||||
processing.join()
|
||||
}
|
||||
|
||||
|
||||
|
||||
private inner class ProcessingThread(val maneuver: Maneuver) : Thread() {
|
||||
override fun run() {
|
||||
send(ManeuverMapper.apply(maneuver))
|
||||
}
|
||||
|
||||
fun send(incoming: eu.ztsh.garmin.State) {
|
||||
if (stateCache.distance != incoming.distance) {
|
||||
setDistance(incoming)
|
||||
}
|
||||
if (stateCache.direction != incoming.direction) {
|
||||
setDirection(stateCache.direction)
|
||||
}
|
||||
stateCache = incoming
|
||||
}
|
||||
|
||||
private fun setDistance(state: eu.ztsh.garmin.State) {
|
||||
connection.enqueue(intArrayOf(
|
||||
0x03, asDigit(state.distance / 1000), asDigit(state.distance / 100), asDigit(state.distance / 10),
|
||||
0x00, asDigit(state.distance), state.unit.data
|
||||
))
|
||||
}
|
||||
|
||||
private fun setDirection(direction: Direction) {
|
||||
val param1 = when (direction.outAngle) {
|
||||
OutAngle.LeftDown -> 0x10
|
||||
OutAngle.RightDown -> 0x20
|
||||
else -> direction.outType.data
|
||||
}
|
||||
val param2: Int = if (direction.outType == OutType.RightRoundabout
|
||||
|| direction.outType == OutType.LeftRoundabout) {
|
||||
if (direction.roundabout == OutAngle.AsDirection) direction.outAngle.data else direction.roundabout.data
|
||||
} else {
|
||||
0x00
|
||||
}
|
||||
val param3: Int = if (direction.outAngle == OutAngle.LeftDown || direction.outAngle == OutAngle.RightDown) 0x00 else direction.outAngle.data
|
||||
connection.enqueue(intArrayOf(0x01, param1, param2, param3))
|
||||
}
|
||||
|
||||
private fun asDigit(n: Int): Int {
|
||||
if (n == 0) {
|
||||
return 0
|
||||
}
|
||||
val m = n % 10
|
||||
return if (m == 0) 10 else m
|
||||
}
|
||||
}
|
||||
|
||||
private inner class ConnectThread : Thread() {
|
||||
|
||||
private val queue: SynchronousQueue<IntArray> = SynchronousQueue()
|
||||
private var current: IntArray = intArrayOf()
|
||||
|
||||
private val socket: BluetoothSocket? by lazy(LazyThreadSafetyMode.NONE) {
|
||||
context.checkBt()
|
||||
device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"))
|
||||
|
@ -41,10 +103,12 @@ 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) {
|
||||
val newCurrent = Optional.ofNullable(queue.poll()).orElse(current)
|
||||
current = newCurrent
|
||||
send(current)
|
||||
sleep(900)
|
||||
}
|
||||
}
|
||||
|
||||
// Closes the client socket and causes the thread to finish.
|
||||
|
@ -56,7 +120,15 @@ class Garmin(
|
|||
}
|
||||
}
|
||||
|
||||
fun readAll() {
|
||||
fun enqueue(data: IntArray) {
|
||||
queue.put(data)
|
||||
}
|
||||
|
||||
private fun send(data: IntArray) {
|
||||
sendRaw(prepareData(data))
|
||||
}
|
||||
|
||||
private fun readAll() {
|
||||
val buffer = ByteArray(64)
|
||||
val istr = socket!!.inputStream
|
||||
istr!!.let {
|
||||
|
@ -66,10 +138,6 @@ class Garmin(
|
|||
}
|
||||
}
|
||||
|
||||
fun send(hex: IntArray) {
|
||||
sendRaw(prepareData(hex))
|
||||
}
|
||||
|
||||
private fun sendRaw(buff: IntArray) {
|
||||
buff.forEach { socket!!.outputStream.write(it) }
|
||||
socket!!.outputStream.flush()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue