Coder Social home page Coder Social logo

Get nfc tag uid about node-pcsclite HOT 3 OPEN

santigimeno avatar santigimeno commented on July 26, 2024
Get nfc tag uid

from node-pcsclite.

Comments (3)

pokusew avatar pokusew commented on July 26, 2024 1

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.

from node-pcsclite.

quocnguyen123 avatar quocnguyen123 commented on July 26, 2024

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.

from node-pcsclite.

martinpaljak avatar martinpaljak commented on July 26, 2024

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

from node-pcsclite.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.