How to get notified when a CBPeripheral value changes ?

Hello,


I am still struggling with my CBPeripheral which requires several "connect" instructions before it actually sends some value (no replies from this previous post https://forums.developer.apple.com/message/339416#339416)


I am trying to use a didChangeValue(forKey:) instruction but I don't know what the key is supposed to be... I googled it of course but I can't find a piece of code thats even remotely looks like what I want.


Can someone give me an exemple ? Pleaaase ? Or any suggestion, I've been stuck for weeks...


Thanks

Replies

Just looked at doc, did not test.


Isn't the instance method of CBPeripheral done for this ?


Instance Method

setNotifyValue(_:for:)


Sets notifications or indications for the value of a specified characteristic.



Declaration

func setNotifyValue(_ enabled: Bool, for characteristic: CBCharacteristic)

Parameters

enabled

A Boolean value indicating whether you wish to receive notifications or indications whenever the characteristic’s value changes.

true
if you want to enable notifications or indications for the characteristic’s value.
false
if you do not want to receive notifications or indications whenever the characteristic’s value changes.
characteristic

The specified characteristic.

Discussion

When you enable notifications for the characteristic’s value, the peripheral calls the

peripheral(_:didUpdateNotificationStateFor:error:)
method of its delegate object to indicate whether or not the action succeeded. If successful, the peripheral then calls the
peripheral(_:didUpdateValueFor:error:)
method of its delegate object whenever the characteristic value changes. Because it is the peripheral that chooses when to send an update, your app should be prepared to handle them as long as notifications or indications remain enabled. If the specified characteristic is configured to allow both notifications and indications, calling this method enables notifications only. You can disable notifications and indications for a characteristic’s value by calling this method with the
enabled
parameter set to
false
.