在 watchOS 2 中关闭屏幕时无法获取加速度计和心率数据

ofi*_*rdi 4 accelerometer healthkit apple-watch watchkit watchos-2

我正在使用 CMMotionManager 和 HKWorkoutSession 开发 Apple Watch 应用程序以获取加速度计和心率数据。该应用程序在短时间内(70 秒)运行良好,但当屏幕关闭时,心率和加速度计数据都会停止。

我的问题是如何从加速度计和心率传感器获得 5 分钟的数据?

Igo*_*uta 5

要在应用程序进入后台时花费更多时间,您可以使用performExpiringActivityWithReason. 这在这里描述https://developer.apple.com/videos/play/wwdc2015/228/?time=345在我的手表上它只需要大约 30 秒

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)
   }
}
Run Code Online (Sandbox Code Playgroud)