Reputation: 9596
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:
And sometimes:
Expected results:
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);
}
}];**/
}
}];
Upvotes: 2
Views: 4128
Reputation: 2980
After 5 different combinations and several hours of testing, here are my findings -
I had this in my viewDidLoad in consecutive lines and it DID NOT work a 100% of the time
self.accessoryBrowser = [HMAccessoryBrowser alloc] init];
[self.accessoryBrowser startSearchingForNewAccessories];
E.g. If you use it in a view controller then that view controllers code should look like -
-(instanceType)init{
if(self = [super init]){
self.accessoryBrowser = [HMAccessoryBrowser alloc] init]; }
}
-(void)viewDidLoad{
[self.accessoryBrowser startSearching];
}
So I reckon you should initialise HMAccessoryBrowser
before.
The instance of HMAccessoryBrowser should be alive until the addition of accessory to home is complete.
Do not call stopSearchingForNewAccessories
until the addition of accessory to home is complete.
So talking about #2 and #3
startSearchingForNewAccessories
, then you call [self.home addAccessory: completion:]
on it.Until you complete this operation successfully, DO NOT call stopSearchingForNewAccessories
and keep the instance of HMAcccessoryBrowser alive.
I was searching for my homekit accessory, on finding it I would stop searching and then try to add the accesory to my home. I would get the ErrorCode 2 almost every single time. Once I stopped calling stopSearching
i was seeing better results
discoveredAccessories
array. Instead, I would recommend using the object received from the delegate callback - (void)accessoryBrowser:(HMAccessoryBrowser *)browser didFindNewAccessory:(HMAccessory *)accessory
Upvotes: 2
Reputation: 3742
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.
Upvotes: 2