The phone version of _main_.

Java for Small Spaces
Can Java phone home?
by Chris Carpenter
Listing 2. The phone version of _main_.


package midp.client;

import java.io.*;
import javax.microedition.rms.*;
import javax.microedition.io.*;
// Note that this import is manufacturer specific
import com.mot.cldc.io.j2me.datagram.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;

// A MIDLet is the phone equivalent to an Applet
public class MidpClient extends MIDlet implements CommandListener {
    protected DatagramConnection datagramConnection = null;
    protected byte[] fileName = "remote.txt".getBytes();


    // Form objects are MIDLet frames
    protected Form form = new Form("Inputs");
    protected TextField ipAddressTextField;
    protected TextField portNumberTextField;
    protected TextField fileTextField;

protected void clearDestinationInfo() {
    destAddress = "datagram://";
    destPort = 0;
}

// We must handle keypress actions from the user
// If the application is asked to terminate it calls destroyApp
public void commandAction(Command cmd, Displayable display) {
    if (cmd.getLabel().equals("Send"))
        send();
    else
        destroyApp(true);
}

// Here we place our input fields on our form
protected void createForm() {
    ipAddressTextField = new TextField("IP", "192.168.123.70", 15, TextField.ANY);
    form.append(ipAddressTextField);
    portNumberTextField = new TextField("Port", "1900", 5, TextField.NUMERIC);
    form.append(portNumberTextField);
    fileTextField = new TextField("File", "remote.txt", 15, TextField.ANY);

    // Here we assign what the keypresses will do
    form.append(fileTextField);
    Command sendCommand = new Command("Send", Command.SCREEN, 1);
    form.addCommand(sendCommand);
    Command exitCommand = new Command("Exit", Command.SCREEN, 1);
    form.addCommand(exitCommand);
    form.setCommandListener(this);
}
// This method stores the retrieved file in the phone's recordstore
// We are using Vector instead of ArrayList because Vector is the only
// supported collection
public void createRecordStore(Vector fileContents) {
    RecordStore recordStore = null;
    String recordStoreName = new String(fileName);
    int recordID = 0;

    // Delete the old database (if any)
    try {
        RecordStore.deleteRecordStore(recordStoreName);
    } catch (Exception e) {
    }
    try {
        recordStore = RecordStore.openRecordStore(recordStoreName, true);
    } catch (RecordStoreException rse) {
        System.err.println(rse.toString());
    }
    try {
        byte[] incomingBytes;
        Enumeration enumeration = fileContents.elements();
        while (enumeration.hasMoreElements()) {
            incomingBytes = ((String) enumeration.nextElement()).getBytes();
            recordStore.addRecord(incomingBytes, 0, incomingBytes.length);
        }
    } catch (Exception e) {
        System.err.println(e.toString());
    }
    try {
        recordStore.closeRecordStore();
    } catch (RecordStoreException rse) {
        System.err.println(rse.toString());
    }
}

public void destroyApp(boolean unconditional) {

}

// init is called before our application is displayed
protected void init() {
    destinationAddress = destinationAddress + ipAddressTextField.getString();
    destinationPort = Integer.parseInt(portNumberTextField.getString());
    fileName = fileTextField.getString().getBytes();
    try {
        datagramConnection =
            (DatagramConnection) Connector.open(destinationAddress + ":1901");
    } catch (Exception e) {
        System.out.println("Exception occured initializing Client: " + e.getMessage());
        throw new RuntimeException("Error initializing Client: " + e.getMessage());
    }

}

public void pauseApp() {
    System.out.println("in pauseApp");
}

public void send() {
    clearDestinationInfo();
    init();
    System.out.println("Beginning to create thread in client");
    DatagramListener listener = new DatagramListener(this, "datagram://:1902");
    Thread t = new Thread(listener);
    t.start();
    sendPacket();
}

protected void sendPacket() {
    try {
        Datagram dobject =
            datagramConnection.newDatagram(
                fileName,
                fileName.length,
                destinationAddress + ":" + destinationPort);
        datagramConnection.send(dobject);
        datagramConnection.close();
    } catch (Exception e) {
        System.out.println(
            "Exception occured in sendPacket(): " + e.getMessage() + " " + e.getClass());
    }
}

// Here we display our form
public void startApp() {
    Display currentDisplay = Display.getDisplay(this);
    MidpClient client = new MidpClient();
    client.createForm();
    currentDisplay.setCurrent(client.form);
}

    protected String destAddress = "datagram://";
protected int destPort;
}

About the Author

Chris Carpenter is a software engineer and architect at RoleModel Software, Inc. in Holly Springs, NC. He can be contacted at [email protected].