health-samples icon indicating copy to clipboard operation
health-samples copied to clipboard

MeasureData and ExerciseSample - both don't measure BPM rate when screen is off

Open mommek opened this issue 2 years ago • 13 comments

Hi, I am trying MeasureData and ExerciseSample because I need create an app that measure your BPM rate every second and check if is above or below the limit (if has - alarm should appear).

When you have MeasureData app open - it is working and showing your actual BPM rate. But when you tilt your wrisp and screen goes off - it will stop measure. Same as ExerciseSample. I found it out in log. I am connected with real device to AndroidStudio and checking log. When screen is off - log stop showing bpm rate.

Is there any way (ideally for MeasureData) to keep alive measuring BPM when display is off?

mommek avatar Jul 14 '22 07:07 mommek

Hi,

I observed this in the MeasureData sample, but not in the ExerciseSample.

MeasureData uses MeasureClient, which is meant to receive data while the app is in use, and not intended for background capture. Using MeasureClient for an extended period of time also increases power consumption. long would you need it to monitor BPM?

breanatate avatar Jul 15 '22 13:07 breanatate

Hi,

I need measure BPM minimum for 8 hours without stopping (one work shift). So foreground app (with screen ON) is not possible according battery life. So if this will work also when screen is off, that would be great.

mommek avatar Jul 15 '22 13:07 mommek

Both MeasureClient and ExerciseClient require a registered listener. These updates are delivered in batches, and to measure BPM every second for 8 hours would significantly drain the battery. You could have a foreground service running in the background (ExerciseSample does this), but the battery drain would still be significant.

Have you considered using Passive Data?

breanatate avatar Jul 15 '22 14:07 breanatate

Yes I also try Passive Data, but it seems they update BPM rate once upon a time (dont know exactly timing) but when I try it, it will check my BPM, show and this same number (f.e. 89) was on screen few minutes. I really need non-stop checking (or at least once per 5-10 seconds) BPM rate and I know that battery will make it, when screen will be OFF. I create the same app two years ago for samsung watch, but this was in Tizen OS (.NET xamarin) and it work very well with screen off. Now it is new Android Kotlin Wear OS system and I dont figure it out, how to simply do it.

mommek avatar Jul 15 '22 14:07 mommek

You can use PassiveMonitoringClient with continuous heart rate monitoring. You should be able to get an alert when HR is above/below a certain threshold for so long.

In order to get HR updates every second, you (or your users) will need to enable Continuous Heart Rate Monitoring for the device in the Samsung Health App.

breanatate avatar Jul 15 '22 16:07 breanatate

Hi,

Checking back in with more things to consider.

If your use-case is to detect an abnormal HR, then it would be best to use the alert we have built into Wear Health Services (PassiveMonitoringClient.registerHealthEvent), since it's not ideal to alert a user for one abnormal reading in most circumstances (i.e. limit is 85 BPM, and one second it's 87, then back to 84).

Would this work for your use-case?

breanatate avatar Jul 15 '22 22:07 breanatate

This app is more complex (checking GPS position, inactivity, if user take off his watch and so on) but this things are OK, working and the last thing is monitoring BPM rate. This app is also in kiosk mode, so when you turn on your watch, this app is autoopen and you can't open any other app and you can't close this app.

Let's say that we set min. to 40 and max to 150. Normal rate through the day is 80-90, sometimes 75 sometimes 110 and this number is changing every second. But when someone reach 150 and more, my app is open new activity with ALERT text, sound and vibrate (custom alarm). If user turn off this alert with special button in 10 seconds, alert is off. This mean the user is OK. If no, this app will start sendind alert SMS and starting calling number you setted.

This is why I need monitoring user BPM rate ideally every second, but is also possible for example every 3-5 seconds. Very important is so that it works even when the display is off because of battery life. This is the last thing what I need to solve, everything working great, but when display is off, it simply stop measure your BPM. When you look at watch (or click on display) and display is ON, it will immediately start measure your bpm again. This is problem for me, because as I said before - in my code I checking your BPM rate and if it is above the limites, new activity ALERT page start. But when display is off, BPM is still the same (for example 80) and when you let's say start workout and display is still off, the BPM rate in background is still 80. After you look on watch or turn screen on, after one-two second it will start measure and suddenly appears your real BPM (for example 120).

mommek avatar Jul 16 '22 09:07 mommek

PassiveMonitoringClient will work when the screen is off and will work in conjunction with other actions. If continuous heart rate monitoring is enabled on the device, you should be able to get updates at the desired frequency.

breanatate avatar Jul 18 '22 16:07 breanatate

But you are talking about Health App already build in WearOS? Or about PassiveData https://github.com/android/health-samples/tree/main/health-services/PassiveData ? Because I try this PassiveData and I don't figure it our, how to change frequency update. I install this app to my Galaxy Watch 4, open and I get actual data (85 BPM) and this number 85 will show for 4-5 minutes straight.

mommek avatar Jul 18 '22 20:07 mommek

You would need to integrate the parts of PassiveMonitoringClient into your existing app. We don't currently have one built as a sample with this exact functionality. For changing the monitoring frequency, if you have a Galaxy Watch, you can enable Continuous Heart Rate Monitoring from Settings > Samsung Health > Heart Rate > Measure continuously.

breanatate avatar Jul 18 '22 21:07 breanatate

You mean this code https://github.com/android/health-samples/tree/main/health-services/PassiveData/app/src/main/java/com/example/passivedata or PassiveMonitoringClient from "build-in" app already in Samsung Health? Because I dont think that build-in-app from Samsung is open source. So I will try PassiveData app with Measure continuously ON.

mommek avatar Jul 19 '22 07:07 mommek

Yes, the first link you sent. The PassiveData sample uses the PassiveMonitoringClient (documentation here), but I don't think using just the sample will satisfy your use-case completely.

breanatate avatar Jul 19 '22 13:07 breanatate

Thank you, I will try this next week.

mommek avatar Jul 21 '22 05:07 mommek

Sorry re-opening this issue, but I'm having a similar issue trying to send the heart rate from background to a socket server. Do you have some suggestion to make it work? Thanks in advance!

mauricio-lorenzetti avatar Dec 08 '23 04:12 mauricio-lorenzetti

@mauricio-lorenzetti for which client? ExerciseClient or PassiveMonitoringClient?

If you are using ExerciseClient to record an active exercise, you can use BatchingModes to receive new HR data every 5 seconds.

For PassiveMonitoringClient there aren't any hard guarantees for data frequency. You can read more here.

breanatate avatar Dec 08 '23 14:12 breanatate

@breanatate Thanks for your reply, I'm actually trying it with PassiveData fetching the heart rate. The data frequency is an potential issue for sure, but we're not addressing it yet. My main problem right now is that I could not call something like that: val mSocket = SocketHandler.getSocket() mSocket.emit("heart_rate", it) from PassiveDataService's onNewDataPointsReceived method, apparently from there I'm not allowed to make network usage or the socket that I opened before is already inactive from the background perspective. Any idea?

mauricio-lorenzetti avatar Dec 08 '23 17:12 mauricio-lorenzetti