Garmin connection (DIRTY)
This commit is contained in:
parent
78922546ab
commit
0c6e305aea
10 changed files with 638 additions and 59 deletions
112
app/src/main/java/eu/ztsh/garmin/Garmin.kt
Normal file
112
app/src/main/java/eu/ztsh/garmin/Garmin.kt
Normal file
|
@ -0,0 +1,112 @@
|
|||
package eu.ztsh.garmin
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.bluetooth.BluetoothAdapter
|
||||
import android.bluetooth.BluetoothDevice
|
||||
import android.bluetooth.BluetoothSocket
|
||||
import android.util.Log
|
||||
import java.io.IOException
|
||||
import java.util.*
|
||||
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
class Garmin(
|
||||
val context: MainActivity,
|
||||
val device: BluetoothDevice,
|
||||
val adapter: BluetoothAdapter
|
||||
) {
|
||||
|
||||
private lateinit var thread: ConnectThread
|
||||
|
||||
fun start() {
|
||||
thread = ConnectThread()
|
||||
thread.start()
|
||||
}
|
||||
|
||||
fun close() {
|
||||
thread.close()
|
||||
}
|
||||
|
||||
private inner class ConnectThread : Thread() {
|
||||
|
||||
private val socket: BluetoothSocket? by lazy(LazyThreadSafetyMode.NONE) {
|
||||
context.checkBt()
|
||||
device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"))
|
||||
}
|
||||
|
||||
override fun run() {
|
||||
// Cancel discovery because it otherwise slows down the connection.
|
||||
context.checkBt()
|
||||
adapter.cancelDiscovery()
|
||||
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))
|
||||
}
|
||||
|
||||
// Closes the client socket and causes the thread to finish.
|
||||
fun close() {
|
||||
try {
|
||||
socket?.close()
|
||||
} catch (e: IOException) {
|
||||
Log.e(TAG, "Could not close the client socket", e)
|
||||
}
|
||||
}
|
||||
|
||||
fun readAll() {
|
||||
val buffer = ByteArray(64)
|
||||
val istr = socket!!.inputStream
|
||||
istr!!.let {
|
||||
if (it.available() > 0) {
|
||||
it.read(buffer)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun send(hex: IntArray) {
|
||||
sendRaw(prepareData(hex))
|
||||
}
|
||||
|
||||
private fun sendRaw(buff: IntArray) {
|
||||
buff.forEach { socket!!.outputStream.write(it) }
|
||||
socket!!.outputStream.flush()
|
||||
sleep(2000)
|
||||
readAll()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun prepareData(input: IntArray): IntArray {
|
||||
val n = input.size
|
||||
var crc = (0xeb + n + n).toUInt()
|
||||
val chars = ArrayList<Int>()
|
||||
chars.add(0x10)
|
||||
chars.add(0x7b)
|
||||
chars.add((n + 0x06))
|
||||
if (n == 0xa)
|
||||
chars.add(0x10)
|
||||
chars.add(n)
|
||||
chars.add(0x00)
|
||||
chars.add(0x00)
|
||||
chars.add(0x00)
|
||||
chars.add(0x55)
|
||||
chars.add(0x15)
|
||||
for (char in input) {
|
||||
crc = (crc + char.toUInt())
|
||||
chars.add(char)
|
||||
if (char == 0x10)
|
||||
chars.add(0x10)
|
||||
}
|
||||
chars.add((-(crc.toInt()) and 0xff))
|
||||
chars.add(0x10)
|
||||
chars.add(0x03)
|
||||
return chars.toIntArray()
|
||||
}
|
||||
|
||||
private const val TAG = "GARMIN"
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue