Skip to main content

Let your Java app "feel the rain" on its skin

Posted by asj2006 on April 24, 2007 at 1:03 AM PDT

Your journey begins with the task of gently introducing your Java applications to the realities of the physical world, an area that normally exists beyond the confines of the limited and rather myopic worldview of the typical Java midlet.

In this paradigm that is exemplified by the new Sun SPOTs, a midlet without a way to "feel the rain" on its skin, is like a gourmet diner who is finally allowed access to a connoiseur's nirvana, only to find out that he has a cold and cannot taste any of the offerings placed before him.

As one singer put it in more elegant fashion:

Feel the rain on your skin

No one else can feel it for you

Only you can let it in

No one else, no one else

Can speak the words on your lips

Drench yourself in words unspoken

Live your life with arms wide open

Today is where your book begins

The rest is still unwritten

- Natasha Bedingfield

You will help write this book, and your Java applications will finally be able to "feel" things that they could not have done otherwise.

As a first step, we'll allow our new midlet to sense temperature and light.

The first thing you need to do is to create a Midlet skeleton from the demo applications that are included in the Sun SPOT kit. It is ok to start from scratch if you want, but why struggle when someone else has already made the path for you easier? I used the BuiltInSensorsDemo demo application and closely studied the source of the BuiltInSensorsDemo class. A quick look at the short source code will immediately assuage any fears by mobile developers that the Midlets here are vastly different than those in mobile devices.

For example, the same life cycle templates are in there:



    protected void startApp() throws MIDletStateChangeException { 
        try {
            run();
        } catch (IOException ex) { //A problem in reading the sensors.
            ex.printStackTrace();
        }
    }


    protected void pauseApp() {
    }


    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
    }

The one big difference of course if that this Midlet uses some quite unusual libraries.



import com.sun.spot.sensorboard.EDemoBoard;
import com.sun.spot.sensorboard.io.IScalarInput;
import com.sun.spot.sensorboard.io.ITemperatureInput;
import com.sun.spot.sensorboard.peripheral.ITriColorLED;
import com.sun.spot.sensorboard.peripheral.ISwitch;
import com.sun.spot.sensorboard.peripheral.ILightSensor;
import com.sun.spot.sensorboard.peripheral.LEDColor;
import com.sun.spot.util.Utils;
import java.io.IOException;

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

The easiest way to get started is to simply copy that demo application folder, modify the contents and rename the folder, then open the lot using Netbeans. In this case, I simply copied the entire BuiltInSensorsDemo folder and renamed the new folder to "PickyMidlet". I then changed several things in the folder to reflect the new name and make sure it compiles and deploys correctly. In the project.xml file in the nbproject folder, rename the BuiltInSensorsDemo to PickyMidlet. I also changed the name in the build.xml file to reflect the change in nomenclature. FInally, in the resources/META-INF/manifest.mf file, change the name of the midlet to reflect the new name.

Now, open Netbeans 5.0 and go on over to where you have your "PickyMidlet" folder. Open the project and Netbeans will show the new project in the left hand pane.

Sun SPOT and Java ME/J2ME.

Now, safely rename the original midlet to the new PickyMidlet name via Netbeans' refactoring. You can obviously leave the same name, but just for the sake of differentiation it would probably be a good idea to change the name.

Sun SPOT and Java ME/J2ME.

Set PickyMidlet as the main project and then clean and build the project. You
should be able to compile the project with no problems as this is based completely
as of this point to the old demo Midlet.

The original midlet cleverly uses various LED lights to convey changes in movement, temperature, and light intensity to outside observers. However, I could do without the blinking lights and so I modified the original code to make it simpler for demonstration purposes. I also personalized the Midlet by having it respond to variations in temperature as a person might. As you can see from the code sample below, getting values from the sensor boards are easy and simple to do.



You can click here to continue reading the guide to this simple procedure.

Related Topics >>