Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get nfc tag uid #76

Open
quocnguyen123 opened this issue Jul 31, 2017 · 3 comments
Open

Get nfc tag uid #76

quocnguyen123 opened this issue Jul 31, 2017 · 3 comments

Comments

@quocnguyen123
Copy link

How can i get uid (unique ID) of NFC tag? Help me please!

@pokusew
Copy link

pokusew commented Jul 31, 2017

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.

@quocnguyen123
Copy link
Author

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.

@martinpaljak
Copy link

You should set the protocol, if needed, during connect. https://github.com/martinpaljak/esteid.js/blob/master/node-pcsc.js#L82

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants