Detect secure element activity
Article Metadata
package test;
import javax.microedition.contactless.DiscoveryManager;
import javax.microedition.contactless.TransactionListener;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
/**
* Simple midlet that notifies the user about secure element activity in card
* emulation mode.
*/
public class ShowExternalSEAccess extends MIDlet implements CommandListener,
TransactionListener {
/**
* Form to show information about detected card
*/
Form form = new Form("ShowExternalSEAccess");
/**
* Exit command
*/
Command exitCmd = new Command("Exit", 1, Command.OK);
/**
* Constructor.
*/
public ShowExternalSEAccess() {
super();
}
/*
* (non-Javadoc)
*
* @see javax.microedition.midlet.MIDlet#startApp()
*
* Sets the form as current displayable and adds
* transaction listener.
*/
protected void startApp() throws MIDletStateChangeException {
// Set form as current displayable
form.addCommand(exitCmd);
form.setCommandListener(this);
Display.getDisplay(this).setCurrent(form);
// Add transaction listener
try {
DiscoveryManager.getInstance().addTransactionListener(this);
} catch (Exception ex) {
// Show any exceptions if adding the target listener fails.
form.append(ex.toString());
}
}
protected void pauseApp() {
}
/*
* (non-Javadoc)
*
* @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
*
* Remove the transaction listener when done.
*/
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
try {
DiscoveryManager.getInstance().removeTransactionListener(this);
} catch (Exception ex) {
// Ignore exceptions at this point
}
}
/*
* (non-Javadoc)
*
* @see javax.microedition.contactless.TransactionListener#externalReaderDetected(byte)
*
* This method is called after secure element activity in card emulation
* mode. This notification informs the application, that there might be
* changes in the data on the secure element.
*/
public void externalReaderDetected(byte slot) {
form.append("External reader detected, slot=" + slot
+ (slot == TransactionListener.UNKNOWN_SLOT ?
" (UNKNOWN SLOT)\n" : "\n"));
}
/*
* (non-Javadoc)
*
* @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command,
* javax.microedition.lcdui.Displayable)
*
* Handle exit command.
*/
public void commandAction(Command c, Displayable d) {
if (c == exitCmd) {
exit();
}
}
/**
* Exit the midlet.
*/
private void exit() {
try {
destroyApp(false);
notifyDestroyed();
} catch (MIDletStateChangeException ex) {
}
}
}


04 Oct
2009
This article shows how to detect secure element activity. This article represents a code snippet this is enough to understand the purpose of this article. Here the required classes are imported to the source code that’s the good thing and gives portability to the application. This code snippet is also helpful to starters because it also covers some basic APIs’ use also. The noticeable thing to this article is the use of the DiscoveryManager API. And its required methods that is necessary to accomplish this task. Overall this is a nice midlet.