ホームキットデバイスを制御する簡単なアプリを作成しようとしていますが、コードに問題があります。HomeKitシミュレータでサービスを追加した後に追加されたアクセサリはありませんか?
ここに私のホームキット付属品を閲覧するためのコードがあります。私はすべての私のアクセサリーを閲覧しています、このクラスで
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()
}
}
:私は
print(accessory.services.count)
私は0を得るときしかし、ここに私のコードの一部です。私はすべてのデバイスのすべての名前にアクセスできますが、 "services.count
"を印刷すると毎回0が返ってきます。助けてもらえますか?そして、はい、HomeKit
シミュレータの各アクセサリの下にサービスと特性を追加しました。
アクセサリーをペアにして、すでに家に追加しましたか?私はペアリングして家に付属品を追加した後、問題なくサービスをループしています。私は作成したアクセサリーでHomeKitアクセサリシミュレータを使用しています。 –