toBytes method
Encodes this message in the NFC Forum wire format.
Chunking is never produced: every record is written whole, which is what real readers expect and what both platforms do.
Implementation
Uint8List toBytes() {
final out = BytesBuilder(copy: false);
for (var i = 0; i < records.length; i++) {
final record = records[i];
final isShort = record.payload.length < 256;
// The specification always writes the ID length byte for an empty record.
final hasId = record.identifier.isNotEmpty || record.typeNameFormat == NdefTypeNameFormat.empty;
var header = record.typeNameFormat.index & _maskTypeNameFormat;
if (i == 0) header |= _flagMessageBegin;
if (i == records.length - 1) header |= _flagMessageEnd;
if (isShort) header |= _flagShortRecord;
if (hasId) header |= _flagIdLengthPresent;
out.addByte(header);
out.addByte(record.type.length);
if (isShort) {
out.addByte(record.payload.length);
} else {
final length = record.payload.length;
out.add([(length >> 24) & 0xFF, (length >> 16) & 0xFF, (length >> 8) & 0xFF, length & 0xFF]);
}
if (hasId) out.addByte(record.identifier.length);
out.add(record.type);
out.add(record.identifier);
out.add(record.payload);
}
return out.takeBytes();
}