0
<CBCharacteristic: 0x156d48cf0, UUID = BLevel, properties = 0x18, value = <32353732 380d0a>, notifying = YES>

I want to extract what's there in value field.

NSData *data = c.value; // c is of type CBCharacteristic

But I don't know which kind of value it is?

Hemang
  • 26,840
  • 19
  • 119
  • 186
  • You need to know what means the `NSData`. Could be a String, struct, uint32, etc. See there: http://stackoverflow.com/questions/38718841/ios-core-bluetooth-get-all-features-description-from-device/38719661#38719661 Is `BLevel` for battery level? You need to do reverse engeenering or read the doc (BLE doc if it's a "known" one, or device doc) – Larme Oct 26 '16 at 18:08
  • @Larme, yes, it's "Battery level". Do you the type of it? – Hemang Oct 26 '16 at 18:13
  • According to the doc: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.battery_level.xml if it's the UUID 0x2A19, but seems long for a int8. – Larme Oct 26 '16 at 18:14
  • How to get the value out of it, can you please help? – Hemang Oct 26 '16 at 18:20
  • http://stackoverflow.com/a/23571000/1801544 ? – Larme Oct 26 '16 at 18:22
  • How/where did you print that information? Is it the result of reading the characteristic? Can you show more code in the context of the function? – Paulw11 Oct 26 '16 at 19:39
  • You got the `NSData` object (`c.value`), you just need then to do the same. It seems that CoreBluetooth is new for you, just look at the sample apps from Apple (Thermometer and Heart one). – Larme Oct 26 '16 at 19:50
  • @Paulw11, I just logs the value inside this delegate method, `- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error`. Yes, it's the result of `characteristic` object. There's no code. I am just unaware of how to translate the NSData to readable form. – Hemang Oct 27 '16 at 06:46
  • You can convert the data to a string using the `initWithData` initialiser of NSString. That data is just "25728" plus a carriage return and a line feed – Paulw11 Oct 27 '16 at 06:51

1 Answers1

2

You can convert the NSData to an NSString using

NSString *str = [[NSString alloc] initWithData:c.value encoding:NSUTF8StringEncoding];
Paulw11
  • 108,386
  • 14
  • 159
  • 186