I'm currently working on Java Web Applet that uses the OpenCard
Framework to access smartcard via a card reader. I am using the
asynchronous approach as suggested. Thus far all is well in that I can
send and retrieve APDUs within the cardInserted function, however
there are two points that I am having difficulties with and you guys
can shed some light.
1) I'm using CTListener so I would assume that when I remove the
smartcard the cardRemoved function should be triggered right? In my
case it doesn't.
2) This may arise due to the above point, I am now unable to read a
second card. No action seems to occur.
Below is an outline of the code:
import java.applet.Applet;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import opencard.core.OpenCardException;
import opencard.core.event.CTListener;
import opencard.core.event.CardTerminalEvent;
import opencard.core.event.EventGenerator;
import opencard.core.service.CardRequest;
import opencard.core.service.CardServiceException;
import opencard.core.service.SmartCard;
import opencard.core.terminal.CardTerminal;
import opencard.core.terminal.CardTerminalException;
import opencard.core.terminal.CommandAPDU;
import opencard.core.terminal.ResponseAPDU;
import opencard.opt.util.PassThruCardService;
public class AppMain extends Applet {
private final static Logger LOG = Logger.getAnonymousLogger();
private SmartCard card;
private CardTerminal terminal;
private int slotID;
@Override
public void init() {
try {
SmartCard.start();
CardListener listener = new CardListener();
EventGenerator.getGenerator().addCTListener(listener);
EventGenerator.getGenerator().createEventsForPresentCards(listener);
} catch (OpenCardException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
@Override
public void start() {
}
@Override
public void stop() {
try {
SmartCard.shutdown();
} catch (CardTerminalException e) {
e.printStackTrace();
}
}
private class CardListener implements CTListener {
public void cardInserted(CardTerminalEvent event) throws
CardTerminalException {
try {
card = SmartCard.getSmartCard(event, new
CardRequest(CardRequest.ANYCARD, null, null));
terminal = event.getCardTerminal();
slotID = event.getSlotID();
PassThruCardService myCardService = (PassThruCardService)
card.getCardService(PassThruCardService.class, true);
CommandAPDU commandAPDU = new CommandAPDU(raw);
responseAPDU = myCardService.sendCommandAPDU(commandAPDU);
commandAPDU = new CommandAPDU(new byte[] { (byte) 0xFF, (byte) 0xC0,
0x00, 0x00, responseAPDU.sw2() });
responseAPDU = myCardService.sendCommandAPDU(commandAPDU);
} catch (CardTerminalException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void cardRemoved(CardTerminalEvent event) throws
CardTerminalException {
if ((event.getSlotID() == slotID) &&
(event.getCardTerminal().equals(terminal))) {
card = null;
terminal = null;
slotID = 0;
}
}
}
}
Some month ago i had to struggle with the same problem. Here's my
approach which worked fine:
1) public class AppMain extends Applet implements CTListener {...}
I did not use a inner class. There was no obvious reason to do so.
2) It looks strange, but i set the CTListener before starting the
OCF, but it works. Then i set a suitable polling interval. Cannot
remember the default value but it may bee too long.
// seems to work although OCF has not been started yet!
EventGenerator eventGenerator = EventGenerator.getGenerator();
eventGenerator.addCTListener(this);
// set a suitable polling interval
eventGenerator.setPollInterval(s_iPollingInterval);
SmartCard.start(); // start Open Card Framework
System.out.println("OpenCard Framework initialized...");
...keep on
Hartmut
> freerad...@gmail.com wrote:
>
>> Hi,
...look in your mailbox, sent you a complete example
of my approach. It is still in use and works fine.