ios - watch 操作系统 2.0 测试版 : access heart beat rate

标签 ios watchkit apple-watch watchos

有了 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/

相关文章:

ios - iPhone视频控制和双视频播放

ios - 是否可以通过编程方式将 "Button Shapes"覆盖为 Off?

android - Release模式下应用程序的日志消息

ios - 如果 iPhone 应用程序处于非事件状态,iPhone 应用程序可以响应通过 Apple Watch 传输的文件吗?

ios - Xcode 7.1 iOS 9.0 watchOS 2 应用程序在安装后停止运行

ios - 在后台 Context 上保存 NSManagedObject 时出现问题

ios - WatchKit,我应该从 ExtensionDelegate 更新 UI 吗?

watchkit - 可以跨平台开发 watch /可穿戴应用程序吗?

ios - 制作具有功能的独立类并在 InterfaceController 类中使用它

ios - 支持 Watch 的 iOS 应用程序的 Crashlytics/Fabric 分布?