-2

What is sample code to turn outlet on/off via Homekit SDK in Swift for IOS phone app?

Outlet name is SYLVANIA Outlet-4515, room is Bedroom, home is Holmdel.

  • SO is not a code writing service. You need to do your own research, have a go, and then if you hit specific problems ask for help with them. – flanker Aug 19 '21 at 00:51

1 Answers1

0

solved...

var accessories: [HMAccessory] = [] var home: HMHome?

var toggleState: Bool = false var indexPathRow = 0

// For discovering new accessories let browser = HMAccessoryBrowser() var discoveredAccessories: [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.")
  }
 //
}