dropdown menüadded not functional
This commit is contained in:
1 parent
c53eefaf79
commit
471741aeb4
5 files changed
+97
-21
No files matched your search
Generated
+8
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="MarkdownSettings">
|
||||||
|
<option name="previewPanelProviderInfo">
|
||||||
|
<ProviderInfo name="Compose (experimental)" className="com.intellij.markdown.compose.preview.ComposePanelProvider" />
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
@@ -29,6 +29,7 @@ import java.util.UUID
|
|||||||
import kotlin.time.Duration.Companion.milliseconds
|
import kotlin.time.Duration.Companion.milliseconds
|
||||||
|
|
||||||
class BleManager(private val context: Context) {
|
class BleManager(private val context: Context) {
|
||||||
|
val deviceAddress = "10:BD:A3:8F:01:0E"
|
||||||
val currentDate: LocalDate? = LocalDate.now()
|
val currentDate: LocalDate? = LocalDate.now()
|
||||||
val deviceUUIDs = DeviceUUIDs
|
val deviceUUIDs = DeviceUUIDs
|
||||||
private var bluetoothGatt: BluetoothGatt? = null
|
private var bluetoothGatt: BluetoothGatt? = null
|
||||||
@@ -39,11 +40,30 @@ class BleManager(private val context: Context) {
|
|||||||
var isConnected by mutableStateOf(false)
|
var isConnected by mutableStateOf(false)
|
||||||
private set
|
private set
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
|
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
|
||||||
fun init() {
|
fun init() {
|
||||||
bluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
|
bluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
|
||||||
bluetoothAdapter = bluetoothManager?.adapter
|
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)
|
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
|
||||||
fun checkCurrentConnectionState(deviceAddress: String) {
|
fun checkCurrentConnectionState(deviceAddress: String) {
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package com.example.smart_watch_android
|
package com.example.smart_watch_android
|
||||||
|
|
||||||
|
import android.Manifest
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
|
import android.content.Context
|
||||||
|
import androidx.annotation.RequiresPermission
|
||||||
import androidx.lifecycle.AndroidViewModel
|
import androidx.lifecycle.AndroidViewModel
|
||||||
import com.example.smart_watch_android.BLE.BleManager
|
import com.example.smart_watch_android.BLE.BleManager
|
||||||
|
|
||||||
@@ -11,7 +14,8 @@ class MainViewModel(
|
|||||||
lateinit var bleManager: BleManager
|
lateinit var bleManager: BleManager
|
||||||
private set
|
private set
|
||||||
|
|
||||||
fun initBleManager(activityContext: android.content.Context) {
|
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
|
||||||
|
fun initBleManager(activityContext: Context) {
|
||||||
if (!::bleManager.isInitialized) {
|
if (!::bleManager.isInitialized) {
|
||||||
bleManager = BleManager(context = activityContext)
|
bleManager = BleManager(context = activityContext)
|
||||||
bleManager.init()
|
bleManager.init()
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.example.smart_watch_android.Screens
|
package com.example.smart_watch_android.Screens
|
||||||
|
|
||||||
import android.Manifest
|
import android.Manifest
|
||||||
|
import android.R
|
||||||
import androidx.annotation.RequiresPermission
|
import androidx.annotation.RequiresPermission
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.DisposableEffect
|
import androidx.compose.runtime.DisposableEffect
|
||||||
@@ -41,10 +42,7 @@ fun AppNavigation(bleManager: BleManager) {
|
|||||||
// Connect Screen
|
// Connect Screen
|
||||||
composable("connect") {
|
composable("connect") {
|
||||||
ConnectScreen(
|
ConnectScreen(
|
||||||
isConnected = bleManager.isConnected,
|
bleManager = bleManager
|
||||||
onConnectClicked = {
|
|
||||||
bleManager.connectToEsp32("10:BD:A3:8F:01:0E")
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,35 +1,81 @@
|
|||||||
package com.example.smart_watch_android.Screens
|
package com.example.smart_watch_android.Screens
|
||||||
|
|
||||||
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Box
|
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.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.size
|
||||||
|
import androidx.compose.foundation.layout.width
|
||||||
import androidx.compose.foundation.shape.CircleShape
|
import androidx.compose.foundation.shape.CircleShape
|
||||||
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||||
import androidx.compose.material3.Button
|
import androidx.compose.material3.Button
|
||||||
import androidx.compose.material3.ButtonDefaults
|
import androidx.compose.material3.ButtonDefaults
|
||||||
|
import androidx.compose.material3.DropdownMenu
|
||||||
|
import androidx.compose.material3.DropdownMenuItem
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.Composable
|
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.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.compose.ui.unit.sp
|
import androidx.compose.ui.unit.sp
|
||||||
|
import com.example.smart_watch_android.BLE.BleManager
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun ConnectScreen(isConnected: Boolean, onConnectClicked: () -> Unit) {
|
fun ConnectScreen(bleManager: BleManager) {
|
||||||
Box(
|
var expanded by remember { mutableStateOf(false) }
|
||||||
modifier = Modifier.fillMaxSize(),
|
var selectedDevice by remember { mutableStateOf("device") }
|
||||||
contentAlignment = Alignment.Center
|
val items = listOf("Option 1", "Option 2", "Option 3")
|
||||||
) {
|
Column(
|
||||||
Button(
|
modifier = Modifier
|
||||||
onClick = onConnectClicked,
|
.fillMaxSize()
|
||||||
modifier = Modifier.size(300.dp),
|
.padding(16.dp),
|
||||||
shape = CircleShape,
|
verticalArrangement = Arrangement.Center,
|
||||||
colors = ButtonDefaults.buttonColors(containerColor = Color(0xFF5E18BE))
|
horizontalAlignment = Alignment.CenterHorizontally
|
||||||
) {
|
)
|
||||||
Text(
|
{
|
||||||
text = "Connect",
|
Button(
|
||||||
fontSize = 30.sp
|
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
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user