time sync with esp32
This commit is contained in:
1 parent
21883af93b
commit
f8337eef46
6 files changed
+53
-6
No files matched your search
@@ -18,9 +18,19 @@ import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
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 kotlin.time.Duration.Companion.milliseconds
|
||||
|
||||
class BleManager(private val context: Context) {
|
||||
val currentTime = LocalTime.now()
|
||||
val currentDate = LocalDate.now()
|
||||
val deviceUUIDs = DeviceUUIDs
|
||||
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")
|
||||
if (status == BluetoothGatt.GATT_SUCCESS) {
|
||||
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.")
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
val gatt = bluetoothGatt
|
||||
if (gatt == null) {
|
||||
@@ -136,7 +146,7 @@ class BleManager(private val context: Context) {
|
||||
}
|
||||
|
||||
// 2. Characteristic holen
|
||||
val characteristic = service.getCharacteristic(TIME_CHARACTERISTIC_UUID)
|
||||
val characteristic = service.getCharacteristic(CHARACTERISTIC_UUID)
|
||||
if (characteristic == null) {
|
||||
Log.d("ESP32_BLE", "Fehler: Characteristic UUID nicht gefunden!")
|
||||
return
|
||||
@@ -155,4 +165,21 @@ class BleManager(private val context: Context) {
|
||||
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 {
|
||||
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 = {
|
||||
if (textInput.isNotEmpty()) {
|
||||
bleManager.writeToEsp32(
|
||||
textInput, DeviceUUIDs.SERVICE_UUID, DeviceUUIDs.TIME_CHARACTERISTIC_UUID
|
||||
textInput, DeviceUUIDs.SERVICE_UUID, DeviceUUIDs.CHAR_DATE_UUID
|
||||
)
|
||||
textInput = "" // Feld danach leeren
|
||||
}
|
||||
@@ -64,6 +64,17 @@ fun ControlPanelScreen(
|
||||
|
||||
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)
|
||||
Button(
|
||||
onClick = onDisconnectClicked,
|
||||
|
||||
Reference in new issue
Block a user