Detect external ISO14443 card
Article Metadata
Compatibility
Platform(s): Series 40
Article
Created: tsavinen
(27 Jun 2007)
Last edited: hamishwillee
(25 Mar 2013)
Simple MIDlet that detects external ISO14443 card and shows its UID and the URL that can be used to open a connection to it.
package test;
import javax.microedition.contactless.DiscoveryManager;
import javax.microedition.contactless.TargetListener;
import javax.microedition.contactless.TargetProperties;
import javax.microedition.contactless.TargetType;
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 detects external ISO14443 card and shows its uid and the
* url that can be used to open a connection to it.
*/
public class ShowISO14443Connection extends MIDlet implements TargetListener,
CommandListener {
/**
* Form to show information about detected card
*/
Form form = new Form("ShowISO1443Connection");
/**
* Exit command
*/
Command exitCmd = new Command("Exit", 1, Command.OK);
/**
* Constructor.
*/
public ShowISO14443Connection() {
super();
}
/*
* (non-Javadoc)
*
* @see javax.microedition.midlet.MIDlet#startApp()
*
* Sets the form as current displayable and adds target listener for
* ISO14443.
*/
protected void startApp() throws MIDletStateChangeException {
// Set form as current displayable
form.addCommand(exitCmd);
form.setCommandListener(this);
Display.getDisplay(this).setCurrent(form);
// Add target listener for ISO14443 cards
try {
DiscoveryManager.getInstance().addTargetListener(this,
TargetType.ISO14443_CARD);
} 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 target listener when done.
*/
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
try {
DiscoveryManager.getInstance().removeTargetListener(this,
TargetType.ISO14443_CARD);
} catch (Exception ex) {
// Ignore exceptions at this point
}
}
/*
* (non-Javadoc)
*
* @see javax.microedition.contactless.TargetListener#targetDetected(javax.microedition.contactless.TargetProperties[])
*
* Display uid and url of the detected (ISO14443) card.
*/
public void targetDetected(TargetProperties[] targetProperties) {
for (int i = 0; i < targetProperties.length; i++) {
TargetProperties tp = targetProperties[i];
// Show the uid
form.append("uid: " + tp.getUid() + "\n");
// Make sure that detected card is ISO14443.
// This check is only needed if we are listening for other targets
// as well.
if (tp.hasTargetType(TargetType.ISO14443_CARD)) {
form.append("Target is ISO14443_CARD\n");
// Show the url to use to open the connection to the ISO14443
// card
try {
form.append("url: " + tp.getUrl(Class.forName(
"javax.microedition.contactless.sc.ISO14443Connection"))
+ "\n");
} catch (Exception ex) {
form.append(ex.toString());
}
} else {
// Never happens in this example
form.append("Target is NOT ISO14443_CARD\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) {
}
}
}


Praburaju - Nokia 701 NFC Phone
This code is not detecting ISO1443 Tag on Nokia 701. I have updated the the above code to check the supported tags and ISO14443 is supported by the phone but whenever I show ISO14443 JCOP javacard , I am geting message "Tag is empty". This message is a default phone message wich is displayed on showing the ISO 14443 Card. It looks like targetDetected code is not executed on detecting ISO14443 card.
Please helppraburaju 19:07, 29 October 2011 (EEST)
Hamishwillee - Author may not be tracking this article
Hi
This article was written in 2007, so there is a good chance that the author is not still watching it. I suggest you try on the Discussion boards, and possibly sending them a private message. Please update this article if you find a solution.
Regards
Hamishhamishwillee 05:54, 3 November 2011 (EET)