Exercise 2 Solution: Simple Sun SPOT Application Code

Code:
 1 /*
 2  * TestApplication.java
 3  *
 4  * Created on Apr 16, 2008 1:00:50 PM;
 5  */
 6 
 7 package org.sunspotworld;
 8 
 9 import com.sun.spot.peripheral.Spot;
10 import com.sun.spot.sensorboard.EDemoBoard;
11 import com.sun.spot.sensorboard.peripheral.ISwitch;
12 import com.sun.spot.sensorboard.peripheral.ITriColorLED;
13 import com.sun.spot.peripheral.radio.IRadioPolicyManager;
14 import com.sun.spot.io.j2me.radiostream.*;
15 import com.sun.spot.io.j2me.radiogram.*;
16 import com.sun.spot.sensorboard.io.IScalarInput;
17 import com.sun.spot.util.*;
18 
19 import java.io.*;
20 import javax.microedition.io.*;
21 import javax.microedition.midlet.MIDlet;
22 import javax.microedition.midlet.MIDletStateChangeException;
23 
24 /**
25  * The startApp method of this class is called by the VM to start the
26  * application.
27  * 
28  * The manifest specifies this class as MIDlet-1, which means it will
29  * be selected for execution.
30  */
31 public class TestApplication extends MIDlet {
32 
33     private ITriColorLED [] leds = EDemoBoard.getInstance().getLEDs();
34 
35     protected void startApp() throws MIDletStateChangeException {
36         System.out.println("Hello, world");
37         new BootloaderListener().start();   // monitor the USB (if connected) and recognize commands from host
38 
39         long ourAddr = Spot.getInstance().getRadioPolicyManager().getIEEEAddress();
40         System.out.println("Our radio address = " + IEEEAddress.toDottedHex(ourAddr));
41 
42         IScalarInput sensor = EDemoBoard.getInstance().getScalarInputs()[EDemoBoard.A0];
43         while(true){
44             try {
45                 int val = sensor.getValue();
46                 System.out.println("Value: " + val);
47             } catch (IOException ex) {
48                 ex.printStackTrace();
49             }
50             Utils.sleep(100);
51         }
52     }
53 
54     protected void pauseApp() {
55         // This is not currently called by the Squawk VM
56     }
57 
58     /**
59      * Called if the MIDlet is terminated by the system.
60      * I.e. if startApp throws any exception other than MIDletStateChangeException,
61      * if the isolate running the MIDlet is killed with Isolate.exit(), or
62      * if VM.stopVM() is called.
63      * 
64      * It is not called if MIDlet.notifyDestroyed() was called.
65      *
66      * @param unconditional If true when this method is called, the MIDlet must
67      *    cleanup and release all resources. If false the MIDlet may throw
68      *    MIDletStateChangeException  to indicate it does not want to be destroyed
69      *    at this time.
70      */
71     protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
72         for (int i = 0; i < 8; i++) {
73             leds[i].setOff();
74         }
75     }
76 }

Go on to Exercise 3