HomeKit模拟器添加服务后添加的配件没有服务可用?
Added accessories have no services available after adding services in HomeKit simulator?
所以我正在尝试编写一个简单的应用程序来控制我的家庭套件设备,但我在编写代码时遇到了问题。
这是我用于浏览家居用品配件的代码。但是当我
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()
}
}
在此class,我正在浏览我所有的配饰。我可以访问所有设备的所有名称,但是当我打印“services.count
”时,我每次都得到 0。有人可以帮忙吗?是的,我在 HomeKit
模拟器的每个配件下添加了服务和特性。
我刚刚确认了这一点,只有在您将配件添加到家中后才能使用服务,这意味着您已经与配件配对:
homeManagerInstance.primaryHome?.addAccessory(accessory, completionHandler: { (error) in
print("ACCESSORY SERVICES: \(accessory.services.count)")
})
如果您在此之前尝试检查 service.count,它确实返回为零。
所以我正在尝试编写一个简单的应用程序来控制我的家庭套件设备,但我在编写代码时遇到了问题。
这是我用于浏览家居用品配件的代码。但是当我
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()
}
}
在此class,我正在浏览我所有的配饰。我可以访问所有设备的所有名称,但是当我打印“services.count
”时,我每次都得到 0。有人可以帮忙吗?是的,我在 HomeKit
模拟器的每个配件下添加了服务和特性。
我刚刚确认了这一点,只有在您将配件添加到家中后才能使用服务,这意味着您已经与配件配对:
homeManagerInstance.primaryHome?.addAccessory(accessory, completionHandler: { (error) in
print("ACCESSORY SERVICES: \(accessory.services.count)")
})
如果您在此之前尝试检查 service.count,它确实返回为零。