fromPacket static method

PbPacket fromPacket(
  1. String raw
)
override

fromPacket creates a PbPacket from a string packet in the format of Layrz Protocol v3.

Implementation

static PbPacket fromPacket(String raw) {
  if (!raw.startsWith('<Pb>') || !raw.endsWith('</Pb>')) {
    throw ParseException('Invalid identification packet, should be <Pb>...</Pb>');
  }

  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 PbPacket(advertisements: BleAdvertisement.fromPacket(parts.sublist(0, parts.length - 1).join(';')));
}