Apple Healthkit带来的心率 [英] Heart Rate With Apple's Healthkit

查看:159
本文介绍了Apple Healthkit带来的心率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前在应用程序中使用Healthkit,在获取大多数类型的信息时没有问题,但是在心率方面遇到了麻烦.每次尝试读取样本时,结果都为"0".我有一个Apple Watch,并且心率"已输入到Apple Health应用程序中,并且可以在那里直观地看到它,所以这不是硬件问题.我只需要显示它,而无需写回数据.它要求我在初次运行时获得允许以允许获取心率,因此该代码应该不会有任何问题,但我还是会发布它.

I'm currently using Healthkit in an app and have no problems getting info for most types, but am having trouble with the Heart Rate. Every time I try to read a sample, I get "0" as a result. I have an Apple Watch, and my Heart Rate gets fed into the Apple Health app, and can visually see it there, so it's not a hardware issue. I only need to display it, no need to write the data back. It asks my permission on first run to allow access to the heart rate, so shouldn't be any issues with that code, but I'll post it anyways.

在心率检测中我能找到的大多数示例要么是在Swift中,我宁愿远离它,要么已经过了蓝牙/相机方法.

Most examples that I can find with heart rate detection are either in Swift, which I prefer to stay away from, or outdates bluetooth/camera methods.

这是我正在使用的东西,主要是从代码中复制并粘贴的代码,这些代码在我的代码中的其他地方检索台阶,步行距离等...因此,我可能在此块中得到了一些与心率数据类型,但找不到.现在看来,只要我能看到某些类型的数据正在馈送到应用程序中,它将获得当天的平均心率,而不是最新的样本,这对现在来说还可以.我找不到一种方法来轮询最新的心率样本,这使我相信我完全使用了错误的方法,但是找不到其他有用的信息或样本.我已经尝试了大约8种不同的在线示例代码/项目,结果没有一个可以产生除"0"之外的任何东西(所以请不要链接到示例代码...我已经尝试了所有可用的示例代码/项目. ... google,github等,等等,因此任何链接都可能会收到我已经测试了一个相同的错误"响应,大声笑).这是我现在正在使用的:

Here is what I'm using, which is mostly copy and pasted from the code that retrieves steps, walking distance, etc elsewhere in my code... so I've probably got something in this block thats not compatible with the heart rate data type, but I can't find it. Right now it looks like it will be getting the average heart rate for the day, and not the latest sample, which is fine for now, as long as I can see SOME type of data being fed to the app. I couldn't find a way to poll the latest sample of the heart rate, which leads me to believe I'm using the wrong method entirely, but can't find any other info or samples that work. I've tried about 8 different online sample code/projects, none of which I can get to produce anything other than "0" as a result (so please no links to example code... I've tried all the ones available already... google, github, etc, etc, so any links will probably get an "I've already tested that one and same error" response, lol). Here's what I'm using right now:

我确定99.9%的障碍正在给我带来问题:

The block that I'm 99.9% sure is giving me the issues:

    - (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上方,我有正确的引用,如下所示:

And above my - (void)queryHealthDataHeart, I have the proper references, as seen here:

#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

推荐答案

如果要获取averageQuantity,请在HKStatisticsQuery选项中进行询问.否则,它将始终为零(即未计算): options参数应设置为HKStatisticsOptionDiscreteAverage,而不是 HKStatisticsOptionNone

if you want to get averageQuantity ask for it in your HKStatisticsQuery options. otherwise it will always be nil (ie. not calculated): options parameter should be set to HKStatisticsOptionDiscreteAverage, NOT HKStatisticsOptionNone

这篇关于Apple Healthkit带来的心率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆