Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

swift - iOS get heart rate from Apple Watch in near real time

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...