Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
This page was last modified 18:14, 20 May 2008.

Detect external ISO14443 card

From Forum Nokia Wiki

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) {
        }
    }
}
Related Discussions
Thread Thread Starter Forum Replies Last Post
Failure in fileconnection thngkl Mobile Java General 4 2007-02-02 19:06
Changing the ordinal position of an exteranl application. Shaikuny Symbian User Interface 1 2004-12-30 08:32
Pushregistry / NFC fibbe Mobile Java Networking & Messaging & Security 12 2007-03-06 10:04
detect Dtmf mohammadyou Mobile Java General 1 2007-09-18 00:58
Help Me mohammadyou Mobile Java General 3 2007-09-16 10:15
 
Powered by MediaWiki
RDF Facets: qfnZtopicQUqfnTopicZjavaQ qfnZtypeQUqfnTypeZCommunityContentQ qfnZtypeQUqfnTypeZWebpageQ qfnZtypeQUqfnTypeZWikiContentQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX