2015-04-06 10 views
8

C'è un modo per ottenere i dati del sensore da Apple Watch? Ad esempio, come posso connettermi e ottenere la frequenza cardiaca da Apple Watch alla mia app? Questi sono i passaggi che ho bisogno di fare nella mia app:Come ottenere i dati del sensore da Apple Watch a iPhone?

  1. Definire un delegato per ricevere informazioni sulla frequenza cardiaca da Apple Watch.
  2. fare una richiesta ad Apple Watch per inviare i dati periodicamente

So come funziona per altri monitor HR oltre BT. L'interfaccia è simile a quella? O dovrebbe dipendere da HealthKit per raggiungere quello?

+0

possibile duplicato di [Dati di frequenza cardiaca su Apple Watch] (http://stackoverflow.com/questions/28858667/heart-rate-data-on-apple-watch) – bmike

risposta

4

Come da WatchKit FAQ on raywenderlich.com (scorrere fino a "È possibile accedere al sensore del battito cardiaco e ad altri sensori sull'orologio dall'app dell'orologio?"), Sembra che non sia possibile accedere ai dati del sensore.

No. Al momento non è disponibile alcuna API per accedere ai sensori hardware sull' Apple Watch.

3

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.

Problemi correlati