I made my own training application (just to find out how the connection between iWatch and iPhone works). I am currently receiving heart rate information as follows. Obviously, this has not been tested, but it makes sense as soon as you look at how the HealthKit framework is laid out.
We know that the Apple Watch will communicate with the iPhone via Bluetooth. If you read the first paragraph of the HealthKit documentation, you will see the following:
In iOS 8.0, the system can automatically save data from compatible Bluetooth LE heart rates. It is controlled directly in the HealthKit store.
Since we know that the Apple Watch will be a Bluetooth device and has a heart rate sensor, I will assume that the information is stored in HealthKit.
So, I wrote the following code:
- (void) retrieveMostRecentHeartRateSample: (HKHealthStore*) _healthStore completionHandler:(void (^)(HKQuantitySample*))completionHandler { HKSampleType *_sampleType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate]; NSPredicate *_predicate = [HKQuery predicateForSamplesWithStartDate:[NSDate distantPast] endDate:[NSDate new] options:HKQueryOptionNone]; NSSortDescriptor *_sortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierStartDate ascending:NO]; HKSampleQuery *_query = [[HKSampleQuery alloc] initWithSampleType:_sampleType predicate:_predicate limit:1 sortDescriptors:@[_sortDescriptor] resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) { if (error) { NSLog(@"%@ An error has occured with the following description: %@", self, error.localizedDescription); } else { HKQuantitySample *mostRecentSample = [results objectAtIndex:0]; completionHandler(mostRecentSample); } }]; [_healthStore executeQuery:_query]; } static HKObserverQuery *observeQuery; - (void) startObservingForHeartRateSamples: (HKHealthStore*) _healthStore completionHandler:(void (^)(HKQuantitySample*))_myCompletionHandler { HKSampleType *_sampleType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate]; if (observeQuery != nil) [_healthStore stopQuery:observeQuery]; observeQuery = [[HKObserverQuery alloc] initWithSampleType:_sampleType predicate:nil updateHandler:^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError *error) { if (error) { NSLog(@"%@ An error has occured with the following description: %@", self, error.localizedDescription); } else { [self retrieveMostRecentHeartRateSample:_healthStore completionHandler:^(HKQuantitySample *sample) { _myCompletionHandler(sample); }];
Be sure to stop the monitoring request after the screen is free.
Sean mcdonald
source share