Java Sun SPOT Application Development using Java ME (J2ME)Home --> Notes --> Let your Java app "feel the rain" on its skin Let your Java app "feel the rain" on its skinYour 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: 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:
The one big difference of course if that this Midlet uses some quite unusual libraries.
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.
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.
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.
Now, we can move the code to one of the Sun SPOT devices. Clean and build the entire project using Netbeans, connect one of the non-basestation Sun SPOT devices to your host computer using the USB cord, then DEPLOY by highlighting the project name on the left pane and selecting DEPLOY TO SUN SPOT.
The IDE should give you an output very similar to the one below if you are
successful.
You should note the line with the 16-digit SPOT serial number (SPOT serial number = 0014.4F01.0000.XXXX), which tells you the unique identification number for that particular Sun SPOT device. This will be useful later when it comes to creating more complex applications. Now that you've successfully deployed the midlet to the Sun SPOT device, it's finally time to see whether your your Java app can actually "feel the rain" on its skin, or at least in this case, tell the temperature and light intensity of its surroundings. I posted below the output of running this midlet. As you can see, the midlet first detected a temperature of 30 degrees and zero light (since the Sun SPOT device was first placed underneath my desk). I then took it out, and a minute later it registered an increase in light intensity. Since I was feeling somewhat emotional about all this (oh, the humanity!) , I cupped the Sun SPOT device in my closed hand and this increased the temperature to 33, then 34 degrees celsius, which seemed a bit too hot for the midlet!
Copyright © 2007 RML. Java ME and other terms are trademarks or copyright of Sun Microsystems and/or other companies. Legal Stuff. |