diff --git a/.idea/markdown.xml b/.idea/markdown.xml new file mode 100644 index 0000000..c61ea33 --- /dev/null +++ b/.idea/markdown.xml @@ -0,0 +1,8 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/example/smart_watch_android/BLE/BleManager.kt b/app/src/main/java/com/example/smart_watch_android/BLE/BleManager.kt index 3bb532b..7b0dec5 100644 --- a/app/src/main/java/com/example/smart_watch_android/BLE/BleManager.kt +++ b/app/src/main/java/com/example/smart_watch_android/BLE/BleManager.kt @@ -29,6 +29,7 @@ import java.util.UUID import kotlin.time.Duration.Companion.milliseconds class BleManager(private val context: Context) { + val deviceAddress = "10:BD:A3:8F:01:0E" val currentDate: LocalDate? = LocalDate.now() val deviceUUIDs = DeviceUUIDs private var bluetoothGatt: BluetoothGatt? = null @@ -39,11 +40,30 @@ class BleManager(private val context: Context) { var isConnected by mutableStateOf(false) private set + + @RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT) fun init() { bluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager bluetoothAdapter = bluetoothManager?.adapter - checkCurrentConnectionState("10:BD:A3:8F:01:0E") + + if (bluetoothAdapter == null) return + + try { + val device = bluetoothAdapter?.getRemoteDevice(deviceAddress) + if (device != null) { + val state = bluetoothManager!!.getConnectionState(device, BluetoothProfile.GATT) + if (state == BluetoothProfile.STATE_CONNECTED) { + Log.d("ESP32_BLE", "App restarted: device still connected. rebuild GATT-connection...") + connectToEsp32(deviceAddress) + } else { + isConnected = false + Log.d("ESP32_BLE", "App restarted: not connected.") + } + } + } catch (e: Exception) { + Log.d("ESP32_BLE", "error while checking the status: ${e.message}") + } } @RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT) fun checkCurrentConnectionState(deviceAddress: String) { diff --git a/app/src/main/java/com/example/smart_watch_android/MainViewModel.kt b/app/src/main/java/com/example/smart_watch_android/MainViewModel.kt index 2dcc5e8..55003e1 100644 --- a/app/src/main/java/com/example/smart_watch_android/MainViewModel.kt +++ b/app/src/main/java/com/example/smart_watch_android/MainViewModel.kt @@ -1,6 +1,9 @@ package com.example.smart_watch_android +import android.Manifest import android.app.Application +import android.content.Context +import androidx.annotation.RequiresPermission import androidx.lifecycle.AndroidViewModel import com.example.smart_watch_android.BLE.BleManager @@ -11,7 +14,8 @@ class MainViewModel( lateinit var bleManager: BleManager private set - fun initBleManager(activityContext: android.content.Context) { + @RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT) + fun initBleManager(activityContext: Context) { if (!::bleManager.isInitialized) { bleManager = BleManager(context = activityContext) bleManager.init() diff --git a/app/src/main/java/com/example/smart_watch_android/Screens/AppNavigation.kt b/app/src/main/java/com/example/smart_watch_android/Screens/AppNavigation.kt index f4334e0..98f292d 100644 --- a/app/src/main/java/com/example/smart_watch_android/Screens/AppNavigation.kt +++ b/app/src/main/java/com/example/smart_watch_android/Screens/AppNavigation.kt @@ -1,6 +1,7 @@ package com.example.smart_watch_android.Screens import android.Manifest +import android.R import androidx.annotation.RequiresPermission import androidx.compose.runtime.Composable import androidx.compose.runtime.DisposableEffect @@ -41,10 +42,7 @@ fun AppNavigation(bleManager: BleManager) { // Connect Screen composable("connect") { ConnectScreen( - isConnected = bleManager.isConnected, - onConnectClicked = { - bleManager.connectToEsp32("10:BD:A3:8F:01:0E") - } + bleManager = bleManager ) } diff --git a/app/src/main/java/com/example/smart_watch_android/Screens/ConnectScreen.kt b/app/src/main/java/com/example/smart_watch_android/Screens/ConnectScreen.kt index 6626d88..2b3a369 100644 --- a/app/src/main/java/com/example/smart_watch_android/Screens/ConnectScreen.kt +++ b/app/src/main/java/com/example/smart_watch_android/Screens/ConnectScreen.kt @@ -1,35 +1,81 @@ package com.example.smart_watch_android.Screens +import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size +import androidx.compose.foundation.layout.width import androidx.compose.foundation.shape.CircleShape +import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.Button import androidx.compose.material3.ButtonDefaults +import androidx.compose.material3.DropdownMenu +import androidx.compose.material3.DropdownMenuItem import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp +import com.example.smart_watch_android.BLE.BleManager @Composable -fun ConnectScreen(isConnected: Boolean, onConnectClicked: () -> Unit) { - Box( - modifier = Modifier.fillMaxSize(), - contentAlignment = Alignment.Center - ) { - Button( - onClick = onConnectClicked, - modifier = Modifier.size(300.dp), - shape = CircleShape, - colors = ButtonDefaults.buttonColors(containerColor = Color(0xFF5E18BE)) - ) { - Text( - text = "Connect", - fontSize = 30.sp +fun ConnectScreen(bleManager: BleManager) { + var expanded by remember { mutableStateOf(false) } + var selectedDevice by remember { mutableStateOf("device") } + val items = listOf("Option 1", "Option 2", "Option 3") + Column( + modifier = Modifier + .fillMaxSize() + .padding(16.dp), + verticalArrangement = Arrangement.Center, + horizontalAlignment = Alignment.CenterHorizontally + ) + { + Button( + onClick = { expanded = !expanded }, + shape = RoundedCornerShape(4.dp), + modifier = Modifier.width(400.dp) + ) { + Text(selectedDevice) + } + DropdownMenu( + expanded = expanded, + onDismissRequest = { expanded = false } + ) { + items.forEach { item -> + DropdownMenuItem( + text = { Text(item) }, + onClick = { + selectedDevice = item + expanded = false + } + ) + } + } + + Spacer(modifier = Modifier.height(64.dp)) + + Button( + onClick = { bleManager.connectToEsp32(deviceAddress = "10:BD:A3:8F:01:0E") }, + modifier = Modifier.size(400.dp), + shape = CircleShape, + colors = ButtonDefaults.buttonColors(containerColor = Color(0xFF5E18BE)) + ) { + Text( + text = "Connect", + fontSize = 32.sp ) - } + } } } \ No newline at end of file