我们可以直接从苹果手表获取心率吗?我知道这是一个重复的问题,但没有人在5个月内问过这个问题。我知道你可以从健康应用程序访问它,但我不确定它将是“实时”的。
答案 0 :(得分:36)
Watchkit for watchOS 2.0
中的心率原始数据信息现已可用。
WatchOS 2
包括对HealthKit
等其他现有框架的许多增强功能,可以访问实时访问心率和健康信息的健康传感器。
您可以在以下会话中查看此信息,总共30分钟演示。如果您不想观看整个会话,那么您可以直接跳转到25分钟之间的Healthkit API
个功能:< / p>
WatchKit for watchOS 2.0 Session in WWDC 2015
以下是源代码实现link
如HKWorkout Class Reference中所述:
HKWorkout
类是HKSample
类的具体子类。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];
他们在那里说:
您需要微调相关样本的确切长度 根据锻炼类型和您的应用程序的需求。用5分钟 间隔最小化存储锻炼所需的内存量, 同时仍然提供强度变化的一般意义 长时间锻炼的过程。使用5秒间隔提供a 更详细的锻炼观点,但需要更多 记忆和处理。
答案 1 :(得分:11)
在探索HealthKit和WatchKit Extension之后,我的发现如下:
要获得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)
}
}
如果有人获得更多信息,请更新我 快乐的编码。
答案 2 :(得分:10)
无法直接访问Apple Watch上的任何传感器。您将不得不依赖HealthKit的访问权。
Apple传教士说这个
目前无法创建心脏监护应用程序。该 数据无法保证实时发送到iPhone,因此您不会受到影响 能够及时确定发生了什么。
答案 3 :(得分:5)
您可以通过开始锻炼并从healthkit查询心率数据来获取心率数据。
要求预先阅读锻炼数据。
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);
}
}];
在 iPhone上的AppDelegate 中,回复此请求
-(void)applicationShouldRequestHealthAuthorization:(UIApplication *)application{
[healthStore handleAuthorizationForExtensionWithCompletion:^(BOOL success, NSError * _Nullable error) {
if (success) {
NSLog(@"phone recieved health kit request");
}
}];
}
然后实施 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;
}
});
}
现在是时候写**[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];
}
您还可以使用健康套件。如果您有任何疑问,请在下面发表评论。