MIDP 2.0

Relation to MIDP 1.0

Networking extensions

Networking is still based on the Connector.open class. Other types of connection are added

Packaging

Push Registry

Push registry pseudocode

Turning pseudocode to real code

GUI

Multi-media

MIDP 2.0 has a number of classes for handling multi-media

Media support

The CLDC device can tell you what protocols and what media types it can handle



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

public class MediaInfo extends MIDlet {

    public MediaInfo() {
    }

    public void startApp() {
	String [] protocols = Manager.getSupportedProtocols(null);
	System.out.println("Supported protocols are:");
	for (int n = 0; n < protocols.length; n++) {
	    System.out.println("- " + protocols[n]);
	    String [] contentTypes = 
		Manager.getSupportedContentTypes(protocols[n]);
	    System.out.println("    Supported content types are:");
	    for (int m = 0; m < contentTypes.length; m++) {
		System.out.println("        " + contentTypes[m]);
	    }
	}
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
}


Single tones



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

public class Tone1 extends MIDlet {

    protected Display display;
    protected Form form;

    public Tone1() {
	display = Display.getDisplay(this);
	form = new Form("Info entry form");
    }

    public void startApp() {
	display.setCurrent(form);
	try {
	    Manager.playTone(ToneControl.C4, 200, 100);
	    // sleep about the same time as the
	    // tone plays for
	    Thread.sleep(2000);
	    // the next tone may be early, late
	    // or maybe on time
	    Manager.playTone(80, 2000, 100);
	} catch(MediaException e) {
	    System.out.println(e.toString());
	} catch(InterruptedException e) {
	}
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
	display = null;
	form = null;
    }
}


Tone sequences

Echo server

The following server uses ServerSocketConnection to listen to connections on port 8189. Each message that is received on this port is echo'ed back to the client


import javax.microedition.io.*;
import javax.microedition.midlet.*;
import java.io.*;

public class EchoServer extends MIDlet implements Runnable {
    
    public static int MYECHOPORT = 8189;

    public void run() {
	ServerSocketConnection s = null;
	try {
	    s = (ServerSocketConnection) Connector.open("socket://:" + 
							 MYECHOPORT);
	} catch(IOException e) {
	    System.out.println(e);
	    return;
	}

	while (true) {
	    try {
		// Wait for a connection.
		SocketConnection sc = (SocketConnection) s.acceptAndOpen();

		handleSocket(sc);
	    } catch(IOException e) {
		break;
	    }
	}  
    }

    public void startApp() {
	new Thread(this).start();
    }

    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }
    
    public static void handleSocket(SocketConnection incoming)
	throws IOException {
	System.out.println("Got connection");
	DataInputStream din = incoming.openDataInputStream();
	DataOutputStream dout = incoming.openDataOutputStream();
	
	boolean done = false;
	while ( ! done) {
	    String str = din.readUTF();
	    if (str == null) {
		done = true;
		System.out.println("Null received");
	    }
	    else {
		System.out.println("Echo: " + str);
		dout.writeUTF(str);
	    }
	    
	}
	din.close();
	dout.close();
	incoming.close();
    }
}

Echo client

This client connects to a server on port 8189 and sends two messages


import javax.microedition.io.*;
import javax.microedition.midlet.*;
import java.io.*;

public class EchoClient extends MIDlet implements Runnable {

    public static final int MYECHOPORT = 8189;

    public void run() {
	String address = "localhost";

	SocketConnection sock = null;
	DataInputStream din = null;
	DataOutputStream dout = null;
	System.out.println("Opening connection");
	try {
	    sock = (SocketConnection) Connector.open("socket://" +
						     address + ":" +
						     MYECHOPORT);
	    din = sock.openDataInputStream();
	    dout = sock.openDataOutputStream();
	} catch(IOException e) {
	    e.printStackTrace();
	    System.exit(3);
	}

	System.out.println("Opened connection");
	String response = null;
	try {
	    // Just send a few messages, for testing
	    dout.writeUTF("Hello");
	    response = din.readUTF();
	    System.out.println("Response: " + response);

	    dout.writeUTF("Bye");
	    response = din.readUTF();
	    System.out.println("Response: " + response);
	} catch(IOException e) {
	    e.printStackTrace();
	    return;
	}

	return;
    }

    public void startApp() {
	// run all the socket handling code 
	// in a separate thread
	new Thread(this).start();
    }

    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }
} // Client

Reading and writing on sockets

J2ME does not have the full range of IO classes that are in the J2SE. For example, it does not have BufferedReader and PrintStream that are useful for reading and writing lines of text. It only has InputStream, DataInputStream, OutputStream and DataOutputStream.

To read and write strings using In/OutputStream, you need to turn the strings into byte arrays


    InputStream in = ...;
    OutputStream out = ...;
    String msg = "hello";
    byte[] barray = msg.getBytes();
    out.write(barray);

    in.read(barray);
    msg = new String(barrray);

To read and write strings using DataIn/OutputStream, use the UTF methods


    DataInputStream din = ...;
    DataOutputStream dout = ...;

    String str = din.readUTF();
    dout.writeUTF(str);


Jan Newmarch (http://jan.newmarch.name)
jan@newmarch.name
Last modified: Wed Apr 28 20:09:28 EST 2004
Copyright ©Jan Newmarch
Copyright © Jan Newmarch, Monash University, 2007
Creative Commons License This work is licensed under a Creative Commons License
The moral right of Jan Newmarch to be identified as the author of this page has been asserted.