2
votes

I have been unable to convert CBCharacteristic's value property which is of type NSData to NSString. I tried the usual initWithData:encoding: method of NSString as given below. But it returns nil.

NSString *str = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
NSLog(@"%@", str);

I am able to see the string value on console when I do NSLog on value directly.

NSLog(@"characteristic.value: %@", characteristic.value);

Can aanybody give any idea on how to get the string value out of characteristic.value?

1
Not all binary data (NSData) have a meaning in UTF8 String. You'll probably get nil if you convert [[NSString alloc] initWithData:UIImagePNGRepresentation(someUIImage) encoding:NSUTF8StringEncoding];.Larme
@Larme: Yes, correct. But in this case it is a known characteristics which has a hexadecimal no as string(uuid format). I am able to see the value of it when I print the data. NSLog(@"characteristic.value: %@", characteristic.value);saikamesh
Show us the value and the result you want then.Larme
It prints. value: <11122334 45566778 899aabbc cddeeff0 00>. This is the default value assigned to the characteristics. I have hardwired this value in my code. If the characteristics has this value then I will have to do some calculation and change the value of it to something else.saikamesh
If you just want to check that it's equal, you can use stackoverflow.com/questions/7317860/… to convert the @"11122334 45566778 899aabbc cddeeff0 00" as NSString into NSData and do a isEqual:.Larme

1 Answers

0
votes
  • You need to know what means the NSData. Could be a String, struct, uint32, etc. See here: core-bluetooth-get-all-features-description

  • Is BLevel for battery level? You need to do reverse engineering or read the doc (BLE doc if it's a "known" one, or device doc)