Accessory Notification method not called

Hello,


I have a class CLightBulb and it is defined as a accessory delegate:


class CLightBulb: NSObject, HMAccessoryDelegate


In the “init?()” I set self as the delegate:


m_lightBulb.delegate = self


And I override the “didUpdateValueFor” method as follows, but it is never invoked when I turn a lightbulb on or off from the Homekit app on another device (other than the one I am testing on)


//_______________________________

// AccessoryDelegate: Update Characteristic Value

//

//

//_____

func accessory(_ accessory: HMAccessory, service: HMService, didUpdateValueFor characteristic: HMCharacteristic)

{

var results: CResults

print("******------> CLightBulb AccessoryDelegate: Update Characteristic View")

results = CResults()

if ((accessory == m_lightBulb) && (service == m_lightBulbService))

{

switch characteristic.characteristicType

{

case HMCharacteristicTypePowerState:

results = GetThePowerStateFromTheLightBulb(workComplete:

{ () in

let delegate = self.m_delegate

if (results.GetResultCode() != CResults.code.success)

{

// NOTE: The attribute value is incorrect for Philips HUE bulbs so we have to check the error description

// to see if the reason for the error is that its unreachable. It's not really an error if its unreachable

if (self.m_isReachable == false)

{

delegate?.lightBulbReachablilityChanged!(device: self, newState: self.m_isReachable)

}

else

{

delegate?.lightBulbEncounteredError(device: self, opResults: results)

}

}

else

{

delegate?.lightBulbPowerStateChanged!(device: self, newState: self.m_powerState)

}

})

default:

print("CLightBulb: Encountered unexpected service type in accessory characteristic update.")

}

}

}



But when I test my app on my phone, it doesn’t get called when I, say, turn the light bulb off from the Homekit app on my iPad. Do you know what I’m missing?

Update: When my app first loads it sets a Home manager delegate and the initial delegate method does get called which triggers my app to look for rooms. However, if I add a room from another device the "added room" deletgate doesn't get invoked either.

Update:


The entry point to my app is a Tab View Controller and it's first tab is my "Home" ViewController. This is where I have the Home Manager object declared as a class property. As a test, I added HMAccessoryDelegate to this view controller class definition and put in test code to get the first room and it's first accessory, a light bulb, and store them as class properties.


Now when I use the HomeKit app on my iPad to turn the light bulb on and off, my app (running on my phone) receives the updates from this test code in the "Home" view controller. However, if I remove the test code those updates don't come through to the "Room" view controller where I am actually displaying the accessories.


I'm thinking that the Home Manager object is being deallocated or something like that. Anyone? (thanks!)

I found that those methods mentioned in my comment above are not invoked consistently.

If I just load my app on my phone and then use my iPad to turn a light on and off, the delegate method doesn’t get invoked. However, if I load the HomeKit app on my phone and then switch back to my app it will start getting the notifications (in the home view). If I tap on the room where the light is from my app and then go back to the home view, the method is no longer getting invoked. If I switch to the HomeKit app on my phone again, and then back to my app, the method starts getting invoked again.

Did you get anywhere with this? I'm observing similar behavior.

call this method

open func enableNotification(_ enable: Bool, completionHandler completion: @escaping (Error?) -> Swift.Void)

@TheRealStarone did you ever get this class figured out? I'm trying to do a HomeKit solution for lights and the Hue keeps throwing an error for reachability and I can't figure out how to work around it. You have any sample code up on GitHub?

Accessory Notification method not called
 
 
Q