How to get sensor data from Apple Watch to iPhone?

Is there any way to get sensor data from Apple Watch? For example, how can I connect and get the heart rate from the Apple Watch before my application? These are the steps I need to take in my application:

  • Identify a delegate for Apple Watch heart rate information.
  • Request Apple Watch to send data periodically.

I know how this works for other HR monitors on BT. Is the interface compatible with this? Or should it depend on HealthKit to achieve this?

+8
ios watchkit health-kit
source share
2 answers

Like the WatchKit FAQ on raywenderlich.com (scroll to โ€œCan you access the heart rate sensor and other sensors on the watch from your watch application?โ€), It seems that you cannot access the sensor data.

Not. There is currently no API for accessing hardware sensors on the Apple Watch at this time.

+4
source share

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); }]; // If you have subscribed for background updates you must call the completion handler here. // completionHandler(); } }]; [_healthStore executeQuery:observeQuery]; } 

Be sure to stop the monitoring request after the screen is free.

+3
source share

All Articles