Page 1 of 1

Parsing Bluetooth Data

Posted: July 9th, 2015, 2:04 am
by horseshoe7
I'm working with the Bluetooth LE spec of the rower and don't quite understand the spec for the rowing service. I'm currently parsing the data coming in from the rowing service and don't know how to interpret the bytes.

The spec says (for example UUID 0x0037),
Data bytes packed as follows:
Elapsed Time Lo (0.01 sec lsb),
Elapsed Time Mid,
Elapsed Time High,
Distance Lo (0.1 m lsb),
Distance Mid,
Distance High,
... etc.
Which means what? There are 18 bytes, and 18 fields. So each of these fields (above only the first 6 shown) has 1 byte to convey a data value. So, I assume I have a value of 0-255 to map to a real quantity. But how? I wrote this code, but to be honest I'm not sure how to interpret these bytes.

Code: Select all

    const uint8_t *payload = [data bytes];  // data is the characteristic.value
    
    NSTimeInterval secondsFraction = 0.01 * (uint8_t)payload[0];  // so the byte, 0-255, is multiplied by 0.01 and that's how many 100ths of a second??  Shouldn't it then have a value somewhere between 0-99 ?
    NSTimeInterval seconds = (NSTimeInterval)1.f * (uint8_t)payload[1];  // likewise, this value would be 0-59 ?
    NSTimeInterval minutes = (NSTimeInterval)1.f * (uint8_t)payload[2];  // what if I am rowing for longer than 255 minutes?  See, I think I don't understand...
I also have this question as a part of this thread: http://www.c2forum.com/viewtopic.php?f=15&t=81699

Re: Parsing Bluetooth Data

Posted: July 9th, 2015, 5:31 am
by dxpack
Objective-C is like a foreign language to me :)

But I think I can help a bit anyway - I'll use Swift, but there's not much code here so you should be able to follow, and anyway you're 99% there.

Taking Elapsed Time as an example:

Code: Select all

// Unpack the characteristic.values (NSData) into an Array of 8-bit integers - this part is probably to most different in Swift:
var values = Array<UInt8>(count:characteristic.values.length, repeatedValue:0)
data.getBytes(&values, length:characteristic.values.length)

var elapsedTime: NSTimeInterval = 0.0

// At this point we're not guaranteed to have the expected array length (characteristic.values may be shorter than we assume):
if values.count >=3 { // but for this example, we only need to be certain we have 3 bytes for Elapsed Time
    elapsedTime = (Double(values[0]) + Double(values[1]) * 256 + Double(values[2]) * 65536) / 100
}
The elapsedTime variable now contains the Elapsed Time value in seconds with 1/100ths precision.

Re: Parsing Bluetooth Data

Posted: July 9th, 2015, 5:32 am
by horseshoe7
Allow me to answer my own question, may it be of help to you:
horseshoe7 wrote: Data bytes packed as follows:
Elapsed Time Lo (0.01 sec lsb),
Elapsed Time Mid,
Elapsed Time High,
Distance Lo (0.1 m lsb),
Distance Mid,
Distance High,
... etc.
For these two properties above, you have to use the 3 bytes as one value, like this:

Code: Select all



uint32_t decimetres;  // because the LSB is 0.1m, this means the 32-bit unsigned int value is counting decimetres.
[data getBytes:&decimetres range:NSMakeRange(3, 3)];
NSLog(@"Distance in m: %.1f", (float)decimetres/10.f);
EDIT: dxpack, looks like we posted our response at the same time!