Modern Apps git

← Modern-Apps.git

watch: collect barometer and motion signals on the watch

Commit
9e5182e1e86686c7fe62dc6f4553d596e822aa20
Author
Vayun Mathur <[email protected]>
When
2026-07-06 02:21 UTC (5 months ago)
Parents
abb8efe

Diff

watch/phone/src/main/java/com/vayunmathur/watch/phone/data/WatchPayload.kt
diff --git a/watch/phone/src/main/java/com/vayunmathur/watch/phone/data/WatchPayload.kt b/watch/phone/src/main/java/com/vayunmathur/watch/phone/data/WatchPayload.ktindex c8a04b6cbc..db73fdd260 100644--- a/watch/phone/src/main/java/com/vayunmathur/watch/phone/data/WatchPayload.kt+++ b/watch/phone/src/main/java/com/vayunmathur/watch/phone/data/WatchPayload.kt@@ -10,6 +10,7 @@ data class WatchRecord(     val timestamp: Long,     val value: Double,     val delta: Double,+    val stationary: Boolean = false, )  @Serializable
watch/watch/src/main/java/com/vayunmathur/watch/watch/ble/GattServerManager.kt
diff --git a/watch/watch/src/main/java/com/vayunmathur/watch/watch/ble/GattServerManager.kt b/watch/watch/src/main/java/com/vayunmathur/watch/watch/ble/GattServerManager.ktindex cf66f398a2..354c3bb754 100644--- a/watch/watch/src/main/java/com/vayunmathur/watch/watch/ble/GattServerManager.kt+++ b/watch/watch/src/main/java/com/vayunmathur/watch/watch/ble/GattServerManager.kt@@ -33,6 +33,7 @@ private data class WireRecord(     val timestamp: Long,     val value: Double,     val delta: Double,+    val stationary: Boolean = false, )  @Serializable@@ -229,7 +230,7 @@ class GattServerManager(         scope.launch(Dispatchers.IO) {             val rows: List<SensorRecord> = dao.getAll()             val batch = WireBatch(rows.map {-                WireRecord(it.id, it.type.name, it.timestamp, it.value, it.delta)+                WireRecord(it.id, it.type.name, it.timestamp, it.value, it.delta, it.stationary)             })             pendingAck[device.address] = rows.map { it.id } 
watch/watch/src/main/java/com/vayunmathur/watch/watch/data/SensorRecord.kt
diff --git a/watch/watch/src/main/java/com/vayunmathur/watch/watch/data/SensorRecord.kt b/watch/watch/src/main/java/com/vayunmathur/watch/watch/data/SensorRecord.ktindex fbb6cdf5a7..749e9b25e1 100644--- a/watch/watch/src/main/java/com/vayunmathur/watch/watch/data/SensorRecord.kt+++ b/watch/watch/src/main/java/com/vayunmathur/watch/watch/data/SensorRecord.kt@@ -14,6 +14,8 @@ import kotlinx.coroutines.flow.Flow enum class MetricType {     HeartRate,     Steps,+    Pressure,+    Motion, }  @Entity@@ -22,9 +24,12 @@ data class SensorRecord(     val type: MetricType,     val timestamp: Long,     // For HeartRate: the bpm reading. For Steps: cumulative counter value.+    // For Pressure: hPa. For Motion: 1.0 = stationary, 0.0 = moving.     val value: Double,-    // For Steps: increment since the previous reading. For HeartRate: 0.+    // For Steps: increment since the previous reading. Otherwise 0.     val delta: Double = 0.0,+    // For HeartRate: whether the wearer was stationary when sampled.+    val stationary: Boolean = false, )  @Dao
watch/watch/src/main/java/com/vayunmathur/watch/watch/service/SensorBackgroundService.kt
diff --git a/watch/watch/src/main/java/com/vayunmathur/watch/watch/service/SensorBackgroundService.kt b/watch/watch/src/main/java/com/vayunmathur/watch/watch/service/SensorBackgroundService.ktindex da0a3da47b..be56651640 100644--- a/watch/watch/src/main/java/com/vayunmathur/watch/watch/service/SensorBackgroundService.kt+++ b/watch/watch/src/main/java/com/vayunmathur/watch/watch/service/SensorBackgroundService.kt@@ -10,6 +10,8 @@ import android.hardware.Sensor import android.hardware.SensorEvent import android.hardware.SensorEventListener import android.hardware.SensorManager+import android.hardware.TriggerEvent+import android.hardware.TriggerEventListener import android.os.IBinder import android.util.Log import com.vayunmathur.watch.watch.R@@ -38,6 +40,9 @@ class SensorBackgroundService : Service(), SensorEventListener {     private var lastHeartRateAt = 0L     // The step counter reports a cumulative count since boot; we store deltas.     private var lastStepCount = -1.0+    private var lastPressureAt = 0L+    // Current motion state, maintained via the one-shot trigger sensors.+    @Volatile private var isStationary = false      override fun onCreate() {         super.onCreate()@@ -60,6 +65,42 @@ class SensorBackgroundService : Service(), SensorEventListener {         sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)?.let {             sensorManager.registerListener(this, it, SensorManager.SENSOR_DELAY_NORMAL)         }+        sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE)?.let {+            sensorManager.registerListener(this, it, SensorManager.SENSOR_DELAY_NORMAL)+        }+        // Arm the stationary detector; the two trigger sensors re-arm each other.+        armStationaryDetect()+    }++    private fun armStationaryDetect() {+        sensorManager.getDefaultSensor(Sensor.TYPE_STATIONARY_DETECT)?.let {+            sensorManager.requestTriggerSensor(triggerListener, it)+        }+    }++    private fun armMotionDetect() {+        sensorManager.getDefaultSensor(Sensor.TYPE_MOTION_DETECT)?.let {+            sensorManager.requestTriggerSensor(triggerListener, it)+        }+    }++    private val triggerListener = object : TriggerEventListener() {+        override fun onTrigger(event: TriggerEvent) {+            val now = System.currentTimeMillis()+            when (event.sensor.type) {+                Sensor.TYPE_STATIONARY_DETECT -> {+                    isStationary = true+                    persist(SensorRecord(type = MetricType.Motion, timestamp = now, value = 1.0))+                    // Trigger sensors are one-shot; arm the opposite one.+                    armMotionDetect()+                }+                Sensor.TYPE_MOTION_DETECT -> {+                    isStationary = false+                    persist(SensorRecord(type = MetricType.Motion, timestamp = now, value = 0.0))+                    armStationaryDetect()+                }+            }+        }     }      override fun onSensorChanged(event: SensorEvent) {@@ -71,7 +112,14 @@ class SensorBackgroundService : Service(), SensorEventListener {                 // Sample roughly once per minute to conserve battery.                 if (now - lastHeartRateAt < HEART_RATE_INTERVAL_MS) return                 lastHeartRateAt = now-                persist(SensorRecord(type = MetricType.HeartRate, timestamp = now, value = bpm))+                persist(+                    SensorRecord(+                        type = MetricType.HeartRate,+                        timestamp = now,+                        value = bpm,+                        stationary = isStationary,+                    ),+                )             }             Sensor.TYPE_STEP_COUNTER -> {                 val cumulative = event.values.firstOrNull()?.toDouble() ?: return@@ -91,6 +139,13 @@ class SensorBackgroundService : Service(), SensorEventListener {                     ),                 )             }+            Sensor.TYPE_PRESSURE -> {+                val hpa = event.values.firstOrNull()?.toDouble() ?: return+                if (hpa <= 0.0) return+                if (now - lastPressureAt < PRESSURE_INTERVAL_MS) return+                lastPressureAt = now+                persist(SensorRecord(type = MetricType.Pressure, timestamp = now, value = hpa))+            }         }     } @@ -125,6 +180,14 @@ class SensorBackgroundService : Service(), SensorEventListener {      override fun onDestroy() {         sensorManager.unregisterListener(this)+        sensorManager.cancelTriggerSensor(+            triggerListener,+            sensorManager.getDefaultSensor(Sensor.TYPE_STATIONARY_DETECT),+        )+        sensorManager.cancelTriggerSensor(+            triggerListener,+            sensorManager.getDefaultSensor(Sensor.TYPE_MOTION_DETECT),+        )         gattServer.stop()         scope.cancel()         super.onDestroy()@@ -137,6 +200,7 @@ class SensorBackgroundService : Service(), SensorEventListener {         private const val CHANNEL_ID = "sensor_collection"         private const val NOTIFICATION_ID = 1         private const val HEART_RATE_INTERVAL_MS = 60_000L+        private const val PRESSURE_INTERVAL_MS = 30_000L          fun start(context: Context) {             val intent = Intent(context, SensorBackgroundService::class.java)