-1

在 Swift for IOS 手机应用程序中通过 Homekit SDK 打开/关闭插座的示例代码是什么?

分店名称是 SYLVANIA Outlet-4515,房间是 Bedroom,家是 Holmdel。

4

1 回答 1

0

解决了...

var 附件:[HMAccessory] ​​= [] var home:HMHome?

var toggleState: Bool = false var indexPathRow = 0

// 为了发现新的附件 let browser = HMAccessoryBrowser() var foundAccessories: [HMAccessory] ​​= []

@IBAction func onoff(_ sender: Any) {

print ("indexPathRow: ", indexPathRow)

toggleState = toggleState ? false: true
let accessory = accessories[indexPathRow]

guard let characteristic = accessory.find(serviceType: HMServiceTypeOutlet, characteristicType: HMCharacteristicMetadataFormatBool) else {
  return
}


characteristic.writeValue(NSNumber(value: toggleState)) { error in
  if error != nil {
    print("Something went wrong when attempting to update the service characteristic.")
  }
 //
}
于 2021-08-26T00:13:36.247 回答