bluetooth check and enabling
This commit is contained in:
1 parent
025f754504
commit
89a0671cff
3 files changed
+18
-6
No files matched your search
@@ -18,6 +18,7 @@ import androidx.annotation.RequiresApi
|
||||
import androidx.annotation.RequiresPermission
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.core.app.ActivityCompat
|
||||
import androidx.core.app.ActivityCompat.startActivityForResult
|
||||
import com.example.smart_watch_android.MainViewModel
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@@ -40,7 +41,7 @@ object BLEManager{
|
||||
private val deviceUUIDs = DeviceUUIDs
|
||||
private var bluetoothGatt: BluetoothGatt? = null
|
||||
private lateinit var bluetoothManager: BluetoothManager
|
||||
private lateinit var bluetoothAdapter: BluetoothAdapter
|
||||
lateinit var bluetoothAdapter: BluetoothAdapter
|
||||
val gattCallback = object : BluetoothGattCallback() {
|
||||
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
|
||||
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
|
||||
@@ -108,7 +109,6 @@ object BLEManager{
|
||||
}
|
||||
|
||||
if (missingPermissions.isNotEmpty()) {
|
||||
// Berechtigungen anfordern
|
||||
ActivityCompat.requestPermissions(activity, missingPermissions.toTypedArray(), 101)
|
||||
} else {
|
||||
Log.d("ESP32_BLE", "Alle Bluetooth-Berechtigungen sind bereits erteilt!")
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
package com.example.smart_watch_android
|
||||
|
||||
import android.Manifest
|
||||
import android.bluetooth.BluetoothAdapter
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.activity.viewModels
|
||||
import androidx.annotation.RequiresApi
|
||||
import androidx.annotation.RequiresPermission
|
||||
@@ -17,12 +21,24 @@ import com.example.smart_watch_android.BLE.BLEManager
|
||||
import com.example.smart_watch_android.Screens.AppNavigation
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
private val enableBluetoothLauncher = registerForActivityResult(
|
||||
ActivityResultContracts.StartActivityForResult()
|
||||
) { result ->
|
||||
if (result.resultCode == RESULT_OK) {
|
||||
Log.d("BLE_","Bluetooth enabled")
|
||||
}
|
||||
}
|
||||
private val viewModel: MainViewModel by viewModels()
|
||||
@RequiresApi(Build.VERSION_CODES.S)
|
||||
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
|
||||
override fun onCreate(savedInstanceState: Bundle?,) {
|
||||
super.onCreate(savedInstanceState)
|
||||
BLEManager.init(context = applicationContext, activity = this)
|
||||
val adapter = BLEManager.bluetoothAdapter
|
||||
if (!adapter.isEnabled) {
|
||||
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
|
||||
enableBluetoothLauncher.launch(enableBtIntent)
|
||||
}
|
||||
enableEdgeToEdge()
|
||||
setContent {
|
||||
MaterialTheme {
|
||||
|
||||
@@ -62,10 +62,6 @@ fun ConnectScreen(viewModel: MainViewModel) {
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
)
|
||||
{
|
||||
Box(
|
||||
){
|
||||
Text("if the list is empty ensure you have bluetooth enabled!")
|
||||
}
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.padding(16.dp)
|
||||
|
||||
Reference in new issue
Block a user