Measuring heartrate while Apple Watch is in sleep mode [duplicate] - swift

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

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?
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.
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
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.
Background delivery is NOT available for Apple Watch apps.
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.

If anyone still need to get heart rate or other data in real time. Use this solution:
Develop an apple watch app/extention
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.
Using watch kit connection with WCSession to send data to iPhone app.
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)

Related

How to keep an Apple Watch app running when watch is charging

I'm making an Apple Watch app to record user acceleration. Currently, the user configures the session length to record for, and using a HKWorkoutSession, the app records acceleration for that time period.
My issue: I want the user to be able to remove their watch and charge it in the middle of a long session. In debugging the watch on Xcode, when I place the watch on the charger, I get the messages:
Message from debugger: Terminated due to signal 9
Program ended with exit code: 0
And the debugger is no longer connected. After a short while, the HKWorkoutSession ends as well, and the app is no longer recording.
Looking at https://medium.com/#gohnjanotis/apple-watch-battery-level-charging-notifications-eb7d0797a4d8, and the statement "While on the charger it seems these background refresh tasks are paused altogether.", I'm not sure I understand the Watch app life cycle fully. Is it even possible to have an app continue running in the background when the watch is set to charge?
Would appreciate any advice on this. Cheers.
It's unlikely that watchOS background tasks work similarly to iOS, where an iPhone can for example do expensive Photos operations when the device is charging. Why not pause and resume the workout session?
The "Recover from Crashes" at the bottom of Running Workout Sessions | Apple Developer Documentation might be helpful.

Requesting location in watchOS and its effects on battery

I am trying to create a low-power watchOS app that will only periodically (every 5 minutes or so) need an updated but precise location. The key word there is need. The app certainly could provide better information if it were getting location updates consistently but the goal is for this app to be low power. That being said (and I am not sure if this is possible) if another app is constantly streaming location in the background it would be nice to get those updates too if that does not have extra battery cost.
Is there a way to only request location periodically? Is this actually better for the energy impact of the app?

workout information send to phone live

I have created a heart rate monitor on the apple watch and that sends alerts to notifies you if your heart rate gets to low. After creating the app, I realized it does not run in the background. Is there a way to either force the app to run in the background on the watch or send the heart rate information to the phone?
I've investigated this, but don't have any practical experience (yet).
From what I understand, watch apps in watchOS3 can capture continuous heart rate updates with these conditions:
It's a full app (not just an extension).
The app starts a workout
Info.plist has the WKBackgroundModes key set
You'll need to either start the workout from the watch app, or if triggered from the companion iOS app you'll need to call start​Watch​App​With​Workout​Configuration:​completion:​ to start a workout session on the paired watch.
p.s. welcome to stackoverflow!

How to run ios application in background on device restart?

I am working on Location Tracking Application. This application continuously send location even in background. I am using SLC property.
I am also using silent push trick for location tracking. Logic of Silent push:
Check AppIconbadge number and perform the action according to AppIconbadge number. I have a timer which check AppIconbadge at every 10 seconds.
Now this is the case:
Device is on SLC and put the app in background and turn off the device. While you switch back to On this device it is still reporting on SLC while Ping (Silent Push) is not working.
I have started the timer when app get SLC trigger. But its not working.
Can you help?
You can't do it.
The way iOS achieves great battery life and reduces RAM usage makes this kind of thing impossible.
Your only real possibility is to have your server send occasional push notifications to the device, and have your app on the device respond with its location. This is how Find My Phone and Find My Friends both work. The idea is to improve battery life by doing as much as possible on a server, which has mains power and virtually unlimited RAM, instead of on the phone which only has a battery and might need all it's RAM to play a 3D game
Basically the CPU is not running at all most of the time, so it can't schedule stuff to run periodically, and you can't rely on there being enough RAM available to run your app so it also can't be running all the time even if the CPU is powered on.
According to the WWDC 2013 keynote, push notifications are much more powerful on iOS 7, so you should look that up (I haven't looked into it myself, just saw the keynote.

Can bluetooth be used with iOS Multitasking?

I'm thinking the answer to this is no, but does anyone know if a Bluetooth connection can be maintained in the background with iOS? I'm thinking I might be able to keep it around with the finite-task background API, but I haven't found anything indicating whether that's true or not. Another option would be to use GPS notifications and just reconnect every time the app gets a location changed notification.
You a right. It's a NO.
But if you use location change notification to wake up your app, you may have a short period of time to use Bluetooth.
I think that the Bluetooth connection should be maintained, but if your bluetooth application is not the foreground application it will not receive any data / commands, when it becomes foreground it will.
It is possible, I use this trick to allow an App to use foreground APIs for iBeacons to allow the app to range even when the App is in the background.
To range for iBeacons it uses a high power API and as so this is restricted to only run when the App is in the foreground and stops all delegates being called once the App enters the background.
By playing a silent audio file and adding the AirPlay capability to your plist it allows your app to run in the background just as it would if it was in the foreground.
I'm not sure if it will work for your case but as iBeacons do use the Core Bluetooth and Core Location frameworks it might just do what you are asking.
http://yifan.lu/2013/12/17/unlimited-backgrounding-on-ios/
Note although this trick has not been patched by Apple in iOS8 beta 5 it is possible they will in an update.
If you're using iBeacons, there are built-in APIs for handling when you enter/exit a beacon region, and you typically get ~5 seconds to range for beacons at that point before the app is put to sleep. Theoretically, you could start a background task w/ expiration handler that might allow you to range for ~30 seconds while backgrounded, but I have not verified this is the case. I do know that the background task can be started when normal CLRegions are entered/exited while in the background, and there is functionally no difference between CLRegions and CLBeaconRegions in terms of region monitoring, so if I had to guess I would say this is more-than-likely possible.