83 lines
2.7 KiB
Kotlin
83 lines
2.7 KiB
Kotlin
package eu.ztsh.garmin
|
|
|
|
import android.Manifest
|
|
import android.annotation.SuppressLint
|
|
import android.bluetooth.BluetoothAdapter
|
|
import android.bluetooth.BluetoothDevice
|
|
import android.bluetooth.BluetoothManager
|
|
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 eu.ztsh.garmin.databinding.ActivityMainBinding
|
|
|
|
@SuppressLint("MissingPermission")
|
|
class MainActivity : AppCompatActivity() {
|
|
|
|
lateinit var garmin: Garmin
|
|
|
|
private lateinit var binding : ActivityMainBinding
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
binding = ActivityMainBinding.inflate(layoutInflater)
|
|
setContentView(binding.root)
|
|
// bluetoothInit()
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
companion object {
|
|
private const val TAG = "bt"
|
|
}
|
|
|
|
}
|