27

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

Community
  • 1
  • 1
user2529173
  • 1,884
  • 6
  • 30
  • 43
  • Wouldn't that deplete the battery real quick? I mean to send data every 5 seconds or so –  Jan 27 '16 at 15:13
  • 1
    It would be just a quick GET request to a server sending a date and the heart rate and it for a tennis match, so it won't be used that long – user2529173 Jan 27 '16 at 15:16
  • @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. – A-Live Jan 27 '16 at 15:26
  • 1
    The app is made for a special group of people, there will be like 10 using it. So I don't worry about those things. I tried background fetches, but it doesn't seem to work reliably – user2529173 Jan 27 '16 at 15:37
  • @user2529173 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? – Saviz Mar 10 '16 at 04:52
  • 1
    Yes you can execute code during the measurements, but there is no guarantee from the watchOS that this measurement takes place every five seconds. – user2529173 Mar 10 '16 at 08:32
  • @user2529173 Did you manage to implement this? I am looking for the same solution. Can you please help me with the code snippet? – Vidhya Sri Jul 11 '18 at 06:45
  • unfortunately this was two years ago and I'm working in a different company now, so I don't have access to the code. I have followed the answer below and actually worked quite well – user2529173 Jul 11 '18 at 09:13

2 Answers2

27

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.

Hugo Alonso
  • 6,684
  • 2
  • 34
  • 65
  • Thank you for your answer. I've been afraid of this answer. Seems the project can't be done with Apple Watch. Anyways, thank you – user2529173 Jan 28 '16 at 07:53
  • I know the feeling. You will have to pivot and maybe wait for next release of **Watch OS**. Maybe they decide to add **HKObserver** capability on Watch's HealthKit – Hugo Alonso Jan 28 '16 at 22:26
  • 2
    Hugo's answer is correct and reveals on the serious limitations of the Apple Watch, and it's true for all other sensors too like accelerometer. Actually it's quite useless gadget now without real time sensors...you have to wait for new watchOS releases or new device or just use a wearable for some bucks. – BootMaker May 16 '16 at 08:12
  • 5
    To be fair, 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 the app is in the background (dark screen on watch) – BootMaker Feb 20 '17 at 09:28
  • @BootMaker you are right about this. I'll update my answer for future references. Thanks for making me notice this outdated answer. – Hugo Alonso Feb 20 '17 at 09:34
  • @HugoAlonso : Can you give tutorial that works same i.e update heart rate in 10 min using watch OS?? – Abhishek Thapliyal Apr 24 '18 at 07:40
  • Hi @AbhishekThapliyal. Right now I have no availability. But I think you could find something around the web, has been a while since this technology is available. – Hugo Alonso Apr 25 '18 at 19:58
  • @Hugo Alonso Can someone pls post some sample code on how to get heart rate in watch app even when watch app is locked(Got black screen) – Vidhya Sri Jul 11 '18 at 07:34
21

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)

nhathm
  • 343
  • 2
  • 5
  • 1
    @RiV Do you have any sample code or reference link ? – Dhaval Bhimani Aug 28 '19 at 06:43
  • 8
    @DhavalBhimani here is my sample: https://github.com/nhathm/swift_heart_rate_real_time/ – nhathm Aug 28 '19 at 09:17
  • 2
    Thanks for the sample, @nhathm, but you forgot to call the completion in your HKObserverQuery as stated mandatory by the documentation at https://developer.apple.com/documentation/healthkit/hkhealthstore/1614175-enablebackgrounddelivery. – appleitung Feb 16 '20 at 20:04
  • @nhathm Will this code works on simulator? – Hardik1344 Jan 06 '22 at 16:54
  • @Hardik1344 did you try it out? It does work on the simulator. But I have no access to an actual device so all I'm getting is updates to HR every 4-5secs – app4g Nov 27 '22 at 09:38