Search code examples
iosuuidhomekit

How do I get the device ID from the callback information when a HomeKit paired with a Matter device within an application


I'm trying to pair the Matter by calling HomeKit from within our app, I can successfully pair and the matter device is discovered via bonjour, but after the HomeKit pairing success callback, I can't get a similar UUID or other identifier. I invoked HomeKit with the following code

- (void)addAccessoriesWithCompletion:(void (^)(NSError * _Nullable))completion {
    if(@available(iOS 15.4, *)) {
        HMAccessorySetupRequest *setupRequest = [[HMAccessorySetupRequest alloc] init];
        setupRequest.homeUniqueIdentifier = self.currentHome.uniqueIdentifier;
        [self.setupManager performAccessorySetupUsingRequest:setupRequest completionHandler:^(HMAccessorySetupResult * _Nullable result, NSError * _Nullable error) {
                   completion(error);
        }];
    }
    else {
        [self.currentHome addAndSetupAccessoriesWithCompletionHandler:completion];
    }
}

- (HMAccessorySetupManager *)setupManager {
    if (_setupManager == nil) {
        _setupManager = [[HMAccessorySetupManager alloc] init];
    }
    return _setupManager;
}

HomeKit successfully added device delegate

- (void)home:(HMHome *)home didAddAccessory:(HMAccessory *)accessory {
    if (self.homeDidAddAccessoryBlock) {
        self.homeDidAddAccessoryBlock(accessory);
    }
    accessory.delegate = self;
}

Multiple matter devices may be discovered through bonjour, and I need to know which matter device I just added in order to bind to our user system. So I need to get the device id from the HomeKit pairing success callback so I can filter the matter device. Or do we need to set something on our matter firmware?


Solution

  • Since iOS16.1, HMAccessory provides a matterNodeID You can search for the _tcp._matter service in Bonjour. Then you can find NodeID in the name of the service, such as name = aaaaa-bbbbbb, convert bbbbbb to base 10, and you will find that bbbbbb is the same as matterNodeID, At this point, you can find the accessory you just added to HomeKit in bonjour