diff --git a/app/src/main/java/com/pulseloop/ring/JringDriver.kt b/app/src/main/java/com/pulseloop/ring/JringDriver.kt index aad1b9fa..daedc6ca 100644 --- a/app/src/main/java/com/pulseloop/ring/JringDriver.kt +++ b/app/src/main/java/com/pulseloop/ring/JringDriver.kt @@ -246,12 +246,19 @@ class JringSyncEngine( val mode = encoder.jringSportMode(activityType) workoutSportMode = mode writer?.enqueue(encoder.makeWorkoutHeartRateStartCommand(mode)) + log("jring workout start: sport mode $mode, continuous") } else { workoutSportMode = null startHeartRate() + log(if (bandCapabilities == null) "jring workout start: 180 s check until the ring reports its features (0x20)" + else "jring workout start: 180 s check (ring doesn't set bit 56)") } } + private fun log(message: String) { + PulseEventBus.publishBlocking(PulseEvent.Diagnostic(message, RingDeviceType.JRING)) + } + override fun stopWorkoutHeartRate() { workoutActivityType = null val mode = workoutSportMode ?: return stopHeartRate() diff --git a/app/src/main/java/com/pulseloop/service/RingSyncCoordinator.kt b/app/src/main/java/com/pulseloop/service/RingSyncCoordinator.kt index 544f42f3..993d9541 100644 --- a/app/src/main/java/com/pulseloop/service/RingSyncCoordinator.kt +++ b/app/src/main/java/com/pulseloop/service/RingSyncCoordinator.kt @@ -228,6 +228,9 @@ class RingSyncCoordinator( private val combinedMeasureSeconds = COMBINED_MEASURE_SECONDS.toLong() companion object { + /** A connected ring silent this long mid-workout gets its start again. */ + const val WORKOUT_SILENCE_MS = 60_000L + const val WORKOUT_WATCHDOG_TICK_MS = 15_000L /** Duration of a combined spot measurement (0x23→0x24); also drives the UI countdown. */ const val COMBINED_MEASURE_SECONDS = 45 /** Default window for the live-HR leg of a spot measurement. A family that ends its own @@ -430,20 +433,64 @@ class RingSyncCoordinator( /** The activity type of the workout whose stream is running — what a restart re-sends. */ private var workoutActivityType: String = "other" + /** A workout is recording and wants its heart-rate stream, whether or not the ring is + * connected right now. [workoutHRActive] is whether the stream was started on this link. */ + private var workoutWanted = false + /** When the workout stream last produced a sample, or was last (re)started. */ + @Volatile private var lastWorkoutSampleAt = 0L + private var workoutWatchdog: kotlinx.coroutines.Job? = null + fun startWorkoutHeartRate(activityType: String = "other") { - if (!isConnected) return workoutActivityType = activityType - engine?.startWorkoutHeartRate(activityType) - workoutHRActive = true + workoutWanted = true + startWorkoutWatchdog() + // Not connected yet: the stream starts when the ring connects (see DeviceStateChanged). + if (!isConnected) { workoutLog("Workout started while the ring is disconnected; stream waits for the connection"); return } + sendWorkoutStart("start") } fun stopWorkoutHeartRate() { + workoutWanted = false + workoutWatchdog?.cancel(); workoutWatchdog = null if (!workoutHRActive) return engine?.stopWorkoutHeartRate() workoutHRActive = false + workoutLog("Workout heart rate stopped") engine?.syncVitalsHistory() } + private fun sendWorkoutStart(reason: String) { + engine?.startWorkoutHeartRate(workoutActivityType) + workoutHRActive = true + lastWorkoutSampleAt = System.currentTimeMillis() + workoutLog("Workout heart rate $reason ($workoutActivityType)") + } + + /** + * The 17 Nov gym session: the ring's own minute log shows live readings for the first four + * minutes and then only its routine checks, so the ring stopped measuring and nothing asked + * it to start again. The vendor's run screen re-sends its start whenever the ring reconnects + * (`RunRecordActivity`, BLE state 2 → `startHeartrateMode(true)`); this also re-sends it when + * a connected ring has gone quiet for [WORKOUT_SILENCE_MS], whatever stopped it. Colmi's sport + * session ignores a repeated start mid-session, so this is safe for it too. + */ + private fun startWorkoutWatchdog() { + if (workoutWatchdog?.isActive == true) return + workoutWatchdog = scope.launch { + while (workoutWanted) { + kotlinx.coroutines.delay(WORKOUT_WATCHDOG_TICK_MS) + if (!workoutWanted || !isConnected) continue + if (System.currentTimeMillis() - lastWorkoutSampleAt >= WORKOUT_SILENCE_MS) { + sendWorkoutStart("restarted after ${WORKOUT_SILENCE_MS / 1000} s of silence") + } + } + } + } + + private fun workoutLog(message: String) { + PulseEventBus.publishBlocking(PulseEvent.Diagnostic(message, client.state.value.activeDeviceType)) + } + /** * Ported from iOS's `restartWorkoutHeartRateIfActive()` (RingSyncCoordinator.swift:418). * A spot read's stop also tears down the realtime stream (Colmi stops both 0x69 and 0x1e), @@ -1033,6 +1080,7 @@ class RingSyncCoordinator( is PulseEvent.LiveSampleGate -> Unit // our own; consumed by EventPersistenceSubscriber is PulseEvent.HeartRateSample -> { if (event.spot) return // our own settled reading, not a ring sample + if (workoutWanted && event.bpm > 0) lastWorkoutSampleAt = System.currentTimeMillis() // During a spot measure the window decides what counts: a sample it rejects is the // ring's cached echo (a bpm from hours ago, stamped now), so it must not become the // live value either — that echo is exactly the number issue #59 saw on the card @@ -1120,12 +1168,20 @@ class RingSyncCoordinator( } is PulseEvent.DeviceStateChanged -> { when (event.state) { - RingConnectionState.CONNECTED -> lastSyncAt = System.currentTimeMillis() + RingConnectionState.CONNECTED -> { + lastSyncAt = System.currentTimeMillis() + // Only the BLE client's own connect carries a deviceType; decoder Status + // replies also map to CONNECTED and recur all session (see AGENTS.md). + // A real (re)connect mid-workout restarts the stream, as the vendor does. + if (event.deviceType != null && workoutWanted) sendWorkoutStart("restarted after reconnect") + } RingConnectionState.DISCONNECTED, RingConnectionState.FAILED, RingConnectionState.IDLE -> { clearSyncProgress() startupEngine = null + if (workoutWanted && workoutHRActive) workoutLog("Ring disconnected during a workout") + workoutHRActive = false } else -> {} } diff --git a/app/src/main/java/com/pulseloop/service/RingSyncWorker.kt b/app/src/main/java/com/pulseloop/service/RingSyncWorker.kt index 917d0a09..a16a22ee 100644 --- a/app/src/main/java/com/pulseloop/service/RingSyncWorker.kt +++ b/app/src/main/java/com/pulseloop/service/RingSyncWorker.kt @@ -83,6 +83,12 @@ class RingSyncWorker( // runs startup sync anyway so history is still pulled without fighting over GATT (issue #103). if (ScheduledSpo2Worker.isMeasuring) return Result.success() + // A workout is recording with the screen off: the app's own client holds the ring and its + // heart-rate stream. This worker would open a second client, re-run the whole startup + // (settings writes included) and then broadcast DISCONNECTED, which is the opposite of + // what a workout needs. The vendor runs no sync during its run screen either. + if (com.pulseloop.AppServices.peek()?.liveWorkout?.state?.value?.isRecording == true) return Result.success() + val keyStore = ApiKeyStore(applicationContext) val db = PulseLoopDatabase.getInstance(applicationContext) diff --git a/docs/jring-56ff-plan.md b/docs/jring-56ff-plan.md index 5dd67b2e..9d903118 100644 --- a/docs/jring-56ff-plan.md +++ b/docs/jring-56ff-plan.md @@ -410,3 +410,23 @@ two minutes. Source: JRing 2.0.8 (`com.jaga.ibraceletplus.jyring`), decompiled locally with jadx 1.5.1 into the gitignored `decompiled-jring-offical/`. + +**Follow-up, 17 Nov gym session (2.16.17).** +- **What the export showed:** the summary still read 76 bpm, with readings covering 12 of 57 + minutes. The export (`-full`, unmasked) only held the four minutes before it was taken, but it + included the ring's own minute log for the morning: + - 06:56–07:00: live, varying readings, 71–89 bpm; + - 07:00 onwards: only the routine single checks, 71 at 07:00, 75 at 07:30, 77 at 07:44. +- **So the ring stopped measuring about four minutes in, and nothing asked it again.** What + stopped it isn't in the export. Two candidates: a start sent before the ring's `0x20` reply, + which falls back to the 180 s check, or a later command ending the live mode. +- **What changed (`RingSyncCoordinator`):** + - **Reconnect:** a real reconnect mid-workout re-sends the start, as the vendor's run screen + does on BLE state 2. + - **Started while disconnected:** a workout started with the ring away now starts the stream + when it connects (the start used to be dropped). + - **Silence:** a connected ring silent for 60 s mid-workout gets the start again. +- **Background sync (`RingSyncWorker`):** it no longer runs during a recording. It used to open + a second client, re-run the whole startup and broadcast DISCONNECTED. +- **Diagnostics:** each start, restart and stop goes to the diagnostics log, with the command + jring chose. The next export then says which start was sent and when the ring went quiet.