J2ME Secrets
 

When Bluetooth Isn't Bluetooth

My first app involved communicating with a Bluetooth GPS to use the phone as a display device for Geocaching. My Motorola RAZR V3 bills itself as a Bluetooth enabled phone so I whipped up a bit of Bluetooth code piecing together somthing from a number of example code snippets found on the various developer sites. However, when I loaded the code on my phone using the developer tools, it kept throwing exceptions when I tried to make Bluetooth API calls. When I dug into it a bit, I found that though the phone has Bluetooth capability, it is primarily reserved for the hands free headset option. In fact, the on-board hardware capability for Bluetooth is not exposed in the device Java environment. As far as J2ME was concerned, I didn't have a Bluetooth phone. This leads to lesson one.

Look Into Your Target Device Detailed Specs and Make Sure That Your Intended API is Implemented.

In the specs for the devices, you want to look for specific "JSR" references. JSR stands for Java Specification Requests. These are a part of the Java Community Process (JCP) and you'll soon get to know and love the JSR's you hold dear. In my case, the Bluetooth API is described in JSR 82. So if you plan to do J2ME programming around Bluetooth on a device, you need to make sure that the J2ME "stack" loaded on it supports JSR 82. Unfortunately you generally can't find this information in the marketing docs you'll find on your device vendor's site. You generally have to go to the developer literature for the device to get this kind of detail. But it is a very important step. There is nothing more frustrating than developing a piece of code for a device only to go and find that you never had a chance of getting it to run because the Java environment in the firmware of the device doesn't support a key API you intend to use.

DO NOT rely upon finding your desired feature listed as a bullet. There are quite a number of phones in current production that have "Bluetooth support" or "Bluetooth enabled" as bullets in their descriptive literature and specs but which do not support JSR 82. That was the case with my RAZR V3. Unless you specifically see the JSR you are looking to use supported, don't assume the capability is there on the basis of the other marketing prose and terms.

Once I'd figured this out, I began searching my carrier's available phones for units that supported JSR 82. I found a number of them and picked out my next phone. Using one of my accounts that was eligible for upgrade, I obtained the new phone by signing up for a plan extension. I'll tell you why that was a bad idea a bit later on.

When I got home with my new phone, I couldn't wait to try out the Bluetooth features. I loaded up my test code and wonder of wonders, the API was there! The sample code did Bluetooth device discovery and sure enough, my little Bluetooth GPS (and my PC with its Bluetooth USB dongle) showed up in the list of nearby devices that MY program had discovered. Things were starting to look good. So I dove right back into app development in earnest. I could hardly wait to finish the Bluetooth parts and try out the full application.

Little did I know that one of the darkest, dirtiest little secrets of the J2ME world was about to stop me dead in my tracks. Not knowing this secret can cost you literally THOUSANDS of dollars and numerous ulcers and headaches. It's one they never tell you at developer conferences when some speaker is hyping the virtues of mobile device development and spinning the siren song of markets consisting of "millions of mobile devices and customers for your apps." The reefs are there just below the surface waiting to sink you to the bottom. You might well invest hundreds and even thousands of dollars in development effort and in the purchase of associated wares on the way to market with your creation, only to become suddenly shipwrecked on the jagged reefs of J2ME reality. I'm going to try and spare you from that.

Page 3