0

I am trying to read the value that I store in the lightBlue application but the problem is it cannot seems to get the string. It detects that I am reading the data but when I convert the data to a string it outputs @""

 -(void) peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
   if([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"2A29"]]){
     if (characteristic.value) {
         NSString *stringFromData = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
        NSLog(@"The String is %@", stringFromData);
        self.manufacturer = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
        NSLog(@"Manufacturer Name = %@", self.manufacturer);
     }
   }

}
user2076774
  • 405
  • 1
  • 8
  • 21
  • I did some more testing and the NSData sent is in HEX format, how would I convert hex to string? I just NSlogged the Data and it outputted hex values – user2076774 Nov 19 '13 at 07:51

1 Answers1

0

Try using this code NSString *stringFromData = [[NSString alloc] initWithData:characteristic.value encoding:NSASCIIStringEncoding]; NSLog(@"The String is %@", stringFromData);

manojdeshmane99
  • 206
  • 3
  • 10
  • I tried and it didn't work, still getting @"" as the value for the string – user2076774 Nov 22 '13 at 00:49
  • if I type "hey there" as the string the output will be 00000000 0f0e2000 00686579 20746865 726520 which should correspond to "Hey there" but in hex – user2076774 Nov 22 '13 at 00:59
  • 2
    it may works. NSMutableString *stringFromData = [NSMutableString stringWithString:@""]; for (int i = 0; i < characteristic.value.length; i++) { unsigned char _byte; [characteristic.value getBytes:&_byte range:NSMakeRange(i, 1)]; if (_byte >= 32 && _byte < 127) { [stringFromData appendFormat:@"%c", _byte]; } } NSLog(@"The String is %@", stringFromData); – manojdeshmane99 Nov 23 '13 at 12:32
  • Reference from here.[http://stackoverflow.com/questions/15276620/nsstring-containing-hex-convert-to-ascii-equivalent] – manojdeshmane99 Nov 23 '13 at 12:33
  • Do you mind making that comment an answer and I will mark it as the correct answer since it works! Thanks a lot! – user2076774 Nov 26 '13 at 01:11