Garmin connection (DIRTY)
This commit is contained in:
parent
78922546ab
commit
0c6e305aea
10 changed files with 638 additions and 59 deletions
|
@ -1,11 +1,125 @@
|
|||
package eu.ztsh.garmin
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import android.Manifest
|
||||
import android.annotation.SuppressLint
|
||||
import android.bluetooth.BluetoothAdapter
|
||||
import android.bluetooth.BluetoothDevice
|
||||
import android.bluetooth.BluetoothManager
|
||||
import android.bluetooth.BluetoothSocket
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import androidx.activity.result.ActivityResultCallback
|
||||
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.app.ActivityCompat
|
||||
import java.io.IOException
|
||||
import java.util.*
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
class MainActivity : AppCompatActivity() {
|
||||
|
||||
lateinit var garmin: Garmin
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
bluetoothInit()
|
||||
}
|
||||
}
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
// bluetoothSerial.onPause()
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
// bluetoothSerial.onResume()
|
||||
}
|
||||
|
||||
private fun bluetoothInit() {
|
||||
val bluetoothManager: BluetoothManager = getSystemService(BluetoothManager::class.java)
|
||||
val bluetoothAdapter: BluetoothAdapter = bluetoothManager.adapter
|
||||
?: // Device doesn't support Bluetooth
|
||||
throw Exception()
|
||||
if (!bluetoothAdapter.isEnabled) {
|
||||
// TODO: Start intent
|
||||
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
|
||||
registerForActivityResult(StartActivityForResult(), ActivityResultCallback { })
|
||||
}
|
||||
checkBt()
|
||||
|
||||
val pairedDevices: Set<BluetoothDevice>? = bluetoothAdapter.bondedDevices
|
||||
val context = this
|
||||
pairedDevices?.firstOrNull { device ->
|
||||
Log.d(TAG, device.name)
|
||||
device.name.equals("GARMIN HUD")
|
||||
}?.apply {
|
||||
garmin = Garmin(context, this, bluetoothAdapter)
|
||||
garmin.start()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun checkBt(): Boolean {
|
||||
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.S) {
|
||||
checkBt(Manifest.permission.BLUETOOTH_SCAN)
|
||||
checkBt(Manifest.permission.BLUETOOTH_CONNECT)
|
||||
} else {
|
||||
checkBt(Manifest.permission.BLUETOOTH)
|
||||
checkBt(Manifest.permission.BLUETOOTH_ADMIN)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
private fun checkBt(permission: String): Boolean {
|
||||
if (ActivityCompat.checkSelfPermission(
|
||||
this,
|
||||
permission
|
||||
) != PackageManager.PERMISSION_GRANTED
|
||||
) {
|
||||
ActivityCompat.requestPermissions(this, arrayOf(permission), 1)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
private inner class ConnectThread(val device: BluetoothDevice, val adapter: BluetoothAdapter) : Thread() {
|
||||
|
||||
private val mmSocket: BluetoothSocket? by lazy(LazyThreadSafetyMode.NONE) {
|
||||
checkBt()
|
||||
// device.createInsecureRfcommSocketToServiceRecord(UUID.fromString("7d00d7f5-921b-450c-8eda-26e1d4a15c61"))
|
||||
device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"))
|
||||
}
|
||||
|
||||
override fun run() {
|
||||
// Cancel discovery because it otherwise slows down the connection.
|
||||
checkBt()
|
||||
adapter.cancelDiscovery()
|
||||
|
||||
mmSocket?.let { socket ->
|
||||
// Connect to the remote device through the socket. This call blocks
|
||||
// until it succeeds or throws an exception.
|
||||
socket.connect()
|
||||
|
||||
// The connection attempt succeeded. Perform work associated with
|
||||
// the connection in a separate thread.
|
||||
// manageMyConnectedSocket(socket)
|
||||
}
|
||||
}
|
||||
|
||||
// Closes the client socket and causes the thread to finish.
|
||||
fun cancel() {
|
||||
try {
|
||||
mmSocket?.close()
|
||||
} catch (e: IOException) {
|
||||
Log.e(Companion.TAG, "Could not close the client socket", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val TAG = "bt"
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue