6 Replies
      Latest reply on Sep 10, 2018 12:12 AM by jiang188
      TheRealStarone Level 1 Level 1 (0 points)

        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?