简体   繁体   中英

Tell if a Health Kit sample came from an Apple Watch?

Health App displays a Watch icon when the source was an Apple Watch.

I'd simply like to get the same information that the Health App is using to determine the type of source. HKSource doesn't seem to provide that.

since iOS 9, samples of type HKSample have the property device of type HKDevice .

https://developer.apple.com/library/prerelease/ios/documentation/HealthKit/Reference/HKDevice_ClassReference/index.html

HKDevice tells you everything about the source device of the sample.

HKDevice.model describes the hardware type. At the time of writing, Apple does not document what values Apple uses in HKDevice.model . In my experiments, I found values "iPhone" and "Watch".

I had a similar problem in my app, where we needed to pull steps data only specific to Apple iPhone and Apple Watch only. I searched a lot, but could not find any answer to it for iOS 8.0, and landed on your question.

I have figured out a way to distinguish between the watch and the phone using the following process (may not be the best solution, but works in my situation):

I noticed that all step data coming from the iPhone/Watch have the following bundleIdentifier format:

com.apple.health.DeviceUUID

Note that manually entered data into the Health app has a bundle identifier of com.apple.Health (with a capital 'H').

So, first thing, get the device name for the phone using:

NSString *deviceName = [[UIDevice currentDevice] name];

Next, fetch all the sources for which there is a prefix match of 'com.apple.health' in the bundleIdentifier. This should give you the iPhone and the Apple watch as the valid sources and ignore the manual entries and all other apps.

Next, check if the name of the device is the same in the source, and ignore that source (iPhone), the other source should be your Apple Watch.

Here's a sample source query for fetching the sources :

- (void)fetchSources 
{
    NSString *deviceName = [[UIDevice currentDevice] name];
    NSMutableArray *dataSources = [[NSMutableArray alloc] init];
    HKQuantityType *stepsCount = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
    HKSourceQuery *sourceQuery = [[HKSourceQuery alloc] initWithSampleType:stepsCount
                                                           samplePredicate:nil
                                                         completionHandler:^(HKSourceQuery *query, NSSet *sources, NSError *error)
                                                         {
                                                             for (HKSource *source in sources)
                                                             {
                                                                 //Ignore the iPhone as a source as the name of the device will watch with the source.
                                                                 //The other device will be an Apple Watch   
                                                                 if ([source.bundleIdentifier hasPrefix:sourceIdentifier] && ![source.name isEqualToString:deviceName])
                                                                 {
                                                                     [dataSources addObject:source];
                                                                 }
                                                             }
                                                         }];
    [self.healthStore executeQuery:sourceQuery];
}

You can now create a predicate with this source for your data pull using the NSPredicate class:

NSPredicate *sourcesPredicate = [HKQuery predicateForObjectsFromSource:source];

Note that my first thought was to match the UUID, but when I generate a UUID using the NSUUID class, it does not match with the one present in the bundle identifier in the pulled sources.

Also, you can change the name of the phone to whatever you want, it will automatically update in the Health app as well.

As I said, not the best solution but works for me, and it's the only way I could find to do this. Please let me know if you were able to find a better solution. Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM