Search code examples
iosbluetooth-lowenergycore-bluetoothcbcentralmanagercbperipheral

Unable to get CBcharacteristics value in didUpdateNotificationStateForCharacteristic


I am having a BLE device to which I am successfully connected. In this case whenever I send a string "GET DATA" to the BLE device, the device will respond by sending a certain response back to me. I am using didDiscoverCharacteristicsForService to writeValue ("GET DATA"). I am expecting data in didUpdateNotificationStateForCharacteristic, but I am getting in response everytime.

 - (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);
    }
}

Solution

  • didUpdateNotificationStateForCharacteristic is called in response to your invocation of setNotifyValue.

    When the value of the characteristic is changed on the peripheral you will get a call to your didUpdateValueForCharacteristic delegate method. In this method you can access the characteristic's value.