getUID method
Retrieves the Unique Identifier (UID) of a Mifare Ultralight C RFID tag.
This method reads the UID from the specified blocks of the RFID tag, verifying the manufacturer ID and checksums to ensure the UID's authenticity. The UID is a crucial component for identifying the tag and ensuring secure operations.
Returns
A Future<List<int>>
that resolves to the UID of the tag as a list of integers.
Throws
RFIDException
if there is an error reading the UID from the tag, including issues such as communication errors with the RFID reader or if the UID data fails validation checks (e.g., incorrect manufacturer ID or checksum validation failure).
Example
try {
List<int> uid = await card.getUID();
print('Tag UID: $uid');
} catch (e) {
print('Failed to get UID: $e');
}
Note: The UID is derived from specific blocks that include the manufacturer ID and checksum bytes for validation. This method ensures the integrity and authenticity of the UID by checking these values against expected standards.
Implementation
Future<List<int>> getUID() async {
final data = await reader
.readBlock(blockNumber: SERIAL_NUMBER_ADDRESS_START, length: 9)
.catchError((dynamic e) {
throw RFIDException('Error getting UID: ${e.toString()}');
});
final manufacturerId = data[0];
final bcc0 = data[3];
final bcc1 = data[8];
final uid = data.sublist(0, 3) + data.sublist(4, 8);
if (manufacturerId != CardManufacturer.NXPSemiconductors.id) {
throw RFIDException('Invalid manufacturer ID');
}
if (bcc0 != 0x88 ^ uid[0] ^ uid[1] ^ uid[2]) {
throw RFIDException('Invalid checksum');
}
if (bcc1 != uid[3] ^ uid[4] ^ uid[5] ^ uid[6]) {
throw RFIDException('Invalid checksum');
}
return uid;
}