EDIT: I get the following error codes:
Error adding accessory The operation couldn’t be completed. (HMErrorDomain error 2.)
And:
Error adding accessory Failed to start pairing with the accessory [ name = xxxxx, providedName = xxxxx, uuid = xxxxx-xxxxx-xxxxx-xxxxx-xxxxx, identifier = xxxxx, configuration-app-id = (null), home = (null), bridge = (null) ]
Both with number 2.
What I don't understand is why on the HMCatalog app this works. What's wrong with my code? It works fine on the Accessory simulator but not on the real accessory (the real accessory is added only via the HMCatalog app but not my custom app).
Actual Behaviour:
- add accessory from my app (works the first time)
- reset accessory and then re-add it (does not work and gives pairing error in screenshot below). However when it does give those errors if I use the Apple example HMCatalog it does work.
And sometimes:
Expected results:
- adds accessory from my app too without pairing error
This is my add accessory code:
[self.home addAccessory:self.accessory completionHandler:^(NSError *error) {
NSLog(@"in adding for accessory %@", self.accessory.name);
if (error) {
NSLog(@"Error adding accessory %@ %li", error.localizedDescription, (long)error.code);
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:@"Pairing error"
message:error.localizedDescription
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"OK", @"OK action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(@"OK action");
}];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
}
else{
// TODO: Tweak this
NSLog(@"Added to home");
[self dismiss:nil];
/**
[homeSweetHome assignAccessory:accessory toRoom:nil completionHandler:^(NSError *error) {
if (error) {
NSLog(@"Failed adding accessory %@ to room %@", accessory.name, room.name);
}
}];**/
}
}];


EDIT: per Tushar Koul's comment above, it looks like you need to ignore the discoveredAccessories array on the browser and instead construct your own array of objects from the accessoryBrowserDelegate (-accessoryBrowser:didFindNewAccessory and -accessoryBrowser:didRemoveAccessory).
After telling the browser to start searching, any accessories currently available will be passed to those methods.
HMErrorCode 2 is not found (see apple docs). This means that the accessory pointer that you have isn't valid anymore. This can be caused by grabbing an accessory object and then telling the accessory browser to start looking for accessories. It might also happen if the browser is deallocated before you add the accessory.
Make sure that you are getting a new HMAccessory for the HMAccessoryBrowser before you try to add the accessory to your home. If you can share more of the code showing where the HMAccessory that you're adding is coming from, I might be able to help more.