I am using the CoreBluetooth
framework, where after I have updated a value for a characteristic I get the callback didUpdateValueForCharacteristic
from the peripheral. Here, I found a snippet do decode the data within the NSData
object returned:
- (void) peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
NSUInteger *flags = (NSUInteger*) [[[characteristic value] subdataWithRange:NSMakeRange(0, 1)] bytes];
NSUInteger length;
if(*flags & 0x01)
{
length = 2;
}
else
{
length = 1;
}
NSUInteger *measurement = (NSUInteger*) [[[characteristic value] subdataWithRange:NSMakeRange(1, length)] bytes];
NSLog(@"Value read from char: %d", *measurement);
}
Here the code throws an exception:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSConcreteData subdataWithRange:]: range {1, 1} exceeds data length 1'
Could someone explain why? The value I am supposed to read should be 1 byte.