IOS receives Apple Watch's heart rate in near real time

I need to make an application that records heart rate data in the near real time and sends this data to the server as soon as possible.

At first I took this approach: Watch os 2.0 beta: access your heart rate

It actually works fine. HealthKit introduces new heart rate data every five seconds. But now I have a problem that I cannot synchronize this with the server.

My first approach is the Watch app. The clock sent data to the server. This does not work, because as soon as the screen turns black on the watch, it stops sending.

My next approach was to request HealthKit on the iPhone every five seconds for new data. This works if the application is in the foreground.

Then I saw that there is some kind of background functionality that monitors HealthKit itself and cancels the application from the background, and you can do something. (enableBackgroundDeliveryForType) This doesn't seem to work for heart rate (Apple Documentation talks about things like steps, it doesn't work, I think heart rate is one of those).

I'm stuck now. Do you know how to do this? I will need a background task that runs every 5-10 seconds on an iPhone. It seems impossible

+9
source share
2 answers

UPDATE

As @BootMaker noted, Apple made the background available for HKWorkout apps in WatchOS 3 , so now it works. You need to run HKWorkoutSession , and this will ensure your hearty delivery in real time, even if the application is in the background (dark screen on the clock)

The closest you will be , while the viewer application is open .

Why am I talking about this?

  • There are two HealthKit databases (one on the iPhone and the other on the Apple Watch). When they are synchronized arbitrarily and only OS resolves

  • The closest you will be in real time when you do not have a password blocking your screen on the iPhone or Apple Watch . In any case, there is no guarantee that synchronization will occur every time a new measure is added to Apple Watch HealthKit

  • The only way to get the heart rate sensor to work in real time is to workout or observer , while > the Apple Watch app is in FOREGROUND .

  • Background delivery is NOT available for Apple Watch apps.

  • Observe OS 2 so that the sensor automatically measures ( in the background ) every 10 minutes.

There is no other workaround, if you need real time for longer periods of time, or until the user uses your application, you will need to use a specialized wearable .

+17
source

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

  1. Build an Apple Watch App / Extension
  2. In the watch app, using HKHealthStore, HKWorkoutConfiguration, HKWorkoutSession, HKLiveWorkoutBuilder to create a workout. After creating a workout, your watch application will receive a real-time heart rate.
  3. Using a dial set connection with WCSession to send data to an iPhone application.
  4. Turn on background mode on both the Apple Watch and the iPhone.

I checked, even the application is closed, we can still get the heart rate (I used local notification to publish heart rate data for debugging)

+1
source

All Articles