phone: poll the connected watch on an interval so sync keeps pulling new records
- Commit
09219780bd07ab7509e3d503e9a2718805d3ff59- Author
- Vayun Mathur <[email protected]>
- When
- 2026-07-07 00:39 UTC (5 months ago)
- Parents
4fa48c2
Diff
watch/phone/src/main/java/com/vayunmathur/watch/phone/ble/GattClientManager.kt
diff --git a/watch/phone/src/main/java/com/vayunmathur/watch/phone/ble/GattClientManager.kt b/watch/phone/src/main/java/com/vayunmathur/watch/phone/ble/GattClientManager.ktindex a76e9a64cf..75837e24e6 100644--- a/watch/phone/src/main/java/com/vayunmathur/watch/phone/ble/GattClientManager.kt+++ b/watch/phone/src/main/java/com/vayunmathur/watch/phone/ble/GattClientManager.kt@@ -188,6 +188,19 @@ class GattClientManager(private val context: Context) { ) } + /**+ * Polls the watch for new data. The watch only streams on subscribe or on a+ * read of the Data characteristic, so reading it here makes the watch re-stream+ * any rows accumulated (and not yet acked) since the last sync.+ */+ fun requestSync() {+ val g = gatt ?: return+ val service = g.getService(BleConstants.SERVICE_UUID) ?: return+ val data = service.getCharacteristic(BleConstants.DATA_CHARACTERISTIC_UUID) ?: return+ reassembly.reset()+ g.readCharacteristic(data)+ }+ private fun finishBatch(gatt: BluetoothGatt) { val bytes = reassembly.toByteArray() reassembly.reset()
watch/phone/src/main/java/com/vayunmathur/watch/phone/sync/SyncForegroundService.kt
diff --git a/watch/phone/src/main/java/com/vayunmathur/watch/phone/sync/SyncForegroundService.kt b/watch/phone/src/main/java/com/vayunmathur/watch/phone/sync/SyncForegroundService.ktindex e39dd1f5d4..70e9806f86 100644--- a/watch/phone/src/main/java/com/vayunmathur/watch/phone/sync/SyncForegroundService.kt+++ b/watch/phone/src/main/java/com/vayunmathur/watch/phone/sync/SyncForegroundService.kt@@ -19,8 +19,10 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.cancel+import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow+import kotlinx.coroutines.isActive import kotlinx.coroutines.launch /**@@ -62,6 +64,17 @@ class SyncForegroundService : Service() { syncedCount.value += records.size } }++ // The watch only streams on subscribe or on a Data read, so poll it while+ // connected to keep pulling newly-recorded rows.+ scope.launch {+ while (isActive) {+ delay(POLL_INTERVAL_MS)+ if (connectionState.value == ConnectionState.Connected) {+ client.requestSync()+ }+ }+ } } private suspend fun processBatch(records: List<WatchRecord>) {@@ -129,6 +142,8 @@ class SyncForegroundService : Service() { private const val NOTIFICATION_ID = 1 // Rolling buffer window (~3 days) used for daily/overnight derivations. private const val BUFFER_WINDOW_MS = 3L * 24 * 60 * 60 * 1000+ // How often to poll the connected watch for newly-recorded rows.+ private const val POLL_INTERVAL_MS = 20_000L val connectionState = MutableStateFlow(ConnectionState.Disconnected) val syncedCount = MutableStateFlow(0)