ios - 使用 Apple Healthkit 测量心率

标签 ios healthkit hkhealthstore

我目前正在应用程序中使用 Healthkit,获取大多数类型的信息都没有问题,但在心率方面遇到问题。每次我尝试读取样本时,结果都是“0”。我有一 block Apple Watch,我的心率被输入到 Apple Health 应用程序中,并且可以在那里直观地看到它,所以这不是硬件问题。我只需要显示它,不需要写回数据。它在第一次运行时需要我的许可才能访问心率,因此该代码应该不会有任何问题,但无论如何我都会发布它。

我能找到的大多数心率检测示例要么是使用 Swift(我宁愿远离它),要么是过时的蓝牙/摄像头方法。

这是我正在使用的内容,主要是从代码中复制并粘贴的,用于检索代码中其他地方的步数、步行距离等...所以我可能在这个 block 中得到了与心率数据类型,但我找不到。现在看起来它将获得当天的平均心率,而不是最新的样本,这目前还可以,只要我可以看到某种类型的数据被输入到应用程序中。我找不到方法来轮询最新的心率样本,这让我相信我完全使用了错误的方法,但找不到任何其他有效的信息或样本。我已经尝试了大约 8 个不同的在线示例代码/项目,其中没有一个能够生成除“0”之外的任何结果(所以请不要链接到示例代码...我已经尝试了所有可用的代码/项目... google、github 等,所以任何链接都可能会得到“我已经测试过那个相同的错误”响应,哈哈)。这是我现在正在使用的:

我 99.9% 确信这个 block 给我带来了问题:

    - (void)queryHealthDataHeart{
    HKQuantityType *typeHeart =[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay
                                               fromDate:now];
    NSDate *beginOfDay = [calendar dateFromComponents:components];
    NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:beginOfDay endDate:now options:HKQueryOptionStrictStartDate];

    HKStatisticsQuery *squery = [[HKStatisticsQuery alloc] initWithQuantityType:typeHeart quantitySamplePredicate:predicate options:HKStatisticsOptionNone completionHandler:^(HKStatisticsQuery *query, HKStatistics *result, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            HKQuantity *quantity = result.averageQuantity;
            double beats = [quantity doubleValueForUnit:[HKUnit heartBeatsPerMinuteUnit]];
            _lblHeart.text = [NSString stringWithFormat:@"%.f",beats];
        }
        );
    }];
    [self.healthStore executeQuery:squery];
}

检查权限的代码:

- (void)readHealthKitData{
HKHealthStore *healthStore = [[HKHealthStore alloc] init];
NSSet *shareObjectTypes = [NSSet setWithObjects:
                           nil];
NSSet *readObjectTypes  = [NSSet setWithObjects:
                           [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning],
                           [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate],
                           [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierFlightsClimbed],
                           [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount],
                           nil];
// Request access
[healthStore requestAuthorizationToShareTypes:shareObjectTypes
                                    readTypes:readObjectTypes
                                   completion:^(BOOL success, NSError *error) {
                                       if(success == YES)
                                       {
                                           [self queryHealthData];
                                           [self queryHealthDataDistance];
                                           [self queryHealthDataFlights];
                                           [self queryHealthDataHeart];
                                       }
                                       else
                                       {
                                           // Determine if it was an error or if the
                                           // user just canceld the authorization request
                                       }

                                   }];}

在我的 - (void)queryHealthDataHeart 之上,我有正确的引用资料,如下所示:

#import "AN_Pedometer.h"
#import <HealthKit/HealthKit.h>

@interface AN_Pedometer (){
UILabel *lbCMData;
NSDateFormatter *formatter;}
@property (nonatomic, strong) CMPedometer *pedometer;
@property (nonatomic, strong) HKHealthStore *healthStore;
@end
@interface HKUnit (HKManager)
+ (HKUnit *)heartBeatsPerMinuteUnit;
@end

@implementation HKUnit (HKManager)

+ (HKUnit *)heartBeatsPerMinuteUnit {
return [[HKUnit countUnit] unitDividedByUnit:[HKUnit minuteUnit]];
}

@end

最佳答案

根据我所读到的内容,如果我是正确的,您正在尝试读取从一天开始到现在的心率。

使用日期格式化程序后注销它们来检查您的开始日期和结束日期。开始时间和结束时间可能相同,这就是结果为 0 的原因。

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZ"];
NSString *dateString = [dateFormatter stringFromDate:beginOfDay];
NSLog(@"%@", dateString);

您还应该将小时、分钟和秒设置为零,以确保您获得一天的开始时间。

NSDateComponents *components = [calendar components:NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay
                                           fromDate:now];
components.hour = 0;
components.minute = 0;
components.second = 0;

希望这有帮助。

关于ios - 使用 Apple Healthkit 测量心率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31440205/

相关文章:

ios - HKAnchoredObjectQuery 可以在 HealthKit 中查询已删除的 HKSampleType 吗?

ios - 带有 Objective-C 客户端的 Socket.IO-Swift 库

ios8 - b\w homekit 和外部附件框架的确切区别是什么?

ios - HealthKit - 当用户不允许访问或关闭所有类别时,源查询是否返回应用程序?

healthkit - 我们如何将测试 HKClinicalRecord 数据加载到 HealthKit 中?

ios - 如何检测用户是否不允许 Apple Health Kit 授权?

ios - swift 1.2 HealthKit

ios - Alamofire 获取请求和 JSON 响应

ios - 临时分发设备 - 每个应用程序还是每个开发人员?

android - 在 Android 和 IOS 中创建像 facebook 这样的应用程序时应该使用什么样的安全性