HKWorkoutSession:当watchOS中的屏幕closures时,没有获得心率

我正在使用iOS 9 beta 4和watchOS 2 beta 4。

手表屏幕变黑时(locking),我似乎无法获取任何心率数据。 我将打电话给applicationWillResignActive ,然后心率数据就停止。

看起来,传感器在一段时间后也停用(不再是绿色),当屏幕locking时。

任何人看到这种行为? 如果其他人在手表上的屏幕locking时获取心率数据,则可以发布我的代码以启动WorkoutSession。

从watchOS 3.0开始,您可以将WKBackgroundModes条目与workout-processing值一起添加到分机的Info.plist中。

从文档 :

在后台运行锻炼会话可以在后台运行。 背景运行为应用程序提供以下function:

即使用户降低手腕或与其他应用程序交互,应用程序仍会继续在整个运动过程中运行。 当用户举起手腕时,应用会再次出现,让用户快速轻松地检查当前的进度和performance。

该应用程序可以继续在后台访问Apple Watch传感器的数据,让您随时保持应用程序的最新状态。 例如,正在运行的应用可以继续跟踪用户的心率,确保每当用户举起手腕时显示最新的心率数据。

Watch应用程序平台devise为在applicationWillResignActive()之后不久完全禁用您的代码。 然而,HKWorkoutSession激活后,手表应该在后台将心率数据logging到HK商店(只要另一个手表应用程序不会启动新的手表并取消)。

这大概是我做什么开始心率收集:

 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.") } 

不幸的是,我还没有find任何方法让心率在手表上的HealthKit商店外任何地方都能可靠地stream动。

我注意到类似的行为,发现第一个锻炼会按预期运行,但随后的只会在表面活动时运行。 一旦会话完成,我通过重置HKWorkoutSession解决了这个问题。