readLongData method

Future<List<int>> readLongData({
  1. required int blockNumber,
  2. int length = BLOCK_SIZE,
})

Reads a sequence of data spanning multiple blocks from a Mifare Ultralight C RFID tag.

This method facilitates reading data over the tag's block size limit by segmenting the read operation into multiple blocks. It is particularly useful for retrieving larger data sets stored across consecutive memory blocks on the tag.

Parameters

  • blockNumber: The starting block number from which the data reading begins. It should be within the RFID tag's valid data address range.
  • length: The total number of bytes to read. While the length may exceed the size of a single block, it must be a positive value and within the constraints of the tag's memory.

Returns

A Future<List<int>> that resolves to a concatenated list of integers representing the data read from the specified blocks.

Throws

  • Exception if the specified length is less than 0, indicating an invalid length.
  • RFIDException if there is an error in reading data from the blocks, which could be due to issues like communication errors with the RFID reader or attempting to read beyond the tag's memory limits.

Example

try {
  List<int> longData = await card.readLongData(blockNumber: 4, length: 16);
  print('Long data read from starting at block 4: $longData');
} catch (e) {
  print('Failed to read long data: $e');
}

Note: This method efficiently manages reading operations that span multiple blocks by automatically calculating the necessary reads based on the length and blockNumber parameters. Ensure that the starting block and the length parameters are chosen such that the read operation does not attempt to access beyond the tag's memory capacity.

Implementation

Future<List<int>> readLongData({
  required int blockNumber,
  int length = BLOCK_SIZE,
}) async {
  if (length < 0) {
    throw Exception('Invalid length');
  }

  _validateBlockNumber(
    blockNumber: blockNumber,
    length: length,
    start: DATA_ADDRESS_START,
    end: DATA_ADDRESS_END,
  );

  try {
    List<int> data = [];

    for (var i = 0; i < length; i += MAX_READ_LENGTH) {
      final currentBlock = blockNumber + (i / BLOCK_SIZE).floor();
      final remainingLength = length - data.length;

      data += await readData(
        blockNumber: currentBlock,
        length: remainingLength > MAX_READ_LENGTH
            ? MAX_READ_LENGTH
            : remainingLength,
      );
    }

    return data;
  } catch (e) {
    throw RFIDException('Error reading data: ${e.toString()}');
  }
}