iOS get heart rate from Apple Watch in near real time
Asked Answered
T

2

30

I need to make an app that records heart rate data in near real time and send this data to a server as soon as possible.

First I took this approach: Watch os 2.0 beta: access heart beat rate

In fact it is working fine. There is new heart rate data in the HealthKit every five seconds. But now I have the problem that I can't sync that with a server.

My first approach was the Watch app. The watch was sending data to a server. That doesn't work because as soon as the screen turns black on the watch, it stops sending.

My next approach was to query the HealthKit on the iPhone every five seconds for new data. This works, as long as the app is in foreground.

Then I saw that there's some kind of background functionality that watches the HealthKit itself and revokes the app from background and you can do something.(enableBackgroundDeliveryForType) This doesn't seem to work for heart rate (the Apple Documentation says for things like steps this doesn't work, I guess heart rate is one of those).

I'm stuck now. Do you know how to it? I would need some background task that is executed every 5-10 seconds on the iPhone. That seems to be impossible

Tombouctou answered 27/1, 2016 at 14:14 Comment(3)
@user2529173, you think it won't take long, but a user with tens of such apps will have to send 2 requests a second to arbitrary servers with no guaranteed low latency or optimal routes, they are not even guaranteed to be alive and the requests can easily be hanged until timeout. Try to use background fetches in combination with health kit, the system will decide when the app should be allowed to access the web, it is not clearly documented but I believe the intervals would depend on how frequently the app is used and how fast it really executes, ofc on top of a general device state.Aube
@Tombouctou can you execute any code using updatehandler when the data is available in the 5 second intervals when the data is updated? How do you know that the data is being updated every 5 seconds?Cormier
Yes you can execute code during the measurements, but there is no guarantee from the watchOS that this measurement takes place every five seconds.Tombouctou
E
28

UPDATE

As noticed by @BootMaker, Apple made background mode available for HKWorkout apps in WatchOS 3, so it's working now. You have to run a HKWorkoutSession and this will keep your heartrate delivery in real time even when the app is in the background (dark screen on watch)

The closest you are going to be is while the watch app is open.

Why I'm stating this?

  1. There are two HealthKit's Database (one at the iPhone and another at the Apple Watch). When they sync is arbitrary and decided only by the O.S.

  2. The closest you are going to be to real time is when you don't have any password locking your screen in iPhone or Apple Watch. Either way, there's no guarantee that the sync will happen every time a new measure is added to Apple Watch's HealthKit

  3. The only way to force the Heart rate sensor into working in real time is via workouts or observer while your Apple Watch app is in FOREGROUND.

  4. Background delivery is NOT available for Apple Watch apps.

  5. Watch OS 2 request the sensor to measure automatically (in background) every 10 minutes minimum.

There's no other workaround, if you need real time for longer periods, or while the user is not using your app, you will need to use an specialized wearable.

Eschar answered 27/1, 2016 at 16:2 Comment(0)
A
26

If anyone still need to get heart rate or other data in real time. Use this solution:

  1. Develop an apple watch app/extention
  2. In watch app, using HKHealthStore, HKWorkoutConfiguration, HKWorkoutSession, HKLiveWorkoutBuilder to create an Workout. After create workout, your watch app will get heart rate in real time.
  3. Using watch kit connection with WCSession to send data to iPhone app.
  4. Enable background mode both in apple watch and iPhone.

I tested, even app terminated, we can still get heart rate (I used Local notification for posting heart rate data for debugging)

Airbrush answered 13/8, 2019 at 2:41 Comment(2)
@DhavalBhimani here is my sample: github.com/nhathm/swift_heart_rate_real_timeAirbrush
Thanks for the sample, @nhathm, but you forgot to call the completion in your HKObserverQuery as stated mandatory by the documentation at developer.apple.com/documentation/healthkit/hkhealthstore/….Particulate

© 2022 - 2024 — McMap. All rights reserved.