writeLongData method
Writes a sequence of data spanning multiple blocks to a Mifare Ultralight C RFID tag.
This method allows for writing data that exceeds the size of a single block by breaking the data into segments that fit within the tag's block size. It ensures that the total data length is a multiple of the block size and validates the byte values before writing.
Parameters
blockNumber
: The starting block number where the data writing begins. Must be within the valid range for the RFID tag's memory.data
: A list of integers representing the data to be written across multiple blocks. The length of this list must be a multiple of the block size.
Throws
InvalidDataException
if the data length is not a multiple of the block size or if any byte in the data list is outside the valid range (0x00 to 0xFF).RFIDException
if there's an error during the write operation, such as communication problems with the RFID reader or if attempting to write beyond the tag's memory limits.
Example
try {
await card.writeLongData(blockNumber: 4, data: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]);
print('Data successfully written starting from block 4');
} catch (e) {
print('Failed to write long data: $e');
}
Note: Ensure that the starting block number and data length are appropriately chosen to avoid writing beyond the RFID tag's memory capacity. The data must be carefully prepared to ensure integrity and compatibility with the tag's specifications.
Implementation
Future<void> writeLongData({
required int blockNumber,
required List<int> data,
}) async {
if (data.length % BLOCK_SIZE != 0) {
throw InvalidDataException(
'Invalid data length. Must be a multiple of $BLOCK_SIZE');
}
_validateBlockNumber(
blockNumber: blockNumber,
length: data.length,
start: DATA_ADDRESS_START,
end: DATA_ADDRESS_END,
);
try {
validateByteList(data);
} catch (e) {
throw InvalidDataException(
'Invalid data. Each byte must be within 0x00 and 0xFF');
}
try {
for (var i = 0; i < data.length; i += BLOCK_SIZE) {
await writeData(
blockNumber: blockNumber + (i / BLOCK_SIZE).floor(),
data: data.sublist(i, i + BLOCK_SIZE),
);
}
} catch (e) {
throw RFIDException('Error writing data: ${e.toString()}');
}
}