Writing From Scratch is Noble, but . . .
JavaOne 2001 was the usual circus. I ducked away from the hoards of Java developers, into the room where the challenge was being held. It was hard to take six hours out of my frantic conference schedule, but I was ready and willing.
It began without much fanfare. I and 24 other programmers wandered around, like dogs sniffing for their sacred spots. The feng shui felt good in one corner, and there I sat.
The first half hour or so was spent getting familiar with the territory, tools, and techniques. The machines were set up with the Java Micro Edition of Metrowerks Code Warrior, which seems by all accounts to be a good development tool. But I was used to WordPad along with a quick and simple J2ME Wireless Toolkit from Sun -- nothing more than a console that let you compile, pre-verify, and package your MIDlet files and then test them out on various emulators. The files are called MIDlets instead of Applets, by the way, because the phones run a special form of J2ME called the MIDP (Mobile Information Device Profile).
Although writing from scratch is noble, time was short. It made much more sense to take a good, working piece of code and use that as the framework. I scanned the various sample applications available for us to look at and stole one. I tore it apart until it was nothing more than a glorified "Hello World" program. But I knew, at least, that I had something workable.
Before going too far, I also wanted to play with the process of getting the MIDlet off the computer and onto the phone. Each phone was attached to a PC using a simple serial cable. Sun's Wireless Toolkit takes all your MIDlet classes, graphics, and other resources and combines them into one JAR file and a JAD, a short descriptor file that summarizes what's inside the JAR. Using Motorola's software, it was crazily easy to connect to the phone and upload the JAR and JAD.
The interface of the phone itself is pretty intuitive. You just hit the Menu button, and the second option down is Java Apps. You scroll down to the application you want, hit the Select button, and off you go.
So by now I'd wasted about an hour. It was time to rock.
|Figure 2. Menus guide a user through choices of lights and appliances to control.|
Building the Controller
The key to writing a user interface in J2ME is to stick to the basics as much as possible. MIDP makes it really, really easy to assemble forms using stuff like radio buttons, check boxes, and text fields. But if you want to get a little trickier and create your own components from scratch, you're going to have lots of debugging time ahead of you.
It didn't take long to set up a hierarchical menu system that lets you choose which room you want to control, and then lets you choose which light or appliance you want to mess with. For the sake of this challenge, everything was hard-coded to suit my apartment, my lights, my air conditioning, and my Mr. Coffee. A real application, of course, would be much smarter. It would fetch an XML properties file from a server and craft the interface dynamically, so that you could hit one server and control a huge office building with complicated machinery, and hit another server to control the porch light at a log cabin.
Each appliance was set up as a check box. If the box was checked, the appliance was already on. You could check or uncheck any box you wanted, and then select the "Do It" command. The program would figure out what changes you made and then send them via HTTP as parameters to a simple Servlet.
It took a few hours to get all the kinks out, and to handle some refresh bugs. But it was all working by hour four. You could turn on or off any appliance, and your changes would be reflected on the server. Of course, the server wasn't actually attached to anything. It was just a proof of concept. It would have been really impressive to use the cell phone to turn off all the lights throughout the Moscone Convention Center, but I probably would have gotten arrested.
More or less happy with the results, I grabbed a few brownies, drank a few more Cokes, and felt the caffeine and sugar surge like a little army through my veins. I hopped around the room, peeking at the competition. Most people had their noses inches away from the screen, their fingers fluttering across the keyboard, utterly rapt with what they were doing. One guy had his head between his knees, groaning. In other words, it looked like a typical development house.