无法在 didUpdateNotificationStateForCharacteristic 中获取 CBcharacteristic 值

Unable to get CBcharacteristics value in didUpdateNotificationStateForCharacteristic

我有一个已成功连接的 BLE 设备。在这种情况下,每当我向 BLE 设备发送字符串 "GET DATA" 时,设备都会通过向我发送特定响应来响应。我正在使用 didDiscoverCharacteristicsForService 来写入值 ("GET DATA")。我期待 didUpdateNotificationStateForCharacteristic 中的数据,但我每次都会得到响应。

 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
    if ([service.UUID isEqual:[CBUUID UUIDWithString:@"Some service UUID"]])
    {         
         for (CBCharacteristic *aChar in service.characteristics)
         {
             if ([aChar.UUID isEqual:[CBUUID UUIDWithString:@"Some UUID"]]) 
             {
                 NSString *str = @"GET DATA";
                 NSData *someData = [str dataUsingEncoding:NSUTF8StringEncoding];
                [self.RN2058Peripheral setNotifyValue:YES forCharacteristic:aChar];
                [self.RN2058Peripheral writeValue:someData forCharacteristic:aChar type:CBCharacteristicWriteWithResponse];
             }  
         }
     }
}



- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
    if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"Some UUID"]])
    {
        NSString *value = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
        NSLog(@"Value %@",value);

        NSData *data = characteristic.value;
        NSString *stringFromData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"Data ====== %@", stringFromData);
    }
}

didUpdateNotificationStateForCharacteristic 被调用以响应您对 setNotifyValue 的调用。

当外围设备上的特征值发生变化时,您将调用 didUpdateValueForCharacteristic 委托方法。在此方法中,您可以访问特征的值。