简体   繁体   English

如何将Apple Watch的传感器数据传输到iPhone?

[英]How to get sensor data from Apple Watch to iPhone?

Is there a way to get sensor data from Apple Watch? 有没有办法从Apple Watch获取传感器数据? For example, how can I connect and get heart rate from Apple Watch to my app? 例如,如何从Apple Watch连接并获取心率到我的应用程序? These are the steps I need do in my app: 这些是我在我的应用中需要执行的步骤:

  1. Define a delegate to receive heart rate information from Apple Watch. 定义代表以从Apple Watch接收心率信息。
  2. Make a request to Apple Watch to send the data periodically 向Apple Watch请求定期发送数据

I know how it works for other HR monitors over BT. 我知道它如何适用于BT上的其他HR监视器。 Is the interface similar to that? 界面是否类似? Or should depend on HealthKit to achieve that? 或者应该依靠HealthKit来实现这一目标?

As per WatchKit FAQ on raywenderlich.com (scroll to "Can you access the heartbeat sensor and other sensors on the watch from your watch app?") it appears as though you can not access the sensor data. 根据raywenderlic.com上的WatchKit常见问题解答 (滚动到“您可以从手表应用程序访问手表上的心跳传感器和其他传感器吗?”),看起来好像您无法访问传感器数据。

No. There is currently no API to access the hardware sensors on the Apple Watch at this time. 不可以。目前Apple Watch上没有访问硬件传感器的API。

I have made my own workout application (just to learn how communication between iWatch and the iPhone work). 我已经制作了自己的锻炼应用程序(只是为了了解iWatch和iPhone之间的通信是如何工作的)。 I am currently getting the heart rate information in the following way. 我目前正在通过以下方式获取心率信息。 Obviously this has not been tested but it makes sense once you look at how the HealthKit framework is laid out. 显然,这还没有经过测试,但是一旦你看看HealthKit框架是如何布局的,它就有意义了。

We know that the Apple Watch will communicate with the iPhone through Bluetooth. 我们知道Apple Watch将通过蓝牙与iPhone通信。 If you read the first paragraph of the HealthKit's documentation you will see this: 如果您阅读HealthKit文档的第一段,您将看到:

In iOS 8.0, the system can automatically save data from compatible Bluetooth LE heart rate monitors directly into the HealthKit store. 在iOS 8.0中,系统可以自动将兼容的蓝牙LE心率监视器中的数据直接保存到HealthKit存储中。

Since we know the Apple Watch will be a Bluetooth device and has a heart rate sensor I will be assuming the information is stored in HealthKit. 由于我们知道Apple Watch将是蓝牙设备并且具有心率传感器,因此我将假设信息存储在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];
}

Make sure to stop executing the observe query once the screen is deallocated. 确保在取消分配屏幕后停止执行观察查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Apple Watch(而非iPhone)获取加速度计和陀螺仪数据? - Get accelerometer and gyroscope data from apple watch (and not the iphone)? 如何将数据从 iPhone 应用程序传递到 Apple Watch 上的 Glance? - How to pass data from iPhone app to a Glance on Apple Watch? 如何在目标C中将数组数据从iPhone传递到Apple Watch - How to pass an array data from iphone to apple watch in objective c 使用Swift将数据从Apple Watch同步到iPhone - Synchronising data from Apple Watch to iPhone with Swift 将数据从 Apple Watch 发送到 iPhone - Sending Data from Apple Watch to iPhone 锻炼会话期间来自Apple Watch的实时传感器数据 - Real Time sensor data from the Apple Watch during a Workout Session 如何继续在Apple Watch上收集运动传感器数据? - How to continue collect motion sensor data on apple watch? 为什么我的iPhone不能从Apple Watch获得heartRate数据,而手表扩展却可以呢? - Why can't my iPhone get heartRate data from an Apple Watch, but the watch extension can? 如何从 iPhone 向 Apple Watch 发送通知? - How to send notification from iPhone to apple watch? Apple Watch 是否可以在没有 iPhone 作为中介的情况下直接从 Web 服务器获取数据? - Is it possible for an Apple Watch to get data directly from a Web Server without having an iPhone as an intermediary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM