2017-05-31 32 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配置」選項....... –

相關問題