Posted By:
Vineet_Billorey
Posted On:
Friday, April 1, 2011 06:31 AM
Hi, my code runs fine to read a single card, but if I keep it in infinite loop, it runs hardly one or two cards infinitely. Thanx in advance. Code is as follows: public void readCard(JTextField tf) { String tagid="No Tag Id Detected"; for(;;) { try { TerminalFactory factory = TerminalFactory.getDefault(); List terminals = factory.terminals().list(); if(terminals.size()>0) { System.out.println("Terminals: " + terminals); //if (terminals.size() == 0) //{ System.out.println("No Terminals Found."); // return tagid; //} // Get the first terminal i
More>>
Hi,
my code runs fine to read a single card, but if I keep it in infinite loop, it runs hardly one or two cards infinitely. Thanx in advance.
Code is as follows:
public void readCard(JTextField tf)
{
String tagid="No Tag Id Detected";
for(;;)
{ try
{ TerminalFactory factory = TerminalFactory.getDefault();
List
terminals = factory.terminals().list();
if(terminals.size()>0)
{ System.out.println("Terminals: " + terminals);
//if (terminals.size() == 0)
//{ System.out.println("No Terminals Found.");
// return tagid;
//}
// Get the first terminal in the list
System.out.println("Connecting..");
CardTerminal terminal = terminals.get(0);
// Establish a connection with the card using
// "T=0", "T=1", "T=CL" or "*"
if(terminal.isCardPresent())
{ System.out.println("Card Detected");
Card card = terminal.connect("*");
System.out.println("Card: " + card);
// Get ATR
byte[] baATR = card.getATR().getBytes();
System.out.println("ATR: " + SmartCardIO.toString(baATR));
// Select Card Manager
// - Establish channel to exchange APDU
// - Send SELECT Command APDU
// - Show Response APDU
CardChannel channel = card.getBasicChannel();
// SELECT Command
// See GlobalPlatform Card Specification (e.g. 2.2, section 11.9)
// CLA: 00
// INS: A4
// P1: 04 i.e. b3 is set to 1, means select by name
// P2: 00 i.e. first or only occurence
// Lc: 08 i.e. length of AID see below
// Data: A0 00 00 00 03 00 00 00
// AID of the card manager,
// in the future should change to A0 00 00 01 51 00 00
//
byte[] baCommandAPDU = { (byte) 0x00, (byte) 0xA4, (byte) 0x04,(byte) 0x00, (byte) 0x08, (byte) 0xA0, (byte) 0x00,(byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x00,(byte) 0x00, (byte) 0x00 };
byte[] baCommandAPDU = {(byte) 0xFF,(byte) 0xCA,(byte) 0x00,(byte) 0x00,(byte) 0x00};
System.out.println("APDU >>>: "+SmartCardIO.toString(baCommandAPDU));
ResponseAPDU r = channel.transmit(new CommandAPDU(baCommandAPDU));
tagid=SmartCardIO.byteArrayToHexString(r.getBytes()).trim();
System.out.println("APDU
<
<
<: "+tagid);
tf.setText(tagid);
//try
//{ Thread.sleep(1000);
//}catch(InterruptedException ie){}
// Disconnect
// true: reset the card after disconnecting card.
card.disconnect(true);
}
else
{
System.out.println("No Card Detected");
}
}
}catch (Exception ex)
{ //ex.printStackTrace();
System.out.println("Error.:"+ex.toString());
}
}
}
<<Less