Reputation: 481
I'm working on apple watch app using CMMotionManager and HKWorkoutSession to get both the accelerometer and the heart rate data. The app works fine for a short period of time (70 seconds), but when the screen is turned off, both the heart rate and the accelerometer data just stops.
My question is how could I get a 5 minutes data from both the accelerometer and the heart rate sensors?
Upvotes: 4
Views: 1473
Reputation: 3579
To take more time when app is going to background you can use performExpiringActivityWithReason
. This is described here https://developer.apple.com/videos/play/wwdc2015/228/?time=345 On my watch it just takes approx 30 seconds
NSProcessInfo.processInfo().performExpiringActivityWithReason("Reason") {
expired in
if !expired {
let delay: Int64 = 30
let delayTime = dispatch_time(DISPATCH_TIME_NOW, delay * Int64(NSEC_PER_SEC))
dispatch_semaphore_wait(semaphore, delayTime)
} else {
dispatch_semaphore_signal(semaphore)
}
}
Upvotes: 5
Reputation: 45
Now, Apple open an API for recording your Accel data Click:CMSensorRecorder . whenever your app is suspended or killed, the API will continuous run in 50Hz almost 3 days.
Upvotes: 1
Reputation: 20237
Since the release of watchOS 2, HKWorkoutSession is the only way to keep an app running when the watch screen goes off. HOWEVER, the app is in a suspended state. Timers and any other process execution is paused until the screen turns back on. Memory allocations for the app however is preserved. During the time the app is suspended, the device's hardware is still collecting data and storing it on the watch's hard drive. When the screen turns back on the the app come out of suspension and any data that was collected by the hardware is returned at that point to the app (assuming the appropriate listeners were subscribed to).
There is not currently a way to continue to send heart rate data (or any data) from the watch to an iPhone after the screen turns off.
Upvotes: 4
Reputation: 7353
watchOS 2 does not allow apps to run while the screen is off. Although there are ways you can get a bit of extra time, such as with performExpiringActivityWithReason
, there is no way to ensure that your app runs for a full 5 minutes.
Upvotes: 1