sourabh dadapure
sourabh dadapure

Reputation: 202

Apple Health initHeartBeatSeries , how to get HKHeartbeatSeriesSample?

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

https://developer.apple.com/documentation/healthkit/hkheartbeatseriesquery/3113764-initwithheartbeatseries?language=objc

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

}

Upvotes: 1

Views: 759

Answers (1)

Eric Schramm
Eric Schramm

Reputation: 288

This is how I did it. Note, you need to add HKSeriesType.heartbeat() as one of the read types in the requestAuthorization function 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.

let last2hours = HKQuery.predicateForSamples(withStart: Date().addingTimeInterval(-60 * 60 * 24), end: Date(), options: [])
let hbSeriesSampleType = HKSeriesType.heartbeat()
let heartbeatSeriesSampleQuery = HKSampleQuery(sampleType: hbSeriesSampleType, predicate: last2hours, limit: 20, sortDescriptors: nil) { (sampleQuery, samples, error) in
    if let heartbeatSeriesSample = samples?.first as? HKHeartbeatSeriesSample {
        let query = HKHeartbeatSeriesQuery(heartbeatSeries: heartbeatSeriesSample) { (query, timeSinceSeriesStart, precededByGap, done, error) in
            print(timeSinceSeriesStart)
        }
        self.healthStore.execute(query)
    }
}
healthStore.execute(heartbeatSeriesSampleQuery)

For this HKHeartbeatSeriesSample:

count=23 F7D641F8-07AD-4543-84C8-126EA7B98B0F "Eric’s Apple Watch" (7.3.3), "Watch6,2" (7.3.3) "Apple Watch"  (2021-04-13 17:18:59 -0500 - 2021-04-13 17:19:59 -0500)

Code above prints out to console:

0.78125
1.5390625
2.296875
3.08203125
3.87109375
4.61328125
5.37109375
6.10546875
6.86328125
7.7109375
9.3359375
10.94921875
11.76953125
12.5625
20.05078125
20.84765625
21.625
22.45703125
32.62109375
33.36328125
34.08203125
34.8046875
35.53515625

Upvotes: 0

Related Questions