EAWiFiUnconfiguredAccessory浏览器第二次未检测到外部配件

时间:2015-11-25 09:45:10

标签: ios objective-c homekit ios-homekit

我正在通过HMCatalog示例应用程序实现Homekit应用程序。一切都很好,但在添加配件视图中,我们使用 EAWiFiUnconfiguredAccessoryBrowser 来检测外部配件。在这第一次一切正常,但如果我回去出来,它不会显示外部配件。如果我从后台删除HMCatalog应用程序并重新启动,那么它运行正常。我的理解是,应用程序中有一些缓存。但我没有在应用程序中找到任何缓存相关的代码。谢谢宝贵的时间。请帮帮我。

代码是:

 // We can't use the ExternalAccessory framework on the iPhone simulator.
#if !TARGET_IPHONE_SIMULATOR
   self.externalAccessoryBrowser = [[EAWiFiUnconfiguredAccessoryBrowser alloc] initWithDelegate:self queue:dispatch_get_main_queue()];
#endif
      [self startBrowsing];

#pragma mark - EAWiFiUnconfiguredAccessoryBrowserDelegate methods

- (void)accessoryBrowser:(EAWiFiUnconfiguredAccessoryBrowser *)browser didFindUnconfiguredAccessories:(NSSet *)accessories 
{
            [self reloadTable];
}

- (void)reloadTable
{
    [self resetDisplayedAccessories];
    dispatch_async(dispatch_get_main_queue(), ^{
        [allDevicesTableView reloadData];
    });
}

- (void)resetDisplayedAccessories 
{
    [self.displayedAccessories removeAllObjects];
    [self.displayedAccessories addObjectsFromArray:[self allAccessories]];
}

- (NSArray *)allAccessories 
{
    NSMutableArray *allAccessories = [[NSMutableArray alloc] initWithCapacity:0];
    NSArray *discoveredAccessories = self.accessoryBrowser.discoveredAccessories;
    if (discoveredAccessories)
   {
        [allAccessories addObjectsFromArray:discoveredAccessories];
    }

    [allAccessories addObjectsFromArray:self.addedAccessories];
    NSArray *externalAccessories = self.externalAccessoryBrowser.unconfiguredAccessories.allObjects;
    if (externalAccessories) 
     {
        // ExternalAcessory framework may still contain an accessory for a little while after it's been configured.
        // If there is a HomeKit accessory with the same name as any external accessory, filter out the external accessory.
        NSPredicate *existingHomeKitAccessoryPredicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
            return ![[allAccessories valueForKeyPath:@"@distinctUnionOfObjects.name"] containsObject:evaluatedObject];
        }];
        NSArray *filteredExternalAccessories = [externalAccessories filteredArrayUsingPredicate:existingHomeKitAccessoryPredicate];
        [allAccessories addObjectsFromArray:filteredExternalAccessories];
    }

    NSSortDescriptor *nameDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name"
                                                                     ascending:YES
                                                                      selector:@selector(caseInsensitiveCompare:)];
    return [allAccessories sortedArrayUsingDescriptors:@[nameDescriptor]];

}

0 个答案:

没有答案