×
Namespaces

Variants
Actions
Revision as of 07:04, 3 February 2012 by hamishwillee (Talk | contribs)

Detect secure element activity

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Compatibility
Platform(s):
Series 40
Article
Created: tsavinen (27 Jun 2007)
Last edited: hamishwillee (03 Feb 2012)
Simple MIDlet that notifies the user about secure element activity in card emulation mode.
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) {
}
}
}
36 page views in the last 30 days.