Java – use scl010 to obtain the uid of Mifare Ultralight
I want to get the uid of Mifare ultralight NFC tag
TerminalFactory factory = TerminalFactory.getDefault(); List<CardTerminal> terminals = factory.terminals().list(); System.out.println("Terminals: " + terminals); CardTerminal terminal = terminals.get(0); Card card = terminal.connect("*"); System.out.println("card: " + card); CardChannel channel = card.getBasicChannel(); ResponseAPDU answer = channel.transmit(new CommandAPDU(0xFF,0xCA,0x00,0x00)); byte[] uid = answer.getBytes();
The problem is that I received two bytes instead of uid What's the problem? Is APDU correct?
Solution
The command you are actually using is not what you expected
The correct command APDU to use this reader to obtain uid / serial number / enumeration identifier is:
+------+------+------+------+------+ | CLA | INS | P1 | P2 | Le | +------+------+------+------+------+ | 0xFF | 0xCA | 0x00 | 0x00 | 0x00 | +------+------+------+------+------+
However, the constructor you use is defined as:
public CommandAPDU(int cla,int ins,int p1,int p2,int ne);
therefore
new CommandAPDU(0xFF,0x00)
You are creating a c-apdu with the following parameters: CLA = 0xff, INS = 0xca, P1 = 0x00, P2 = 0x00 So far, this is the same as the APDU above But the last parameter is ne = 0x00 NE = 0 indicates that the expected number of response bytes is zero (while Le = 0 indicates that the expected number of response bytes is (up to) 256)
This effectively creates the following case-1 apdus:
+------+------+------+------+ | CLA | INS | P1 | P2 | +------+------+------+------+ | 0xFF | 0xCA | 0x00 | 0x00 | +------+------+------+------+
Therefore, you will get up to 2 bytes of status word as a response (indicating successful use of 0x90 0x00 or indicating error, status code such as 0x6x 0xxx)
So you can use byte array to form your APDU:
new CommandAPDU(new byte[] { (byte)0xFF,(byte)0xCA,(byte)0x00,(byte)0x00 } )
Alternatively, you can specify an appropriate value for NE:
new CommandAPDU(0xFF,256)