• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Swift中HomeKist发现和管理支持HomeKit的配件(五)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

如果我们想打开灯,或者别的控件,首先需要找到改控件,根据该控件找到该控件的服务,根据服务找到该控件的特征,根据特征的描述打开或者关闭该灯,这里只写到特征,控制特征参考下一篇

import UIKit
import HomeKit

class ViewController: UIViewController,HMHomeManagerDelegate,HMAccessoryBrowserDelegate{
    
    @IBOutlet weak var tableView: UITableView?
    var manager: HMHomeManager!
    /*HMAccessory控件,具体到冰箱,洗衣机,空调*/
    var accessories = [HMAccessory]()
    /*HMAccessoryBrowser查找控件的类,*/
    lazy var accessoryBrower:HMAccessoryBrowser = {
        let brower = HMAccessoryBrowser()
        brower.delegate = self
        return brower
    }()
    var home:HMHome!
    var room:HMRoom!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.manager = HMHomeManager()
        self.manager?.delegate = self;
    }
    //homekit初始化完毕
    public func homeManagerDidUpdateHomes(_ manager: HMHomeManager){
        home = self.manager.homes[0]
        room = home.rooms[0]
        //开始查找配件,支持HomeKit
        self.accessoryBrower.startSearchingForNewAccessories()
    }
    
    //发现一个新的配件  HMAccessoryBrowserDelegate
    public func accessoryBrowser(_ browser: HMAccessoryBrowser, didFindNewAccessory accessory: HMAccessory){
        print("-------------\(accessory.name)")
        home.addAccessory(accessory) {[weak self] (error) in
            let strongSelf = self!
            if error == nil{
                print("添加控件成功")
                strongSelf.home.assignAccessory(accessory, to: strongSelf.room, completionHandler: { (error) in
                    if error == nil{
                        print("把控件添加房间成功")
                        strongSelf.findServicesForAccessort(accessory: accessory)
                    }else{
                         print("把控件添加房间失败")
                    }
                })
            }else{
               print("添加控件失败")
            }
        }
        
        
    }
    //移除一个新的配件  HMAccessoryBrowserDelegate
    public func accessoryBrowser(_ browser: HMAccessoryBrowser, didRemoveNewAccessory accessory: HMAccessory){
        
    }
    
    func findServicesForAccessort(accessory:HMAccessory){
        print("发现服务")
        for service in accessory.services as [HMService]{
            print("--------service name:\(service.name)  type:\(service.serviceType)")
            //找到服务,遍历服务的特征
            findCharacteristicsOfService(service: service)
        }
    }
    func findCharacteristicsOfService(service:HMService){
        for characteristic in service.characteristics as [HMCharacteristic]{
            print("???????????????????? characteristic type:\(characteristic.characteristicType)")
        }
    }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift语言中与C/C++和Java不同的语法(三)发布时间:2022-07-13
下一篇:
Swift在HomeKit中管理用户住宅(HMHome)(二)发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap