简体   繁体   English

HKWorkoutSession:在watchOS 2中关闭屏幕时未获得心率

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

I am using iOS 9 beta 4 and watchOS 2 beta 4. 我正在使用iOS 9 beta 4和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. 我将调用applicationWillResignActive然后心率数据停止。

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. 如果有人在手表上的屏幕被锁定时获得心率数据,我可以发布我的代码来启动WorkoutSession。

Starting in watchOS 3.0 you can add the WKBackgroundModes entry with the workout-processing value to the extension's Info.plist. 从watchOS 3.0开始,您可以将带有workout-processing值的WKBackgroundModes条目添加到扩展的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. 该应用程序可以在后台继续从Apple Watch的传感器访问数据,让您始终保持应用程序的最新状态。 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. 例如,正在运行的应用程序可以继续跟踪用户的心率,确保每当用户抬起手腕时显示最新的心率数据。

The watch app platform is designed to totally disable your code shortly after applicationWillResignActive(). watch app平台旨在在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). 但是,如果HKWorkoutSession处于活动状态,手表应该在后台将心率数据记录到HK商店(只要另一个手表应用程序没有启动新的并取消您的)。

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. 不幸的是,我还没有找到任何方法让心率在手表上的HealthKit商店外的任何地方可靠地直播。

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. 我通过在会话完成后重置HKWorkoutSession来解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 watchOS:如何在没有心率传感器的情况下使用 HKWorkoutSession - watchOS: How to use HKWorkoutSession without heart rate sensor WatchOS2心率 - WatchOS2 Heart Rate WatchOS 4没有收到心率样本? - WatchOS 4 not receiving heart rate samples? 当watchOS应用程序使用HKWorkoutSession在后台运行时如何使WCSession可访问 - How to make WCSession reachable when the watchOS app is running in background with HKWorkoutSession 当屏幕关闭/设备在iOS中被锁定时,计时器未运行 - Timers not running when screen is turned off / device is locked in iOS 是否可以在 JavaScript 中检测到 Android 和 iOS 浏览器中的屏幕何时关闭 - Is it possible, in JavaScript, to detect when the screen is turned off in the Android & iOS browsers iOS:使用三键单击“主页”按钮的“辅助功能”快捷方式打开VoiceOver时,屏幕将关闭 - iOS: When VoiceOver is turned ON using the Accessibility shortcut of triple clicking Home Button, the screen gets turned off 深度链接关闭时,用户将重定向到ios应用 - Users getting redirected to ios app when deep linking is turned off 从简单视频获取心率:内部代码 - Getting heart rate from simple video: code inside 从 Health App 实时获取新的心率数据? - Getting new heart rate data live from Health App?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM