Sample code for 30+ languages & platforms
Dart

PC/SC Get Card UID

See more SCard Examples

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

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  var success = false;

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

  final scard = CkSCard();

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

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

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

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

    print('Received: ${bdRecv.getEncoded('hex')}');

    // The UID is the returned data without the final 2 bytes.
    final numBytes = bdRecv.numBytes;
    if (numBytes > 2) {
      print('UID: ${bdRecv.getEncodedChunk(0, numBytes - 2, 'hex')}');
    }
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
  }

  // Disconnect from this reader.
  try {
    scard.disconnect('leave');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
  }

  // Applications should always release the context when finished.
  try {
    scard.releaseContext();
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
  }
}