首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在不使用锻炼会话的情况下获得watchOS 3+中的心率

如何在不使用锻炼会话的情况下获得watchOS 3+中的心率
EN

Stack Overflow用户
提问于 2019-05-19 21:23:08
回答 1查看 454关注 0票数 2

我想通过一个watchkit扩展程序来收集用户的心率,但是在锻炼过程中,不是

据我所知,如果用户移动,watchOS 3+不会收集心率:然而,只要他休息一段时间(10分钟?)我想通过HealthKit API获得当前的bpm值。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-01 17:47:44

由于您有一个配套的iPhone应用程序,您可以从那里查询心率更新。

  • 你只需要一个iPhone与配对的苹果手表(这是显而易见的)
  • 默认的Apple心率监视器应用程序只有在前台时才会立即更新HealthKit数据。
  • 当默认的Apple心率监控器应用程序处于后台时,它会以9-10分钟的间隔更新HealthKit数据。这段代码应该完成以下工作: 导入UIKit导入HealthKit类ViewController: UIViewController { @IBOutlet弱var heartRateLabel: UILabel!@IBOutlet弱var timeStampLabel: UILabel!hkStore: HKHealthStore?设heartRateUnit: HKUnit = HKUnit.count().unitDivided(by:.minute()) var healthStore: HKHealthStore?重写func viewDidLoad() { super.viewDidLoad() healthStore = HKHealthStore()让sampleTypes = HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate)!healthStore?.requestAuthorization(toShare: sampleTypes,读: sampleTypes,完成:在if (error != nil) { print(error!.localizedDescription) }}中{(成功,错误) }) getSamples() } func getSamples() { let心脏= HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate) let sort =NSSortDescriptor(键: HKSampleSortIdentifierStartDate,升序: false) let sampleQuery = HKSampleQuery(sampleType: heartrate!)谓词: nil,限制: 1,sortDescriptors: sort,resultsHandler:{ unowned self in如果让结果=结果?HKQuantitySample { let sample =结果as HKQuantitySample let value = sample.quantity.doubleValue(for: self.heartRateUnit) let rate =结果打印(value,rate) self.updateHeartRate( healthStore?.execute(sampleQuery) }) healthStore?.execute(SampleQuery)} func updateHeartRate(样品: HKSample?)HKQuantitySample guard { let } DispatchQueue.main.async {guard sample = heartRateSamples.first else {else} let值= sample.quantity.doubleValue(for: self.heartRateUnit) self.heartRateLabel.text =String(UInt16(值)) let date = sample.startDate let dateFormatter = DateFormatter() =“yyyy dd hh:mm:ss”self.timeStampLabel.text = dateFormatter.string(from: date) }}

请注意,查询需要定期触发。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56212114

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档