有了 Watch OS 2.0,开发者应该被允许访问心跳传感器.... 我很想尝试一下,并为我的想法构建一个简单的原型(prototype),但我找不到任何关于此功能的信息或文档。
谁能告诉我如何完成这项任务?任何链接或信息将不胜感激
最佳答案
从技术上讲,Apple 并未允许开发人员访问 watchOS 2.0 中的心率传感器。他们所做的是提供对 HealthKit 中传感器记录的心率数据的直接访问。要做到这一点并近乎实时地获取数据,您需要做两件主要的事情。首先,您需要告诉 watch 您正在开始锻炼(假设您正在运行):
// 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 收到更新时为您提供更新:
// 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)
视频末尾的演示中对此进行了详细描述 What's New in HealthKit - WWDC 2015
关于ios - watch 操作系统 2.0 测试版 : access heart beat rate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30776331/