J2ME & Embedded Systems
by William Wright


Example 1: 

public class Hello {
    public Hello() {
        System.out.println("Hello, World!");
    }
    public static void main(String [] argv) {
        new Hello();
    }
}

Example 2:

StreamConnectionNotifier  ss = (StreamConnectionNotifier)
Connector.open("serversocket://:80", Connector.READ_WRITE);
StreamConnection conn = ss.acceptAndOpen();


Example 3:

HTTP/1.0 200 Document follows <CRLF>
Content-Type: text/html <CRLF>
Connection: close <CRLF>
<CRLF>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML><HEAD><TITLE>Security Event Log</TITLE><HEAD>


Listing One
/* Initialize the GPIO pins and register listeners */
private void initializeGpio() {
    int [] pins = {
        GpioPin.GPIOE_BIT0, GpioPin.GPIOE_BIT1,
        GpioPin.GPIOE_BIT2, GpioPin.GPIOE_BIT3,
        GpioPin.GPIOE_BIT4, GpioPin.GPIOE_BIT5,
        GpioPin.GPIOE_BIT6, GpioPin.GPIOE_BIT7};
        
        for (int i=0; i<pins.length; i++) {
            PushButton inPin = new PushButton(pins[i]);
            inPin.setTriggerReportPolicy(PushButton.TRIGGER_ON_PUSH);
            inPin.addTriggerListener(new EventListener(i));
        }
}
/* This inner class is a listener for events from a PushButton object */
private class EventListener implements TriggerEventListener {
    int detectorNumber;
    public EventListener(int detectorNumber) {
        this.detectorNumber = detectorNumber;
    }
  public void triggerEvent() {
        if (events.size() == MAX_EVENTS)
            events.removeElementAt(0);
        MotionEvent me = new MotionEvent(detectorNumber, 
                                         System.currentTimeMillis());
        events.addElement(me);
  }
}


Listing Two
StreamConnectionNotifier scn = null;
initializeGpio();
try {
  scn = (StreamConnectionNotifier)Connector.open(
  "serversocket://:" + myListenPort,
  Connector.READ_WRITE);
} catch (IOException ioe) {
  System.out.println(
  "Can't open server socket: "+ioe.getMessage());
}
while (true) {
  try {
    StreamConnection sc = scn.acceptAndOpen();
    InputStream is = sc.openInputStream();
    OutputStream os = sc.openOutputStream();

    eatInputStream(is);
    StringBuffer buf = new StringBuffer(header);
    buf.append("<BODY><TABLE border=\"1\"><TR><TD><B>Sensor 
                                     Number</B><TD><B>How Long Ago</B>");
    long now = System.currentTimeMillis();
    for (int i=events.size()-1; i>=0; i--) {
      MotionEvent event = (MotionEvent)events.elementAt(i);
      buf.append("<TR><TD align=center>");
      buf.append(String.valueOf(event.detectorNumber));
      buf.append("<TD>");
      buf.append(event.getTime(now));
    }
    buf.append("</TABLE></BODY></HTML>");
    buf.append("\r\n\r\n");
    os.write(buf.toString().getBytes());
    os.close();
    sc.close();
  } catch (IOException ioe) {
    System.err.println("IOException: "+ioe.getMessage());
  }
}






2


