如何从Apple Watch获取传感器数据到iPhone?

有没有办法从Apple Watch获取传感器数据? 例如,我怎样才能连接并从苹果手表获得心率到我的应用程序? 这些是我需要在我的应用程序中执行的步骤:

  1. 定义一个委托来接收来自Apple Watch的心率信息。
  2. 向Apple Watch发送请求以定期发送数据

我知道它是如何工作的其他人力资源显示器超过BT。 界面是类似的吗? 还是应该依靠HealthKit来实现呢?

根据raywenderlich.com上的WatchKit常见问题解答 (滚动到“您可以通过手表应用程序访问手表上的心跳传感器和其他传感器吗?”),看起来好像您无法访问传感器数据。

不,目前没有API访问Apple Watch上的硬件传感器。

我做了我自己的锻炼应用程序(只是为了了解如何沟通之间的iWatch和iPhone工作)。 我目前正在通过以下方式获取心率信息。 显然这还没有经过testing,但是一旦你看看HealthKit框架是如何布置的,这是有道理的。

我们知道苹果手表将通过蓝牙与iPhone进行通信。 如果您阅读HealthKit文档的第一段,您将看到:

在iOS 8.0中,系统可以自动将兼容蓝牙LE心率监视器的数据直接保存到HealthKit存储中。

由于我们知道苹果手表将是一个蓝牙设备,并有一个心率传感器,我会假设信息存储在HealthKit。

所以我写了下面的代码:

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

一旦屏幕解除分配,确保停止执行观察查询。