Zip
Zip

Reputation: 21

Read NFC Mifare Ultralight cards with javax.smartcardio

I try to read NFC Mifare Ultralight cards (page 4) with an ACR1252U and the javax.smartcardio Java library this way:

TerminalFactory factory = TerminalFactory.getDefault();
List<CardTerminal> terminals = factory.terminals().list();
System.out.println("Terminals: " + terminals);

CardTerminal terminal = terminals.get(0);

System.out.println("Waiting for a card..");

if (terminal == null)
    return;
terminal.waitForCardPresent(0);

Card card = terminal.connect("T=1");
System.out.println("Card: " + card);
System.out.println("ATR: " + bytesToHex(card.getATR().getBytes()));
System.out.println("Protocol: " + card.getProtocol());

CardChannel channel = card.getBasicChannel();

CommandAPDU command = new CommandAPDU(new byte[]{(byte) 0xFF, 
    (byte) 0xB0, (byte) 0x00, (byte) 0x04, (byte) 0x04});
ResponseAPDU response = channel.transmit(command);

if (response.getSW1() == 0x90) {
    // success command
    byte[] data = response.getData();
    System.out.println(new String(data));
}

Sometimes it works and sometimes not (with the same card)

When the reading works, I get these values:

And when it doesn't work, I get these:

Any idea what I'm doing wrong?

Upvotes: 2

Views: 1725

Answers (1)

Michael Roland
Michael Roland

Reputation: 40849

The ATR 3B80800101 in the second case (where it did not work) indicates that the reader did not detect (or did not properly detect) the card. The ACR1252U seems to emulates this ATR only to allow connecting through the PC/SC API (e.g. javax.smartcardio) even if there is no actualy card present. This would not be possible if the reader explicitly indicated card-no-present.

The more reliably way to check if the reader detected your card (and identified it as MIFARE Ultralight) would be to parse the ATR according to the PC/SC specification (see the section on ATR for contactless storage cards):

3B 8F 80 01 80 4F 0C A000000306 03 0003 00000000 68
                     |          |  |    |
                     |          |  |    \--> RR = reserved for future use
                     |          |  \-------> NN = MIFARE Ultralight
                     |          \----------> SS = ISO 14443 Type A part 3
                     \---------------------> PC/SC RID

Upvotes: 2

Related Questions