苹果手表的心率数据

我们可以直接从苹果手表访问心率吗? 我知道这是一个重复的问题,但没有人在5个月内问过这个问题。 我知道你可以从健康应用程序访问它,但我不知道如何“实时”。

没有直接的方式来访问Apple Watch上的任何传感器。 您将不得不依赖HealthKit的访问。

一位苹果传教士对此表示

目前无法创build心脏监测器应用程序。 数据不能保证实时发送到iPhone,所以你将无法及时确定发生了什么。

请参阅https://devforums.apple.com/message/1098855#1098855

Heart Watchkit for watchOS 2.0原始数据信息现已Watchkit for watchOS 2.0

WatchOS 2包括对WatchOS 2等其他现有框架的许多增强function,可以访问实时访问心率和健康信息的健康传感器。

您可以在下面的会话中查看这些信息,总共30分钟的演示。如果您不想看整个会话,那么您直接跳转到25-28分钟之间的Healthkit APIfunction:

在WWDC 2015的watchOS 2.0会议上的WatchKit

这里是源代码实施链接

正如“ 劳工处参考资料”所述 :

HKWorkout类是HKWorkout类的具体子类。 HealthKit使用锻炼来跟踪广泛的活动。 锻炼对象不仅存储有关活动的总结信息(例如,持续时间,总距离和总能量消耗),还可以作为其他样本的容器。 您可以将任意数量的样本与锻炼关联起来。 通过这种方式,您可以添加与锻炼有关的详细信息。

在给定的链接中,代码的以下部分定义heartRate的采样率

 NSMutableArray *samples = [NSMutableArray array]; HKQuantity *heartRateForInterval = [HKQuantity quantityWithUnit:[HKUnit unitFromString:@"count/min"] doubleValue:95.0]; HKQuantitySample *heartRateForIntervalSample = [HKQuantitySample quantitySampleWithType:heartRateType quantity:heartRateForInterval startDate:intervals[0] endDate:intervals[1]]; [samples addObject:heartRateForIntervalSample]; 

当他们在那里陈述时:

您需要根据锻炼的types和应用程序的需求,精确调整关联样本的确切长度。 使用5分钟的时间间隔可以最大限度地减less存储锻炼所需的内存量,同时仍能在长时间的锻炼过程中提供一般的强度变化。 使用5秒的间隔提供了更详细的锻炼视图,但需要更多的内存和处理。

在探索HealthKit和WatchKit Extension后,我的发现如下:

  1. 我们不需要WatchKit Extension来获取心率数据。
  2. 你只需要有一个iPhone配对苹果手表(这是显而易见的)
  3. 默认Apple Watch心率监测器应用程序只有在前台时立即更新HealthKit数据。
  4. 当“默认Apple Watch心率”监视器应用程序位于“后台”中时,它将以9-10分钟的时间间隔更新HealthKit数据。
  5. 要从HealthKit获取心率数据,需要定期触发以下查询。

     func getSamples() { let heartrate =HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate) let sort = [ NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false) ] let heartRateUnit = HKUnit(fromString: "count/min") 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.doubleValueForUnit(self.heartRateUnit) print (value) let rate = results[0] print(results[0]) print(query) self.updateHeartRate(results) } }) healthStore?.executeQuery(sampleQuery) } func updateHeartRate(samples: [HKSample]?) { guard let heartRateSamples = samples as? [HKQuantitySample] else {return} dispatch_async(dispatch_get_main_queue()) { guard let sample = heartRateSamples.first else{return} let value = sample.quantity.doubleValueForUnit(self.heartRateUnit) self.heartRateLabel.text = String(UInt16(value)) let date = sample.startDate let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss" self.timeStampLabel.text = dateFormatter.stringFromDate(date) } } 

如果有人获得更多信息,请更新我。
快乐的编码。

您可以通过开始锻炼并从healthkit查询心率数据来获取心率数据。

  1. 请求阅读锻炼数据的准备

     HKHealthStore *healthStore = [[HKHealthStore alloc] init]; HKQuantityType *type = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate]; HKQuantityType *type2 = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning]; HKQuantityType *type3 = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierActiveEnergyBurned]; [healthStore requestAuthorizationToShareTypes:nil readTypes:[NSSet setWithObjects:type, type2, type3, nil] completion:^(BOOL success, NSError * _Nullable error) { if (success) { NSLog(@"health data request success"); }else{ NSLog(@"error %@", error); } }]; 
  2. iPhone的AppDelegate中 ,回应这个请求

     -(void)applicationShouldRequestHealthAuthorization:(UIApplication *)application{ [healthStore handleAuthorizationForExtensionWithCompletion:^(BOOL success, NSError * _Nullable error) { if (success) { NSLog(@"phone recieved health kit request"); } }]; } 
  3. 然后实施Healthkit Delegate

     -(void)workoutSession:(HKWorkoutSession *)workoutSession didFailWithError:(NSError *)error{ NSLog(@"session error %@", error); } -(void)workoutSession:(HKWorkoutSession *)workoutSession didChangeToState:(HKWorkoutSessionState)toState fromState:(HKWorkoutSessionState)fromState date:(NSDate *)date{ dispatch_async(dispatch_get_main_queue(), ^{ switch (toState) { case HKWorkoutSessionStateRunning: //When workout state is running, we will excute updateHeartbeat [self updateHeartbeat:date]; NSLog(@"started workout"); break; default: break; } }); } 
  4. 现在是时候写**[self updateHeartbeat:date]**

     -(void)updateHeartbeat:(NSDate *)startDate{ //first, create a predicate and set the endDate and option to nil/none NSPredicate *Predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:nil options:HKQueryOptionNone]; //Then we create a sample type which is HKQuantityTypeIdentifierHeartRate HKSampleType *object = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate]; //ok, now, create a HKAnchoredObjectQuery with all the mess that we just created. heartQuery = [[HKAnchoredObjectQuery alloc] initWithType:object predicate:Predicate anchor:0 limit:0 resultsHandler:^(HKAnchoredObjectQuery *query, NSArray<HKSample *> *sampleObjects, NSArray<HKDeletedObject *> *deletedObjects, HKQueryAnchor *newAnchor, NSError *error) { if (!error && sampleObjects.count > 0) { HKQuantitySample *sample = (HKQuantitySample *)[sampleObjects objectAtIndex:0]; HKQuantity *quantity = sample.quantity; NSLog(@"%f", [quantity doubleValueForUnit:[HKUnit unitFromString:@"count/min"]]); }else{ NSLog(@"query %@", error); } }]; //wait, it's not over yet, this is the update handler [heartQuery setUpdateHandler:^(HKAnchoredObjectQuery *query, NSArray<HKSample *> *SampleArray, NSArray<HKDeletedObject *> *deletedObjects, HKQueryAnchor *Anchor, NSError *error) { if (!error && SampleArray.count > 0) { HKQuantitySample *sample = (HKQuantitySample *)[SampleArray objectAtIndex:0]; HKQuantity *quantity = sample.quantity; NSLog(@"%f", [quantity doubleValueForUnit:[HKUnit unitFromString:@"count/min"]]); }else{ NSLog(@"query %@", error); } }]; //now excute query and wait for the result showing up in the log. Yeah! [healthStore executeQuery:heartQuery]; } 

您还可以打开Healthkit的function。 如果您有任何问题,请在下面留言。