node-pcsclite
node-pcsclite copied to clipboard
Get nfc tag uid
How can i get uid (unique ID) of NFC tag? Help me please!
Hi @quocnguyen123,
in order to get UID of NFC tag, you need to send an appropriate APDU command (Get Data
in this case, btw you can find some basic commands here) using reader.transmit
method and then parse the response.
You can do it like this:
// ... connection to the reader and detecting card code as shown in README
// APDU CMD: Get Data
const packet = new Buffer([
0xff, // Class
0xca, // INS
0x00, // P1: Get current card UID
0x00, // P2
0x00 // Le: Full Length of UID
]);
reader.transmit(packet, 12, (err, response) => {
if (err) {
console.log(err);
return;
}
if (response.length < 2) {
console.log(`Invalid response length ${response.length}. Expected minimal length was 2 bytes.`);
return;
}
// last 2 bytes are the status code
const statusCode = response.slice(-2).readUInt16BE(0);
// an error occurred
if (statusCode !== 0x9000) {
console.log('Could not get card UID.');
return;
}
// strip out the status code (the rest is UID)
const uid = response.slice(0, -2).toString('hex');
// const uidReverse = reverseBuffer(response.slice(0, -2)).toString('hex'); // reverseBuffer needs to be implemented
console.log('card uid is', uid);
});
If you don't want to deal with these underlying things, take a look at nfc-pcsc library. It offers easy to use high level API for detecting, reading and writing NFC tags and cards, also supports Promises and async/await. You just need a few lines to read card UID.
Hope it helps.
I solved this issue with your direction. Thank you very much In line: reader.transmit(packet, 12, (err, response) I think we should pass protocol as third argument.
You should set the protocol, if needed, during connect. https://github.com/martinpaljak/esteid.js/blob/master/node-pcsc.js#L82