2016-10-03 13 views
0

所以我試圖寫一個簡單的應用程序來控制我的家庭套件設備,但我遇到了代碼的問題。在HomeKit模擬器中添加服務後,新增配件是否提供服務?

這是我瀏覽home套件配件的代碼。但是,當我

print(accessory.services.count) 

我得到0。下面是我的一些代碼:

import UIKit 
import HomeKit 

class DiscoveryTableViewController: UITableViewController, HMAccessoryBrowserDelegate { 

//outlets 
@IBAction func backButton(_ sender: AnyObject) { 
    navigationController?.dismiss(animated: true, completion: nil) 
} 

@IBOutlet weak var saveButton: UIBarButtonItem! 



let homeManager = HMHomeManager() 
let browser = HMAccessoryBrowser() 

var accessories = [HMAccessory]() 
var selectedAcc: HMAccessory? 

required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 

    //setting the title to "Searching..." 
    title = "Searching..." 

    //setting the browser delegate = to self 
    browser.delegate = self 
    //searching for accessories 
    browser.startSearchingForNewAccessories() 

    //only searching for a short time to be efficient 
    Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(DiscoveryTableViewController.stopSearching), userInfo: nil, repeats: false) 


} 



func stopSearching() { 
    title = "Discovered" 
    browser.stopSearchingForNewAccessories() 
} 


override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

// MARK: - Table view data source 

override func numberOfSections(in tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 1 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    return accessories.count 

} 


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cellIdentifier = "DiscoveryTableViewCell" 
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! DiscoveryTableViewCell 
    let accessory = accessories[(indexPath as NSIndexPath).row] as HMAccessory 

    // Configure the cell... 
    cell.discNameLabel.text = accessory.name 

    //print(accessory.services.count) 

    return cell 
} 

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    selectedAcc = accessories[indexPath.row] 
    performSegue(withIdentifier: "showDeviceDetail", sender: self) 
    //performSegue(withIdentifier: "UnwindToDeviceTable", sender: self) 
} 


override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "showDeviceDetail" { 
     if let destination = segue.destination as? AddDeviceViewController { 
      destination.addedAcc = selectedAcc 
     } 
    } 
} 


// Informs us when we've located a new accessory in the home 
func accessoryBrowser(_ browser: HMAccessoryBrowser, didFindNewAccessory accessory: HMAccessory) { 
    accessories.append(accessory) 

    //PRINTING SERVICES.COUNT 
    print(accessory.services.count) 
    tableView.reloadData() 
} 



func accessoryBrowser(_ browser: HMAccessoryBrowser, didRemoveNewAccessory accessory: HMAccessory) { 
    var index = 0 
    for item in accessories { 
     if item.name == accessory.name { 
      accessories.remove(at: index) 
      break; // done 
     } 
     index += 1 
    } 
    tableView.reloadData() 
} 


} 

在這個類中,我瀏覽我的所有配件。我可以訪問所有設備的所有名稱,但是當我打印「services.count」時,我每次都得到0。有人可以幫忙嗎?是的,我已在HomeKit模擬器中的每個附件下添加了服務和特性。

+0

您是否配對了配件並將其添加到了家中?在配對和添加配件到家之後,我正在循環服務而沒有問題。我將HomeKit Accessory Simulator與我創建的配件一起使用。 –

回答

1

我只是證實了這一點,直到後您添加一個附件到一個家庭,這意味着你已經與附件配對服務不可用:

homeManagerInstance.primaryHome?.addAccessory(accessory, completionHandler: { (error) in 
    print("ACCESSORY SERVICES: \(accessory.services.count)") 
}) 

如果試圖在此之前,要檢查service.count ,它確實回到零。

+0

所以我把你的代碼放在我的代碼中,然後它開始配對過程,它會顯示「輸入家庭套件配對號碼」或任何它所說的菜單,但隨後消失並顯示「HomeKit無法配對這個設備」。你知道那可能是什麼嗎?在我改變輸入代碼之前,它會消失 – Jolaroux

+0

不確定那裏發生了什麼,因爲這些屏幕都是由iOS神奇地生成和顯示的。只有想到我已經擺脫了我的頭頂,那就是一旦配對屏幕彈出,請不要點擊它。只需輸入代碼即可。 –

+0

即使我沒有輸入任何內容,屏幕仍然會在一兩秒鐘內消失。我也很困惑:/我把你的代碼放在那裏所以我真的不知道發生了什麼haha – Jolaroux