HomkeKit开发者:"Enter setup code"页面只出现半秒就跳转到页面"Couldn't add DeviceName, Home couldn't connect to this accessory"

HomkeKit dev: "Enter setup code" page appears only half a second and jump to page "Couldn't add DeviceName, Home couldn't connect to this accessory"

我正在学习开发 HomeKit 应用程序。我正在尝试向 home/room 添加附件。当我 运行 项目时,它能够发现配件,但无法将其添加到家庭或为配件分配房间。

根据教程,项目发现配件如下:

当我给home/room添加配件时,"Enter setup code" 只出现了半秒,然后中间变成了"Couldn't add XXX, Home couldn't connect this accessory" ,如下图:

我用来给 room/home 添加附件的部分代码如下所示:

currentRoomVC.h

#import <UIKit/UIKit.h>
#import "MyHomeKit.h"
@interface currentRoomVC : UIViewController<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource,HMAccessoryBrowserDelegate>
@property (nonatomic,strong)HMRoom *currentRoom;
@property (nonatomic,strong)HMRoom *currentHome;

@property(nonatomic,strong) NSMutableArray *accArry;
@property(weak,nonatomic) IBOutlet UIScrollView *scroView;
@property(strong,nonatomic) IBOutlet UITableView *accTable;

@property(nonatomic,strong) HMAccessoryBrowser *browser;
@end

currentRoomVC.m

#import "currentRoomVC.h"

@interface currentRoomVC ()

@end

@implementation currentRoomVC
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = NO;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = YES;
    [self.browser stopSearchingForNewAccessories];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor yellowColor];
    self.navigationItem.title = self.currentRoom.name;

    self.accArry = [[NSMutableArray alloc] init];
    self.browser = [[HMAccessoryBrowser alloc]init];
    self.browser.delegate = self;

    [self configSearchBtn];
    [self cofigureTableview];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark -
#pragma mark accessoryBrowser



- (void) configSearchBtn{
    UIButton *addProject = [UIButton buttonWithType: UIButtonTypeRoundedRect];
    addProject.frame = CGRectMake(self.view.bounds.size.width/2-50, self.view.bounds.size.height / 4-10, 100, 40);
    [addProject setTitle:@"Add device" forState:UIControlStateNormal];
    [addProject addTarget:self action:@selector(searchDevice:) forControlEvents:UIControlEventTouchUpInside];
    addProject.backgroundColor = [UIColor redColor];
    [self.view addSubview:addProject];

}

- (void)searchDevice:(UIButton*) button
{
    NSLog(@"Start searching new devices...");
    [self.browser startSearchingForNewAccessories];
}




- (void) accessoryBrowser:(HMAccessoryBrowser *)browser didFindNewAccessory:(HMAccessory *)accessory{
    NSLog(@"Found a new device: %@", accessory.name);
    [_accArry addObject:accessory];
    NSLog(@"%lu", (unsigned long)_accArry.count);
    [self.accTable reloadData];
}

- (void) accessoryBrowser:(HMAccessoryBrowser *)browser didRemoveNewAccessory:(nonnull HMAccessory *)accessory{
    NSLog(@"Remove deveice %@", accessory.name);
}

#pragma mark -
#pragma mark tableview

-(void)cofigureTableview
{

    self.accTable = [[UITableView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height / 2,self.view.bounds.size.width,self.view.bounds.size.height / 2) style:UITableViewStylePlain];
    self.accTable.delegate = self;
    self.accTable.dataSource = self;
    [self.view addSubview:self.accTable];

}


- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return _accArry.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cellIdentifier";

    UITableViewCell *cell = [self.accTable dequeueReusableCellWithIdentifier:cellIdentifier];

    if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

    }
    HMAccessory *acc = _accArry[indexPath.row];
    cell.textLabel.text = acc.name;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
    NSLog(@"title of cell %@", [self.accArry objectAtIndex:indexPath.row]);
    NSLog(@"current room: %@", self.currentRoom.name);
    NSLog(@"current home: %@", self.currentHome.name);

    HMAccessory *acc = _accArry[indexPath.row];
    __block HMHome *home = self.currentHome;
    __block HMRoom *room = self.currentRoom;



    [home addAccessory:acc completionHandler:^(NSError * _Nullable error) {
        if (error)
        {
            // Failed to add accessory to home
            NSLog(@"Fail to add device to home %@",error.localizedDescription);
        }
        else
        {
            if (acc.room != room) {
                // 3. If successfully, add the accessory to the room
                [home assignAccessory:acc toRoom:room completionHandler:^(NSError * _Nullable error) {
                    if (error) {
                        // Failed to add accessory to room
                        NSLog(@"Fail to assign room to room");
                    }
                    else{
                        NSLog(@"Add this device to %@", room.name);
                    }
                }];
            }
        }
    } ];


}

如您所见,didSelectRowAtIndexPath 函数尝试捕获这两个错误,而 error.localizedDescription 仅显示 Failed to add the accessory

为什么我无法连接到配件?我哪里做错了?谢谢!!

在 iPhone 模拟器上,转到设置->隐私->HomeKit 和 select "Reset HomeKit Configuration"。

如果这不起作用,请删除 Xcode 并从 App Store 重新安装。

自己修复了!!!!!!!!!!!!

我在这里犯了一个错误。在功能上 - (void)viewWillDisappear:(BOOL)animated

我不应该包含 [self.browser stopSearchingForNewAccessories] ,否则如果我离开 currentRoom 页面,所有发现过程都将停止。所以正确的版本是

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = YES;
}

愚蠢的我也是天才!!!!! :)