OGeek|极客世界-中国程序员成长平台

标题: ios - Apple Healthkit 的心率 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 15:51
标题: ios - Apple Healthkit 的心率

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

我能找到的关于心率检测的大多数示例要么是 Swift,我更愿意远离它,要么是过时的蓝牙/摄像头方法。

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

我 99.9% 确定的问题是给我带来的问题:

    - (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



Best Answer-推荐答案


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

在使用日期格式化程序后将其注销,以检查您的开始日期和结束日期。开始时间和结束时间可能相同,这就是结果为 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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4