Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
Expertise Level:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)

This page was last modified 16:59, 1 May 2008.

NFC UIDReader

From Forum Nokia Wiki

The code shows a basic MIDlet the reads the UID of an external Tag/Transponder and displays it on the screen. (Shown at NFC Forum Developer Training, WIMA 2008 and NFC-Congress Hagenberg 2008) 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("NFC-Research.at: UID Reader");
 
        form.addCommand(exitCommand);
        form.append("Touch Tag to read ID.");
        form.setCommandListener(this);
        
        // 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() {
        Display.getDisplay(this).setCurrent(form);
    }
 
    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) {
            return;
        }
 
        // 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);
            destroyApp(false);
            notifyDestroyed();
        }
 
    }
 
    private void displayAlert(String error, AlertType type) {
        Alert err = new Alert(form.getTitle(), error, null, type);
        Display.getDisplay(this).setCurrent(err, form);
    }
}
Related Discussions
Thread Thread Starter Forum Replies Last Post
6131NFC and Supermarket use funkyplates Near Field Communication 11 2008-01-13 19:45
NFC SDK integration with Eclipse and Ant hvuoltee Near Field Communication 0 2007-04-25 14:16
6131 NFC Browser bug? tboufflet Near Field Communication 4 2007-06-18 16:31
NFC 3220 Creating package not work metos_rs Mobile Java Tools & SDKs 3 2007-08-30 13:43
NDEF and Mifare cgauld Near Field Communication 1 2007-07-09 13:14
 
Powered by MediaWiki