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

2

7

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.

Blount answered 29/6, 2015 at 7:37 Comment(2)
By reading the HKSource name property, I can check for a substring "Apple Watch" since it's likely named "[user]'s Apple Watch" but that's obviously not a solid solution.Blount
Do you have any solution for this?Manchukuo
M
8

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".

Mako answered 10/3, 2016 at 22:35 Comment(0)
U
6

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.

Undulate answered 23/7, 2015 at 6:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.