ospr
ospr

Reputation: 1671

Will BLE peripherals send a Characteristic's value automatically upon subscribing?

With CoreBluetooth I can subscribe to a peripheral's CBCharacteristic using:

[peripheral setNotifyValue:YES forCharacteristic:characteristic]

After subscribing should I expect to get a CBPeripheral delegate callback for peripheral:didUpdateValueForCharacteristic:error: with the characteristic having the latest value set in its value property?

Or is it more standard to require the central to first explicitly read that value, then subscribe to the characteristic? By "standard" I'm wondering if this is called out or recommended explicitly in the BLE specs somewhere (took a look and couldn't find anything).

Upvotes: 1

Views: 586

Answers (1)

Michał Ciuba
Michał Ciuba

Reputation: 7944

The documentation is pretty clear on that:

This method is invoked when your app calls the readValueForCharacteristic: method, or when the peripheral notifies your app that the value of the characteristic for which notifications and indications are enabled (via a successful call to setNotifyValue:forCharacteristic:) has changed.

You won't get the delegate callback peripheral:didUpdateValueForCharacteristic:error: immediately after subscribing. The peripheral will only send a notification after the characteristic's value has changed. So you have to call readValueForCharacteristic: on CBPeripheral to get the current value in this delegate callback.

Note that you usually get characteristic's value in peripheral:didDiscoverCharacteristicsForService:error: callback. Then you can subscribe to notifications to get notified when the characteristic changes its value.

Upvotes: 1

Related Questions