本文介绍了Watch os 2.0 beta:访问心跳率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Watch OS 2.0 应该允许开发人员访问心跳传感器......我很想使用它并为我的想法构建一个简单的原型,但我找不到任何关于此功能的信息或文档.

With Watch OS 2.0 developers are supposed to be allowed to access heart beat sensors.... I would love to play a bit with it and build a simple prototype for an idea I have, but I can't find anywhere info or documentation about this feature.

谁能告诉我如何完成这项任务?任何链接或信息将不胜感激

Can anyone point me on how to approach this task? Any link or info would be appreciated

推荐答案

Apple 在技术上并未允许开发人员访问 watchOS 2.0 中的心率传感器.他们正在做的是提供对 HealthKit 中传感器记录的心率数据的直接访问.要做到这一点并近乎实时地获取数据,您需要做两件主要的事情.首先,您需要告诉手表您正在开始锻炼(假设您正在跑步):

Apple isn't technically giving developers access to the heart rate sensors in watchOS 2.0. What they are doing is providing direct access to heart rate data recorded by the sensor in HealthKit. To do this and get data in near-real time, there are two main things you need to do. First, you need to tell the watch that you are starting a workout (lets say you are running):

// Create a new workout session
self.workoutSession = HKWorkoutSession(activityType: .Running, locationType: .Indoor)
self.workoutSession!.delegate = self;

// Start the workout session
self.healthStore.startWorkoutSession(self.workoutSession!)

然后,您可以从 HKHealthKit 启动流式查询,以便在 HealthKit 收到更新时为您提供更新:

Then, you can start a streaming query from HKHealthKit to give you updates as HealthKit receives them:

// This is the type you want updates on. It can be any health kit type, including heart rate.
let distanceType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)

// Match samples with a start date after the workout start
let predicate = HKQuery.predicateForSamplesWithStartDate(workoutStartDate, endDate: nil, options: .None)

let distanceQuery = HKAnchoredObjectQuery(type: distanceType!, predicate: predicate, anchor: 0, limit: 0) { (query, samples, deletedObjects, anchor, error) -> Void in
    // Handle when the query first returns results
    // TODO: do whatever you want with samples (note you are not on the main thread)
}

// This is called each time a new value is entered into HealthKit (samples may be batched together for efficiency)
distanceQuery.updateHandler = { (query, samples, deletedObjects, anchor, error) -> Void in
    // Handle update notifications after the query has initially run
    // TODO: do whatever you want with samples (note you are not on the main thread)
}

// Start the query
self.healthStore.executeQuery(distanceQuery)

视频末尾的演示中详细介绍了这一切HealthKit 的新功能- WWDC 2015

This is all described in full detail in the demo at the end of the video What's New in HealthKit - WWDC 2015

这篇关于Watch os 2.0 beta:访问心跳率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 07:40