time sync with esp32

This commit is contained in:
Gitea committed 2026-10-17 14:12:28 +02:00
1 parent 21883af93b
commit f8337eef46
6 files changed
+53 -6

No files matched your search

+1
View File
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings"> <component name="GradleSettings">
<option name="linkedExternalProjectsSettings"> <option name="linkedExternalProjectsSettings">
<GradleProjectSettings> <GradleProjectSettings>
+1 -1
View File
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" /> <component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_25" project-jdk-name="jbr-25" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_25" default="true" project-jdk-name="jbr-25" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" /> <output url="file://$PROJECT_DIR$/build/classes" />
</component> </component>
<component name="ProjectType"> <component name="ProjectType">
Generated
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
@@ -18,9 +18,19 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.time.LocalDate
import java.time.LocalTime
import java.time.format.DateTimeFormatter
import java.util.UUID import java.util.UUID
import kotlin.time.Duration.Companion.milliseconds
class BleManager(private val context: Context) { class BleManager(private val context: Context) {
val currentTime = LocalTime.now()
val currentDate = LocalDate.now()
val deviceUUIDs = DeviceUUIDs val deviceUUIDs = DeviceUUIDs
private var bluetoothGatt: BluetoothGatt? = null private var bluetoothGatt: BluetoothGatt? = null
@@ -89,7 +99,7 @@ class BleManager(private val context: Context) {
Log.d("ESP32_BLE", "6. onServicesDiscovered aufgerufen mit Status: $status") Log.d("ESP32_BLE", "6. onServicesDiscovered aufgerufen mit Status: $status")
if (status == BluetoothGatt.GATT_SUCCESS) { if (status == BluetoothGatt.GATT_SUCCESS) {
isConnected = true isConnected = true
writeToEsp32("Hallo ESP32", deviceUUIDs.TIME_CHARACTERISTIC_UUID, deviceUUIDs.TIME_CHARACTERISTIC_UUID) writeToEsp32("Hallo ESP32", deviceUUIDs.SERVICE_UUID, deviceUUIDs.CHAR_DATE_UUID)
Log.d("ESP32_BLE", "7. Services erfolgreich gefunden! Verbindung komplett erfolgreich.") Log.d("ESP32_BLE", "7. Services erfolgreich gefunden! Verbindung komplett erfolgreich.")
} }
} }
@@ -120,7 +130,7 @@ class BleManager(private val context: Context) {
} }
} }
fun writeToEsp32(dataString: String, SERVICE_UUID: UUID?, TIME_CHARACTERISTIC_UUID: UUID?) { fun writeToEsp32(dataString: String, SERVICE_UUID: UUID?, CHARACTERISTIC_UUID: UUID?) {
// Prüfen, ob die Verbindung steht und bluetoothGatt nicht null ist // Prüfen, ob die Verbindung steht und bluetoothGatt nicht null ist
val gatt = bluetoothGatt val gatt = bluetoothGatt
if (gatt == null) { if (gatt == null) {
@@ -136,7 +146,7 @@ class BleManager(private val context: Context) {
} }
// 2. Characteristic holen // 2. Characteristic holen
val characteristic = service.getCharacteristic(TIME_CHARACTERISTIC_UUID) val characteristic = service.getCharacteristic(CHARACTERISTIC_UUID)
if (characteristic == null) { if (characteristic == null) {
Log.d("ESP32_BLE", "Fehler: Characteristic UUID nicht gefunden!") Log.d("ESP32_BLE", "Fehler: Characteristic UUID nicht gefunden!")
return return
@@ -155,4 +165,21 @@ class BleManager(private val context: Context) {
Log.d("ESP32_BLE", "Schreibvorgang gestartet: $success") Log.d("ESP32_BLE", "Schreibvorgang gestartet: $success")
} }
} }
fun syncTime() {
val formatterTime = DateTimeFormatter.ofPattern("HH mm ss")
val formattedTime = currentTime.format(formatterTime)
val formatter = DateTimeFormatter.ofPattern("dd MM yyyy")
val formattedDate = currentDate.format(formatter)
Log.d("Time/Date","$formattedDate $formattedTime")
CoroutineScope(Dispatchers.IO).launch {
writeToEsp32(formattedTime, deviceUUIDs.SERVICE_UUID, deviceUUIDs.CHAR_TIME_UUID)
delay(150.milliseconds) // Kurze Pause für den BLE-Stack (z. B. 150 ms)
writeToEsp32("$formattedDate", deviceUUIDs.SERVICE_UUID, deviceUUIDs.CHAR_DATE_UUID)
}
}
} }
@@ -4,5 +4,7 @@ import java.util.UUID
object DeviceUUIDs { object DeviceUUIDs {
val SERVICE_UUID: UUID? = UUID.fromString("791cadd7-de87-49f9-8986-94b7ae11940b") val SERVICE_UUID: UUID? = UUID.fromString("791cadd7-de87-49f9-8986-94b7ae11940b")
val TIME_CHARACTERISTIC_UUID: UUID? = UUID.fromString("3e4eac5b-eaa2-4e0c-a705-dcac4f606e02") val CHAR_MESSAGE_UUID: UUID? = UUID.fromString("3e4eac5b-eaa2-4e0c-a705-dcac4f606e01")
val CHAR_TIME_UUID:UUID? = UUID.fromString("3e4eac5b-eaa2-4e0c-a705-dcac4f606e02")
val CHAR_DATE_UUID:UUID? = UUID.fromString("3e4eac5b-eaa2-4e0c-a705-dcac4f606e03")
} }
@@ -52,7 +52,7 @@ fun ControlPanelScreen(
onClick = { onClick = {
if (textInput.isNotEmpty()) { if (textInput.isNotEmpty()) {
bleManager.writeToEsp32( bleManager.writeToEsp32(
textInput, DeviceUUIDs.SERVICE_UUID, DeviceUUIDs.TIME_CHARACTERISTIC_UUID textInput, DeviceUUIDs.SERVICE_UUID, DeviceUUIDs.CHAR_DATE_UUID
) )
textInput = "" // Feld danach leeren textInput = "" // Feld danach leeren
} }
@@ -64,6 +64,17 @@ fun ControlPanelScreen(
Spacer(modifier = Modifier.height(32.dp)) Spacer(modifier = Modifier.height(32.dp))
Button(
onClick = {
bleManager.syncTime()
},
modifier = Modifier.fillMaxWidth()
) {
Text("sync Time")
}
Spacer(modifier = Modifier.height(64.dp))
// Disconnect Button (z.B. in Rot, um ihn abzusetzen) // Disconnect Button (z.B. in Rot, um ihn abzusetzen)
Button( Button(
onClick = onDisconnectClicked, onClick = onDisconnectClicked,