Developing UIDReader

To create the UIDReader MIDlet:

  1. Create the UIDReader.java class file.

  2. Import the required classes.

    // Classes for contactless communication
    import javax.microedition.contactless.ContactlessException;
    import javax.microedition.contactless.DiscoveryManager;
    import javax.microedition.contactless.TargetListener;
    import javax.microedition.contactless.TargetProperties;
    import javax.microedition.contactless.TargetType;
    
    // Classes for UI
    import javax.microedition.lcdui.Alert;
    import javax.microedition.lcdui.AlertType;
    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.*;
  3. Set UIDReader to extend MIDlet and to implement TargetListener and CommandListener.

    public class UIDReader
            extends MIDlet
            implements TargetListener, CommandListener {
  4. Define the global variables used by the MIDlet, and implement the UIDReader method for launching the MIDlet. The UIDReader method sets up the MIDlet UI, and registers the MIDlet to receive notifications when NDEF targets are detected.

        // Global variables for the UI
        private Command exitCommand;
        private Form form;
    
        public UIDReader() {
    
            // Create the UI
            exitCommand = new Command("Exit", Command.EXIT, 1);
            form = new Form("UIDReader");
    
            form.addCommand(exitCommand);
            form.setCommandListener(this);
            form.append("Touch tag to read ID");
    
            // Determine the contactless target types supported by the device
            TargetType[] supportedTargetTypes = DiscoveryManager.getSupportedTargetTypes();
    
            // Check if the NDEF_TAG type is supported by the device,
            // and, if it is, register TargetListener for it
            for (int i=0; i<supportedTargetTypes.length; i++) {
                if (supportedTargetTypes[i].equals(TargetType.NDEF_TAG)) {
                    // Register TargetListener (this) for NDEF targets
                    try {
                        DiscoveryManager dm = DiscoveryManager.getInstance();
                        dm.addTargetListener(this, TargetType.NDEF_TAG);
                    } catch (SecurityException se) {
                        displayAlert("Unable to register TargetListener: " + se.toString(), AlertType.ERROR);
                    } catch (ContactlessException ce) {
                        displayAlert("Unable to register TargetListener: " + ce.toString(), AlertType.ERROR);
                    } catch (IllegalStateException ise) {
                        displayAlert("Unable to register TargetListener: " + ise.toString(), AlertType.ERROR);
                    } catch (NullPointerException npe) {
                        displayAlert("Unable to register TargetListener: " + npe.toString(), AlertType.ERROR);
                    }
                    // Break the loop, since a matching target type has been found
                    break;
                }
            }
    
        }
  5. Implement the MIDlet lifecycle requirements. The MIDlet uses the startApp method to display the UI.

        public void startApp() {
            Display.getDisplay(this).setCurrent(form);
        }
    
        public void pauseApp() {
        }
    
        public void destroyApp(boolean unconditional) {
        }
  6. Implement the targetDetected method mandated by the TargetListener class. The method is called every time the device detects one or more NDEF targets. The MIDlet uses the targetProperties array returned by the method to retrieve the UID of the first target found.

        /**
         * Implements the callback function of TargetListener
         * @param targetProperties: Array of targets found by the device
         */
        public void targetDetected(TargetProperties[] targetProperties) {
    
            // In case no targets are found, exit the method
            if (targetProperties.length == 0) {
                return;
            }
    
            // Read and display the UID of the first target found
            TargetProperties tmp = targetProperties[0];
            displayAlert("UID read: " + tmp.getUid(), AlertType.INFO);
        }
  7. Implement command and message display handling for the MIDlet UI. The MIDlet uses the commandAction method to handle the exit command, which involves unregistering TargetListener and closing the MIDlet. The MIDlet uses the displayAlert method to display error and other messages through the MIDlet UI.

        /**
         * Implements the callback function of CommandListener
         * @param command: Command given
         * @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);
        }
    }

You can now build and run the MIDlet.