Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

NFC UIDReader using Java ME

From Wiki
Jump to: navigation, search

This Java ME code example shows a basic MIDlet the reads the UID of an external Tag/Transponder and displays it on the screen. This was initially shown at NFC Forum Developer Training, WIMA 2008 and NFC-Congress Hagenberg 2008.

Article Metadata
Code Example
Source file:
Created: geri-m (01 May 2008)
Last edited: hamishwillee (23 Jul 2013)

Warning.pngWarning: This example was created for the NFC implementation provided on Series 40 devices, and may include use of features not yet available for NFC on Symbian devices.

Download appropriate NetBeans Project:

package at.nfcresearch.wima.examples;
// Packages for contactless Communcation
import javax.microedition.contactless.ContactlessException;
import javax.microedition.contactless.DiscoveryManager;
import javax.microedition.contactless.TargetProperties;
import javax.microedition.contactless.TargetListener;
import javax.microedition.contactless.TargetType;
// Packages for GUI Stuff
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.*;
public class UIDReader extends MIDlet implements TargetListener, CommandListener {
// Display Stuff
private Command exitCommand;
private Form form;
public UIDReader() {
// Create the GUI
exitCommand = new Command("Exit", Command.EXIT, 1);
form = new Form(" UID Reader");
form.append("Touch Tag to read ID.");
// Registration of the TargetListener for external contactless
// Targets (in this RFID_TAG).
try {
DiscoveryManager dm = DiscoveryManager.getInstance();
dm.addTargetListener(this, TargetType.NDEF_TAG);
} catch (ContactlessException ce) {
displayAlert("Unable to register TargetListener: " + ce.toString(), AlertType.ERROR);
public void startApp() {
public void pauseApp() {
public void destroyApp(boolean unconditional) {
* Implementation of the Call-Back Function of the TargetListener
* @param targetProperties: Array of Targets found by the Phone

public void targetDetected(TargetProperties[] targetProperties) {
// in case no targets found, exit the method
if (targetProperties.length == 0) {
// show the UID of the first tag found
TargetProperties tmp = targetProperties[0];
displayAlert("UID read: " + tmp.getUid(), AlertType.INFO);
* Implementation of the Call-Back function of the CommandListener
* @param command: command key pressed
* @param displayable: associated displayable Object

public void commandAction(Command command, Displayable displayable) {
if (command == exitCommand) {
DiscoveryManager dm = DiscoveryManager.getInstance();
dm.removeTargetListener(this, TargetType.NDEF_TAG);
private void displayAlert(String error, AlertType type) {
Alert err = new Alert(form.getTitle(), error, null, type);
Display.getDisplay(this).setCurrent(err, form);
This page was last modified on 23 July 2013, at 08:28.
40 page views in the last 30 days.