HKWorkoutSession: Not getting Heart Rate when screen is turned off in watchOS 2

会有一股神秘感。 提交于 2019-12-18 04:23:25

问题


I am using iOS 9 beta 4 and watchOS 2 beta 4.

I can't seem to get any heart rate data when the watch screen turns black (locks). I will get a call to applicationWillResignActive and then the heart rate data just stops.

It seems that the sensor is deactivating after some time as well (not green anymore), when the screen locks.

Anyone else seeing this behavior? I can post my code for initing the WorkoutSession if anyone else getting heart rate data when the screen on the watch is locked.


回答1:


Starting in watchOS 3.0 you can add the WKBackgroundModes entry with the workout-processingvalue to the extension's Info.plist.

From the docs:

Running in the Background Workout sessions can run in the background. Background running grants an app the following abilities:

The app continues to run throughout the entire workout session, even when the user lowers their wrist or interacts with a different app. When the user raises their wrist, the app reappears, letting the user quickly and easily check their current progress and performance.

The app can continue to access data from Apple Watch’s sensors in the background, letting you keep the app up to date at all times. For example, a running app can continue to track the user’s heart rate, ensuring that the most recent heart rate data is displayed whenever the user raises their wrist.




回答2:


The watch app platform is designed to totally disable your code shortly after applicationWillResignActive(). However with an HKWorkoutSession active, the watch should record heart rate data to the HK store in the background (as long as another watch app doesn't start a new one and cancel yours).

This is roughly what I do to start heart rate collection:

func startCollectingHR(){
    guard workoutSession == nil else{return} // Make sure it's not already started
    // Also make sure you get the appropriate HealthKit permissions
    workoutSession = HKWorkoutSession(activityType: HKWorkoutActivityType.CrossTraining, locationType: HKWorkoutSessionLocationType.Outdoor)
    workoutSession!.delegate = self
    healthStore.startWorkoutSession(workoutSession!)
    print("HR collection started.")
}

Unfortunately, I haven't found any way to get heart rate to reliably live-stream anywhere outside the HealthKit store on the watch.




回答3:


I noticed similar behaviour and found that the first workout session would run as intended, but subsequent ones would only run when the watch face was active. I solved the problem by resetting HKWorkoutSession once the session was completed.



来源:https://stackoverflow.com/questions/31830686/hkworkoutsession-not-getting-heart-rate-when-screen-is-turned-off-in-watchos-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!