encodeNdefMessage function

Uint8List encodeNdefMessage(
  1. List<NDEFRecord> records, {
  2. bool canonicalize = true,
})

Encode an NDEF message (containing several NDEFRecords) to byte array. Set canonicalize to set the MB and ME fields automatically in the first / last record.

Implementation

Uint8List encodeNdefMessage(List<NDEFRecord> records,
    {bool canonicalize = true}) {
  if (records.length == 0) {
    return new Uint8List(0);
  }

  records.forEach((r) {
    r.flags.resetPositionFlag();
  });

  if (canonicalize) {
    records.first.flags.MB = true;
    records.last.flags.ME = true;
  }

  var encoded = <int>[];
  records.forEach((r) {
    encoded.addAll(r.encode());
  });

  return new Uint8List.fromList(encoded);
}