Sample code for 30+ languages & platforms
React Native

PC/SC Get Card UID

See more SCard Examples

Sends the APDU command to get a card's UID.

Chilkat React Native Downloads

React Native
import { BinData, SCard } from '@chilkat/react-native'

function chilkatExample() {
  let success = false;

  // This example requires the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  const scard = new SCard();

  // First establish a context to the PC/SC Resource Manager
  try {
    scard.establishContext('user');
  } catch {
    console.log(scard.lastErrorText);
    return;
  }

  // Use your own smart card reader name here.
  success = scard.connect('ACS ACR122 0', 'shared', 'no_preference');
  if (!success) {
    console.log(scard.lastErrorText);
    return;
  }

  console.log(`Connected reader: ${scard.connectedReader}`);
  console.log(`Active protocol: ${scard.activeProtocol}`);
  console.log(`ATR: ${scard.cardAtr}`);
  console.log(`Reader Status: ${scard.readerStatus}`);

  //  Send the APDU command 0xFF, 0xCA, 0x00, 0x00, 0x00
  const bdRecv = new BinData();
  try {
    scard.transmitHex(scard.activeProtocol, 'FFCA000000', bdRecv, 32);

    console.log(`Received: ${bdRecv.getEncoded('hex')}`);

    // The UID is the returned data without the final 2 bytes.
    const numBytes = bdRecv.numBytes;
    if (numBytes > 2) {
      console.log(`UID: ${bdRecv.getEncodedChunk(0, numBytes - 2, 'hex')}`);
    }
  } catch {
    console.log(scard.lastErrorText);
  }

  // Disconnect from this reader.
  try {
    scard.disconnect('leave');
  } catch {
    console.log(scard.lastErrorText);
  }

  // Applications should always release the context when finished.
  try {
    scard.releaseContext();
  } catch {
    console.log(scard.lastErrorText);
  }
}