Posts

Post not yet marked as solved
9 Replies
972 Views
Hello Coders,I'm new to Swift, iOS, HomeKit and Delegates. I'm trying to get a HomeKit Accessory to notify my function when a value changes. I'm testing with Brightness of a lamp for now. In other parts of my app I can read and write brightness.I've created two different functions to receive the brightness change. I realize that I will only need one, once I learn more. I'm able to enable Notifications for this one device.Any suggestions would be helpful, including general coding style!Thanks Steveimport Foundation import HomeKit //let hkHomeManager = HMHomeManager() let hkHomeManager = HomeKitManagerClass() let hkAccessoryManager = HomeKitAccessoryClass() let myCharTypes = readCharacteristicsCSV() protocol HomeKitAccessoryDelegate { func didUpdateValueFor (_ accessory: HMAccessory, service: HMService, didUpdateValueFor characteristic: HMCharacteristic) func didReceiveBrightness(value: Int) } class HomeKitAccessoryClass: NSObject, HMAccessoryDelegate { var accDelegate:HomeKitAccessoryDelegate? func didUpdateValueFor (_ accessory: HMAccessory!, service: HMService!, didUpdateValueFor characteristic: HMCharacteristic) { var jsmAcc: HMAccessory = HMAccessory() var jsmSer: HMService = HMService() var jsmChar: HMCharacteristic = HMCharacteristic() if let accDelegate = self.accDelegate { accDelegate.didUpdateValueFor(jsmAcc, service: jsmSer, didUpdateValueFor: jsmChar) print ("Did Update Value For...") print ("Did Update Value For...") } } func brightnessUpdated () { var brightnessReceived: Int = Int() if let accDelegate = self.accDelegate { accDelegate.didReceiveBrightness(value: brightnessReceived) print ("Did Update Value For...") print ("Did Update Value For...\(brightnessReceived)") } } func hkNotifications(idIn: String) { if appSettings.key.homeKitAllowed == false { return } let timeStart = CFAbsoluteTimeGetCurrent() if let index = getDeviceIndexById(idIn) { if (globalDevices.deviceList[index].displayName).contains("Couch") == false { return } // Test ONLY LR Couch Light print("hkNotificaitons Start \(globalDevices.deviceList[index].displayName)") let oneDevice = globalDevices.deviceList[index].hkDevice // Device = Accessory from HomeKit if oneDevice == nil { return } // no HomeKit Accessory Found if oneDevice!.isReachable == false && oneDevice!.isBlocked == true { return } // Don't try to access this device} for indexServices in 0..<oneDevice!.services.count { let hkCharacteristics = oneDevice!.services[indexServices].characteristics for indexCharacteristics in 0..<hkCharacteristics.count { let hkChar = hkCharacteristics[indexCharacteristics] if hkChar.isNotificationEnabled { print("hkNotificaitons are ALREADY enabled for: \(globalDevices.deviceList[index].displayName)") } else { if hkChar.characteristicType == "00000008-0000-1000-8000-0026BB765291" { // Test ONLY Brightness for now hkChar.enableNotification(true, completionHandler: { error in if error != nil { print("\(hkChar), Error in Notification Sets \(error!.localizedDescription)") } else { print("hkNotificaitons are set to TRUE for: \(globalDevices.deviceList[index].displayName)") } } ) } } } } } } }
Posted Last updated
.
Post marked as solved
2 Replies
662 Views
First time poster and new developer...I'm working on a simple app to see the status of HomeKit devices, additionally it will be able to change the state of many of those devices.It will not have the capability to create new homes, rooms, accessories.I have 10 HK devices in my home and can control them from the built it Home App on my personal iPhone 11.Additionally, I have 12 HK devices created to a second home from the Simulator. The name of that home is Simulator. I can access them also from the Home app.From my iPhone 11 I can access both the real home and the simulator home.When I test in xCode to my iPhone 11, I can access both my real home and the simulator home. It reads both homes from the one app. I can control both real and simulated devices from my app. I was rather pleased with the outcome. I cannot access or control any HK device from a iOS Simulated iPhone device. My iPhone reads and controls all devices as expected when connected to mac & xCode via USB & wireless. My iPhone reads and controls devices as expected even when not running from Xcode.But then...I submit to App Store Connect and run via Test Flight and now FAIL.My app does not see any "homes" from homeManagerDidUpdateHomes Delegate//Accessing HMHomeManager.homes = 2 devices// to delegate = 2 homes from delegate didInitializeHomeManager//Auth Status raw data = 5// read homes.accessories// read services// read the values from the services// readValue of various devices including Brightness// writeValue Brightness// switch On / Off
Posted Last updated
.