有没有办法从Apple Watch获取传感器数据?例如,我怎样才能连接并从苹果手表获取心率到我的应用程序?这些都是我需要做我的应用程序的步骤:如何从Apple Watch获取传感器数据到iPhone?
- 定义委托从Apple关注接收心脏速率信息。
- 让苹果观看请求发送数据定期
我知道它是如何工作的其他人力资源监视器在BT。界面是否与此类似?还是应该依靠HealthKit来实现?
有没有办法从Apple Watch获取传感器数据?例如,我怎样才能连接并从苹果手表获取心率到我的应用程序?这些都是我需要做我的应用程序的步骤:如何从Apple Watch获取传感器数据到iPhone?
我知道它是如何工作的其他人力资源监视器在BT。界面是否与此类似?还是应该依靠HealthKit来实现?
根据WatchKit FAQ on raywenderlich.com(滚动到“您可以通过手表应用程序访问手表上的心跳传感器和其他传感器吗?”),看起来好像无法访问传感器数据。
不。目前没有API访问 Apple Watch上的硬件传感器。
我做了自己的锻炼应用程序(只是为了了解iWatch和iPhone之间的沟通工作)。我目前正在通过以下方式获取心率信息。显然这还没有经过测试,但一旦你看看HealthKit框架是如何布置的,它就是有意义的。
我们知道Apple Watch将通过蓝牙与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];
}
确保停止执行,一旦画面被释放的观察查询。
[Apple Watch上的心率数据]的可能重复(http://stackoverflow.com/questions/28858667/heart-rate-data-on-apple-watch) – bmike 2015-07-01 15:49:39