Trying to get HeartbeatSeries working but not sure how to get HkHeartbeatSeriesSample. Here's my code
I have this query which is gonna return the data from HeartbeatSeries but I'm not sure how to get the HKHeartbeatSeriesSample
built the query from here
-(void)fetchHeartSeries:(HKHeartbeatSeriesSample *)sample
timeSinceStart: (NSTimeInterval *)timeSinceStart
completion:(void (^)(NSArray *, NSError *))completionHandler API_AVAILABLE(ios(13.0)){
HKHeartbeatSeriesSample *sampleSeries = sample;
NSTimeInterval *timeSince = timeSinceStart;
if (@available(iOS 13.0, *)) {
HKHeartbeatSeriesQuery *query = [
[HKHeartbeatSeriesQuery alloc]
initWithHeartbeatSeries:(HKHeartbeatSeriesSample *)sampleSeries
dataHandler:^(HKHeartbeatSeriesQuery *query,
NSTimeInterval timeSince,
BOOL precededByGap,
BOOL done,
NSError * error){
if (error) {
// Perform proper error handling here
NSLog(@"*** An error occurred while getting the heart beat series: %@ ***", error.localizedDescription);
completionHandler(nil, error);
}
if(done){
NSArray *data = query.accessibilityElements;
NSLog(@"Successfully retrieved heart beat data");
completionHandler(data, nil);
}
}];
[self.healthStore executeQuery:query];
} else {
// Fallback on earlier versions
}
}
This is how I did it. Note, you need to add
HKSeriesType.heartbeat()as one of the read types in therequestAuthorizationfunction to have permissions to get the Beat-to-Beat Measurements.A simple, proof-of-concept to grab the HKHeartbeatSeriesSamples from the last 2 hours and use the first one to get the beat-to-beat measurements and print out the timestamp differences from the start.
I apologize for using Swift here. Let me know and I can provide an Objective-C version.
For this HKHeartbeatSeriesSample:
Code above prints out to console: