Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not getting accelerometer and heart rate data when screen is turned off in watchOS 2

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?

like image 850
ofirdi Avatar asked Dec 18 '22 21:12

ofirdi


2 Answers

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)
   }
}
like image 159
Igor Palaguta Avatar answered May 08 '23 00:05

Igor Palaguta


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.

like image 30
lehn0058 Avatar answered May 08 '23 02:05

lehn0058