1

So I have been trying to collect and measure heart rate data from an android smartwatch (Wear OS 4), the problem is that I need the sampling to be somewhat constant, but when the smartwatch goes to sleep, the sampling rate for the heart rate sensor stops sampling, presumably because of the device enters doze. When in this mode I would receive one single data point per 15 minutes.

I would ideally receive as much data asleep as awake, or a longer window for sampling (say 90 sec). The application is purely for research/scientific purposes, and not meant for public use, therefore energy usage is not a priority (yet).

Currently I'm using a repository (from: https://github.com/android/health-samples/tree/main/health-services/MeasureDataCompose) where a callback is registered on the heart rate sensor:

fun heartRateMeasureFlow() = callbackFlow {
        val callback = object : MeasureCallback {
            override fun onAvailabilityChanged(
                dataType: DeltaDataType<*, *>,
                availability: Availability
            ) {
                // ...
            }

            override fun onDataReceived(data: DataPointContainer) {
                val heartRateBpm = data.getData(DataType.HEART_RATE_BPM)
                trySendBlocking(MeasureMessage.MeasureData(heartRateBpm))
            }
        }

        measureClient.registerMeasureCallback(DataType.HEART_RATE_BPM, callback)

        awaitClose {
            Log.d(TAG, "Unregistering for data")
            runBlocking {
                measureClient.unregisterMeasureCallbackAsync(DataType.HEART_RATE_BPM, callback)
                    .await()
            }
        }
    }

I'm also running a foreground service that listens to the flow created here, and for now, only displays it in the notification:

repo.heartRateMeasureFlow()
                .collect {
                    when (it) {
                        is MeasureMessage.MeasureData -> {
                            // Notify
                        }
                        // ...
                    }
                }

As stated above, when testing this on physical devices (and on emulators), the sampling is frequent (once a second), but as soon as the device goes to sleep or the screen goes black the sampling is too infrequent to be of any use. The callback is never unregistered, so I assume the OS is simply not allowing the sampling to occur too often when asleep (understandably). The thread listening is awake when the device is asleep as it's in a foreground service.

So the final question is:

Is there a way to take control over the sensor, the sensor's sampling frequency, or force a manual sampling for heart rate, even when the device is sleeping/in doze?

Again, I know this will have an impact on the battery life of the device but assume it is of no importance.

1
  • Did you aquire any knowledge from other sources regarding this issue? Commented Sep 2, 2024 at 13:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.