2017-05-31 29 views
0

我学习开发HomeKit应用。我正在尝试为家中/房间添加配件。当我运行该项目时,它能够发现附件,但无法将其添加到家中或为附件分配空间。HomkeKit开发:“输入设置代码”页仅半秒,并跳转到页面“不能添加设备名称,首页无法连接到该附件”

按照教程,该项目发现如下的配件: enter image description here

当我将附件添加到主/间,“输入设置代码” 仅出现半秒,然后将其变为“不能添加XXX,首页无法连接此附件” intermediatly,如下图所示:

enter image description here

enter image description here 我所使用的部分代码添加附件室/家庭如下所示:

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

为什么我不能连接到配件?我哪里错了?谢谢!!

+0

请向我们展示整个代码文件。可能有其他开发人员需要帮助您。另外,你做了什么来解决你的问题? – moonman239

+0

@ moonman239谢谢你提醒我。我已经包括了我处理配件的全班。我没有做任何故障排除,因为我不知道我可以从哪里开始排除故障......当我在问题中更新时,我删除了两个“捕获错误”条件并让日志显示一些错误消息。 –

+0

我想我明白你来自哪里。附件在那里,否则,Home不知道你想要连接的是什么。 – moonman239

回答

0

由我自己定!!!!!!!!!!!

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

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

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

愚蠢的我,但也天才!!!!! :)

0

在iPhone模拟器,进入设置 - >与隐私> HomeKit并选择“重置HomeKit配置”。

如果还是不行,请删除Xcode和从App Store重新安装。

+0

我尝试了你所说的,删除Xcode并重新安装。没有任何改变.....并且Homekit页面没有“重置Homekit配置”选项....... –

相关问题