打印

setNotifyValue回调真正取值方法是didUpdateValueForCharacteristic

setNotifyValue回调真正取值方法是didUpdateValueForCharacteristic

翻开 CBPeripheral.h 中 setNotifyValue 方法的定义,didUpdateValueForCharacteristic

/*!
*  @method setNotifyValue:forCharacteristic:
*
*  @param enabled            Whether or not notifications/indications should be enabled.
*  @param characteristic    The characteristic containing the client characteristic configuration descriptor.
*
*  @discussion                Enables or disables notifications/indications for the characteristic value of <i>characteristic</i>. If <i>characteristic</i>
*                            allows both, notifications will be used.
*                          When notifications/indications are enabled, updates to the characteristic value will be received via delegate method
*                          @link peripheral:didUpdateValueForCharacteristic:error: @/link. Since it is the peripheral that chooses when to send an update,
*                          the application should be prepared to handle them as long as notifications/indications remain enabled.
*
*  @see                    peripheral:didUpdateNotificationStateForCharacteristic:error:
*  @seealso                CBConnectPeripheralOptionNotifyOnNotificationKey
*/
- (void)setNotifyValueBOOL)enabled forCharacteristicCBCharacteristic *)characteristic;

但是回头再来看看  Subscribing to a Characteristic’s Value 一节
https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/PerformingCommonCentralRoleTasks/PerformingCommonCentralRoleTasks.html#//apple_ref/doc/uid/TP40013257-CH3-SW16

Subscribing to a Characteristic’s ValueThough reading the value of a characteristic using the readValueForCharacteristic: method can be effective for static values, it is not the most efficient way to retrieve a dynamic value. Retrieve characteristic values that change over time—for instance, your heart rate—by subscribing to them. When you subscribe to a characteristic’s value, you receive a notification from the peripheral when the value changes.
You subscribe to the value of a characteristic that you are interested in by calling the peripheral’s setNotifyValue:forCharacteristic: method, specifying the first parameter as YES, like this:
    [peripheral setNotifyValue:YES forCharacteristic:interestingCharacteristic];

When you subscribe to (or unsubscribe from) a characteristic’s value, the peripheral calls the peripheral:didUpdateNotificationStateForCharacteristic:error: method of its delegate object. If the subscription request fails for any reason, you can implement this delegate method to access the cause of the error, as the following example shows:
- (void)peripheralCBPeripheral *)peripheral
didUpdateNotificationStateForCharacteristicCBCharacteristic *)characteristic
             errorNSError *)error {

    if (error) {
        NSLog(@"Error changing notification state: %@",
           [error localizedDescription]);
    }
    ...

于是乎:
didUpdateValueForCharacteristic 与 didUpdateNotificationStateForCharacteristic 打上架了?

不不不
看后面的

After you have successfully subscribed to a characteristic’s value, the peripheral device notifies your app when the value has changed. Each time the value changes, the peripheral calls the peripheral:didUpdateValueForCharacteristic:error: method of its delegate object. To retrieve the updated value, you can implement this method in the same way as described above in Reading the Value of a Characteristic

也就是说是整个过程是这样的
1、先是调用setNotifyValue声明要要订个媳妇;
2、然后didUpdateNotificationStateForCharacteristic告诉你有没有订阅成功;
3、最后,订阅成功了,仍是会通过回调
didUpdateValueForCharacteristic方法来取值!

三者之间的关系是这样的,我刚开始掉到坑里,以为是
didUpdateNotificationStateForCharacteristic直接就是回调拿数据并可以处理,当数据有变化时就得到数据了。但是等呀等,一直没有数据,然后 didUpdateValueForCharacteristic 方法却有数据出来,搞晕了。。。现在终于清楚了。
拥抱智慧地球!地球守护使者!

TOP