3

In writing sample code to understand how HomeKit works, I found that I don't get the the current value if I simply use the .value property such as:

let theValue = serviceCharacteristic.value

Rather, I'd run the standard Apple HomeKit application which seems to sync everything up with the database, run my application again and now I'll get the current value.

Is it only possible to truly get the device characteristic's current value only by using readValue?

func readValue(completionHandler completion: @escaping (Error?) -> Void)

If not, what other method should I be using?

Dead Pixel
  • 433
  • 4
  • 9

2 Answers2

1

Yes, the database holds the relatively static information about the characteristic such as whether it is readable or writable, but does not hold the current actual value of a characteristic. For that you need to communicate with the accessory itself and readValue (and writeValue) accomplish that. These are executed by the system and run asynchronously to your thread, so you need a completion handler to receive the results when the system gets done and comes back to deliver them to your app.

Wayne Henderson
  • 237
  • 3
  • 9
0

Yes, you have to readValue to communicate with the device and have it return the current value. When you writeValue, it should return with whether the request was successful. So you don't need to read it again after a change.

Wayne Henderson
  • 237
  • 3
  • 9