fromPacket static method
fromPacket creates a AbPacket from a string packet in the format of Layrz Protocol v3.
Implementation
static AbPacket fromPacket(String raw) {
if (!raw.startsWith('<Ab>') || !raw.endsWith('</Ab>')) {
throw ParseException('Invalid identification packet, should be <Ab>...</Ab>');
}
final parts = raw.substring(4, raw.length - 5).split(';');
int? receivedCrc = int.tryParse(parts[parts.length - 1], radix: 16);
int? calculatedCrc = calculateCrc("${parts.sublist(0, parts.length - 1).join(';')};".codeUnits);
if (receivedCrc != calculatedCrc) {
throw CrcException('Invalid CRC, received: $receivedCrc, calculated: $calculatedCrc');
}
return AbPacket(devices: BleData.fromPackets(parts.sublist(0, parts.length - 1).join(';')));
}