extractMeasurementData static method
Extracts measurement data from raw blocks.
Each block is 18 bytes: 2-byte block index (little endian) + 16 bytes data. The last block may be smaller (2-byte index + remaining data). Blocks are sorted by their index to handle out-of-order BLE delivery. Returns the measurement data with block indices stripped.
Implementation
static Uint8List extractMeasurementData(Uint8List rawData) {
if (rawData.isEmpty) return rawData;
// Step 1: Parse blocks into a map keyed by block index
final Map<int, List<int>> blockMap = {};
int offset = 0;
while (offset < rawData.length) {
// Read 2-byte block index (little endian)
if (offset + 2 > rawData.length) break;
final blockIndex = (rawData[offset + 1] << 8) | rawData[offset];
offset += 2;
// Read up to 16 bytes of data
final remaining = rawData.length - offset;
final dataLen = remaining > 16 ? 16 : remaining;
if (dataLen > 0) {
blockMap[blockIndex] = rawData.sublist(offset, offset + dataLen);
offset += dataLen;
}
}
// Step 2: Sort blocks by their index
final sortedBlockIndices = blockMap.keys.toList()..sort();
// Step 3: Combine sorted blocks
final output = BytesBuilder();
for (final blockIndex in sortedBlockIndices) {
output.add(blockMap[blockIndex]!);
}
return output.toBytes();
}