Understanding NSData?!?!?!?

I'm dying here! Spent so many hours trying to understand how to get this bluetooth characteristic data to parse. My device is a cycling power meter which has the first two bits as the flags for the device. This is where my issue is. I can connect just fine to devices with one bit as the flag but two is killing me.

Characteristic: <CBCharacteristic: 0x282298f00, UUID = 2A63, properties = 0x10, value = {length = 12, bytes = 0x2c080000235134005e4348f0}, notifying = YES>

Code Block
NSData *data = [characteristic value];
const uint8_t *byteData = [data bytes];
uint16_t flags = byteData[0];
NSLog(@"measurement flag byte %lu", (unsigned long)flags);
NSLog(@"flag bit 0: %d", (bool)(flags & 0x00));
NSLog(@"flag bit 1: %d", (bool)(flags & 0x01));
NSLog(@"flag bit 2: %u", (bool)(flags & 0x02));
NSLog(@"flag bit 3: %d", (bool)(flags & 0x03));
NSLog(@"flag bit 4: %d", (bool)(flags & 0x04));
NSLog(@"flag bit 5: %d", (bool)(flags & 0x05));
NSLog(@"flag bit 6: %d", (bool)(flags & 0x06));
NSLog(@"flag bit 7: %d", (bool)(flags & 0x07));
NSLog(@"flag bit 8: %d", (bool)(flags & 0x08));
NSLog(@"flag bit 9: %d", (bool)(flags & 0x09));
NSLog(@"flag bit 10: %d", (bool)(flags & 0x10));
NSLog(@"flag bit 11: %d", (bool)(flags & 0x11));
NSLog(@"flag bit 12: %d", (bool)(flags & 0x12));
offset = offset + 2;
uint16_t powerMeasurment = CFSwapInt16LittleToHost(*(uint16_t *)(&[[characteristic value] bytes][offset]));
NSLog(@"Power: %u", powerMeasurment);
offset = offset + 2;


I believe based on the data I think the flags should be "844" Which is swapping the first and second bit and concatenating them.

Then I'm not sure if I am getting the bit from the flags correctly either, I'm a mess.

Thank you greatly in advance.
Seems fragments of codes and inconsistent description is blurring your intention.

I guess the code example is made of two parts, line 1 to 20 Attempt 1, and line 22 to 24 Attempt 2.
If no, what you have shown does not make sense, as you say you want to get the first 16-bit flags, but you read the first *one* byte from the data, and skipping one byte, reading the next two bytes at offset 2.

the flags should be "844" 

I guess the value "844" is represented in hexadecimal, no? Please explain the reason why do you think you can expect 0x844.

The output Characteristic: <CBCharacteristic: 0x282298f00, UUID = 2A63, properties = 0x10, value = {length = 12, bytes = 0x2c080000235134005e4348f0}, notifying = YES> is showing the first byte is 0x2c followed by the next byte 0x08.

It represents a bit pattern 00101100 and 00001000. You cannot make 0x844 from these two bytes with concatenating them.

According to the official doc he UUID = 2A63 Cycling Power Measurement, the flags 0x082c has these meanings:
  • Pedal Power Balance Present: False

  • Pedal Power Balance Reference: Unknown

  • Accumulated Torque Present: True

  • Accumulated Torque Source: Crank Based

  • Wheel Revolution Data Present: False

  • Crank Revolution Data Present: True

  • Extreme Force Magnitudes Present: False

  • Extreme Torque Magnitudes Present: False

  • Extreme Angles Present: False

  • Top Dead Spot Angle Present: False

  • Bottom Dead Spot Angle Present: False

  • Accumulated Energy Present: True

  • Offset Compensation Indicator : False

Are these really against the specs of the device?



The first part from line 1 to 20 is to create the 0x082C, now that I see how it reads. But the value is 0x2c08. So how do I get the flags data if the first and second bit are swapped? How do I create the flags correctly to then read them?

Code Block
NSData *data = [characteristic value];
const uint8_t *byteData = [data bytes];
uint16_t flags = byteData[0];

I appreciate your help!

uint16_t flags = byteData[0];

You need to read two bytes, not a single byte.
Something like this:
Code Block
uint16_t flags = (uint16_t)byteData[0] | ((uint16_t)byteData[1] << 8);


Understanding NSData?!?!?!?
 
 
Q