Watchkit 如何在watchOS 3+;不使用训练课程
我希望通过watchkit扩展程序以编程方式收集用户的心率,但在训练过程中,不Watchkit 如何在watchOS 3+;不使用训练课程,watchkit,heartbeat,Watchkit,Heartbeat,我希望通过watchkit扩展程序以编程方式收集用户的心率,但在训练过程中,不 据我所知,如果用户在移动,watchOS 3+不会采集心率:但是,只要他休息一段时间(10分钟?),我想通过HealthKit API以某种方式获取当前的bpm值 由于您有一个配套的iPhone应用程序,您可以从那里查询心率更新 你只需要有一个iPhone和一对苹果手表(这是 (显而易见) 默认的Apple Watch心率监视器应用程序会更新HealthKit 仅当数据位于前台时,才立即显示数据 当默认的Apple
据我所知,如果用户在移动,watchOS 3+不会采集心率:但是,只要他休息一段时间(10分钟?),我想通过HealthKit API以某种方式获取当前的bpm值 由于您有一个配套的iPhone应用程序,您可以从那里查询心率更新
- 你只需要有一个iPhone和一对苹果手表(这是 (显而易见)
- 默认的Apple Watch心率监视器应用程序会更新HealthKit 仅当数据位于前台时,才立即显示数据
- 当默认的Apple Watch心率监视器应用程序处于
背景,每隔9-10天更新HealthKit数据
分钟。
此代码应完成以下工作:
import UIKit import HealthKit class ViewController: UIViewController { @IBOutlet weak var heartRateLabel: UILabel! @IBOutlet weak var timeStampLabel: UILabel! var hkStore: HKHealthStore? let heartRateUnit: HKUnit = HKUnit.count().unitDivided(by: .minute()) var healthStore: HKHealthStore? override func viewDidLoad() { super.viewDidLoad() healthStore = HKHealthStore() let sampleTypes = HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate)! healthStore?.requestAuthorization(toShare: [sampleTypes], read: [sampleTypes], completion: { (success, error) in if (error != nil) { print(error!.localizedDescription) } }) getSamples() } func getSamples() { let heartrate = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate) let sort = [ NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false) ] let sampleQuery = HKSampleQuery(sampleType: heartrate!, predicate: nil, limit: 1, sortDescriptors: sort, resultsHandler: { [unowned self] (query, results, error) in if let results = results as? [HKQuantitySample] { let sample = results[0] as HKQuantitySample let value = sample.quantity.doubleValue(for: self.heartRateUnit) let rate = results[0] print(value, rate) self.updateHeartRate(samples: results) } }) healthStore?.execute(sampleQuery) } func updateHeartRate(samples: [HKSample]?) { guard let heartRateSamples = samples as? [HKQuantitySample] else {return} DispatchQueue.main.async { guard let sample = heartRateSamples.first else{return} let value = sample.quantity.doubleValue(for: self.heartRateUnit) self.heartRateLabel.text = String(UInt16(value)) let date = sample.startDate let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss" self.timeStampLabel.text = dateFormatter.string(from: date) } } }