Java 2 Micro Edition and the Mobile Information Device Profile

Wednesday Nov 15th 2000 by David Fox

Write wireless networking programs using MIDP and then run them on pretty much every cell phone or handheld device.

Who wouldn't love to be able to write cool wireless networking programs and then have them work on pretty much every cell phone or handheld device? Java 2 Micro Edition (J2ME) is touted as the way to achieve this — write once and run anywhere, easy as pie. But how useful is J2ME, really? Currently the reference implementation for J2ME is the Palm Pilot. You can read an intro article about Java Palm Pilot programming here (Java 2 Micro Edition and the Kilobyte Virtual Machine), or read about how to develop a simple J2ME Palm application here (Creating Palm Pilot Software Using J2ME). But the exciting part of J2ME are the companies that have banded together to create the MIDP — the Mobile Information Device Profile — an implementation of user interface and networking optimized for small devices such as cell phones. Companies that have worked on the MIDP include Ericsson, NEC, Nokia, NTT DoCoMo, Palm Computing, Research In Motion (RIM), DoCoMo, LG TeleCom, Samsung, and more. Taking the lead in defining the MIDP is Motorola.

Motorola's has recently released an optimized Software Development Kit (SDK) allowing programmers to create "MIDlets" that can be dropped into any J2ME-supporting Motorola device. Every cell phone rolled out by Motorola by the end of next year, such as Motorola's Integrated Digital Enhanced Network (iDen) line of devices, is slated to support J2ME. Since the Motorola SDK sticks to the MIDP and J2ME standard, it should theoretically be a trivial matter to use the same code for any Java-supporting handheld gadget, pager, cell phone, Web browser, or other device.

Motorola's J2ME SDK is only available for Windows 98 or NT machines. Eventually, this SDK will be available commercially as part of Metrowerk's Code Warrior development environment. The Motorola developer's site has everything you need to get started. Register at the site, then log in using your username and password. Choose the Tools & Resources area from the top navigation bar. Download the J2ME SDK. The ZIP file should contain a file called MOTOROLA_SDK.EXE. Run it. Note that the installer may be password protected — be sure to read the installation notes in order to get the proper password. You may need to e-mail technical support at for access.

You'll also need to be sure that the Java Developer's Kit 1.2.2 is installed. To test that everything is install properly, go to your DOS command line and type:

java -version

The result should be "Java version 1.2.2" or higher. If so, you're ready to roll. If not, you may need to adjust your PATH so that JDK1.2.2 is the earliest directory in the list.

The heart of every MIDlet is the startApp() method. It kicks the MIDlet into action and should set up all the components or other objects you are interested in. Every MIDlet must also contain a pauseApp() and destroyApp() methods, which are called if and when a user chooses to either pause or quit your program.

Graphically speaking, the actual screen on your cell phone or other devices is a Display object. Every MIDlet has one and only one Display object, which you can access using getDisplay(this) . The most common object to add to the Display is a Form which can contain common input elements such as strings, text fields, radio buttons, checkboxes, and interactive gauges.

So the quick and dirty way to create a Hello World MIDlet would create a form and add it to the display using Display's setCurrent() method:

package com.mot.j2me.midlets.hello;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class HelloWorld extends MIDlet {

     private Form aForm;
     private Display theDisplay;

        theDisplay = Display.getDisplay(this);
        aForm = new Form("Hello World");
     StringItem str = new StringItem("Hello World","And Welcome!");

     protected void startApp() throws MIDletStateChangeException 

     protected void pauseApp() { }
     protected void destroyApp(boolean unconditional)
            throws MIDletStateChangeException {

To program such a MIDlet you could create a directory called hello beneath the MotoSDK\demo\midlets\com\mot\j2me\midlets directory. You would then create a text file called in this directory. The reason we're using such as long package/directory name is to take advantage of some nice batch files that Motorola has been kind enough to write for us.

For example, you can compile and preverify the HelloWorld MIDlet by switching to the MotoSDK\demo\midlets directory and typing:

compileAll com\mot\j2me\midlets\hello\*.java

If all goes well, you'll wind up with a fresh, happy HelloWorld.class file in the MotoSDK\demo\midlets\com\mot\j2me\midlets directory. To test the application out, Motorola has been kind enough to include an emulator. Assuming you installed everything in the \MotoSDK directory, move to the \MotoSDK\scripts directory and type:

runMotoiDEN com.mot.j2me.midlets.hello.HelloWorld

Figure 1.

You'll see your MIDlet, as in Figure 1.

To hit the 0 through 9, #, *, left, right, up and down keys you can use your keypad or simply click on the desired key with your mouse. Since HelloWorld doesn't do anything with user input, hitting these keys will have no effect.

If you happened to own an iDen phone, you could plug it into your PC and use the phone's linking software to install the MIDlet directly into the phone's memory, similarly to the way you might install an application on the Palm Pilot. In the very near future, Motorola and other cell phone manufacturers may make it easy for users to download applets right to their phones, on demand.

Now let's blaze ahead a bit and talk about the MIDP specification. A Display can consist of either a Screen object or a Canvas. Every Screen has an optional title (usually at the top) and Ticker (usually running along the bottom). Specific types of Screens include:

  • List — A row of elements that a user may choose from, perfect for a menu.
  • Alert — A "dialog box" that appears and shows some text and/or an Image to the user and then, after a set amount of time, disappears. This is perfect for important messages.
  • Form — A screen for user input which may contain all or some of the following MIDP objects: ImageItem, StringItem, TextField, DateField, Gauge, or ChoiceGroup.
  • TextBox — A box that allows the user to type in or edit some text.

Using the ease and convenience of Screens, creating quick MIDlets is a snap. If you want to customize a component, capture user input, or create more detailed graphics, however, you should use a Canvas object. A Canvas must define the paint() method. The Graphics object allows for simple renderings using familiar Java methods such as drawArc(), drawImage(), drawLine(), drawRect(), drawString(), fillRect(), etc.

You should always create your own Canvas subclass. For example:

class SillyCanvas extends Canvas 
     public void paint(Graphics g) 
        g.drawString("Hello World", getWidth() / 2, 0,                           
            Graphics.HCENTER | Graphics.TOP);

This Canvas contains a rectangle bordering the entire screen with the string "Hello World" at the top, horizontal center. You could drop this canvas into the Display by creating it in your MIDlet:

private SillyCanvas aCanvas = new SillyCanvas();

And then, in the MIDlet's startApp() method or anywhere else that made sense, you could set the current Display to show the special canvas:


Note that a cell phone's Display can only show one Screen or Canvas at a time. A typical application starts with a List as a main menu of choices, branching into other types of Screens depending on the user's selection. An application such as a game is more likely to paint the game screen using a Canvas, redrawing the screen as often as possible to achieve animation.

Of course the real fun of having an application on a cell phone is networking. The MIDP specification, especially Motorola's implementation, makes it extremely easy to pass data back and forth using Datagrams. Every iDen phone has its own static IP address, so having one phone communicate with another is only a matter of simple peer-to-peer networking.

In addition, the phone can connect to any outside server machine. This server can be used as a gateway for pretty much any type of network communication imaginable. For instance, a gateway can be set up to access an entire database of sports scores, and then stream only the latest requested scores to an MIDlet.

To connect to a server, simply have the MIDlet use the Connector class:

Datagram dgram = dc.newDatagram(message, msglength,

The remote server could, of course, be another iDen phone. Just create an endless loop that listens to a port and waits for some data:

DatagramConnection dc = ( DatagramConnection)
while (true) 
   dgram = dc.newDatagram(dc.getMaximumLength());
   reply = new String(dgram.getData(), 0,dgram.getLength());

Since the Datagram protocol is standard, one could write a dastardly-simple server component in Java Standard Edition (or any other language), running on any PC. For instance:

DatagramSocket receiveSocket = null; 
DatagramPacket receivePacket = new DatagramPacket(bytesReceived, bytesReceived.length);

A simple game can be written with relative ease. For instance,

is a MIDlet that allows a user to pick a number between 0 and 9, as shown in Figure 2. It then sends this number to a server (at a fictitious machine with the host name of Note that for the sake of testing, it's a good idea to set the server to 'localhost' — that way you can run the server and the client from the comfort of your desktop machine.

Figure 2.

Running on the machine is

, which simply sits there and waits for a message on port 9000. If the number is 4, then the server sends back a simple one-byte "Y" message. Otherwise it sends back an "N". Maybe not as compelling a game as Unreal or Quake III, but it's a start.

Scan the MotoSDK\docs\api directory for the complete CLDC and MIDP API. And for additional sample code, check out the applications in the MotoSDK\demo\midlets\com\mot\j2me\midlets directory.

About the Author

David Fox is vice president of Games at PlayLink, Inc. He's also the author of numerous books and articles about cyberculture and technology.

Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved