Ho creato la mia applicazione di allenamento (solo per sapere come funziona la comunicazione tra iWatch e iPhone). Attualmente sto ricevendo le informazioni sulla frequenza cardiaca nel modo seguente. Ovviamente questo non è stato testato ma ha senso una volta che si guarda a come è strutturato il framework HealthKit.
Sappiamo che l'Apple Watch comunicherà con l'iPhone tramite Bluetooth. Se avete letto il primo paragrafo della documentazione del HealthKit si vedrà questo:
In iOS 8.0, il sistema può salvare automaticamente i dati da compatibili monitor Bluetooth LE frequenza cardiaca direttamente in negozio HealthKit.
Poiché sappiamo che l'Apple Watch sarà un dispositivo Bluetooth e ha un sensore di frequenza cardiaca, assumerò che le informazioni siano memorizzate in HealthKit.
Così ho scritto il seguente codice:
- (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];
}
Assicurarsi di fermare l'esecuzione della query osservare una volta che lo schermo è deallocata.
fonte
2015-04-08 21:56:54
possibile duplicato di [Dati di frequenza cardiaca su Apple Watch] (http://stackoverflow.com/questions/28858667/heart-rate-data-on-apple-watch) – bmike