Sun Java Solaris Communities My SDN Account Join SDN
 
Article

Getting Started With Bluetooth

 
By Eric Giguere, October 2004  

One of the most exciting new features in version 2.2 of the J2ME Wireless Toolkit, currently in beta, is its support for development and testing of Bluetooth applications. Bluetooth is a set of protocols that enable devices to communicate wirelessly over short distances as needed, using low-powered radio transmitters and receivers. For example, a Bluetooth headset can be paired with a Bluetooth-capable cellphone for hands-free telephone calling, or a personal digital assistant can use Bluetooth to connect to a local area network.

Applications running on the Java platform access Bluetooth through the Java APIs for Bluetooth Wireless Technology (JABWT, also called BTAPI), an optional package that can be used with any J2ME configuration or profile. JABWT is defined as JSR 82 within the Java Community Process. The specification was finalized in 2002, but only recently have J2ME-enabled devices that support JSR 82 appeared. Of course, Bluetooth support at the device level is a prerequisite for JABWT support. As more Bluetooth-enabled devices make it to market, you can expect to see greater support for Java-based Bluetooth programming.

That the J2ME Wireless Toolkit now supports JSR 82 makes Bluetooth development and testing much simpler. The toolkit not only includes the necessary APIs to compile programs, it also lets you test Bluetooth functionality without Bluetooth hardware.

The best way to start Bluetooth development is to read the first parts of the JSR 82 specification, which include a brief overview of Bluetooth and definitions of important terms. You might also consider purchasing a book on JABWT development, either Bluetooth for Java, by Bruce Hopkins and Ranjith Antony, or Bluetooth Application Programming with the Java APIs, by C. Bala Kumar, Paul J. Kline, and Timothy J. Thompson. Generic Bluetooth books are also available, but these two Java-centric ones are better choices for J2ME development. Of course, you can always visit the official Bluetooth technical site for even more information.

Note that Bluetooth programming requires a good understanding of concurrent programming techniques. You don't want to perform Bluetooth operations on the application's event thread, for example, because you may freeze the application. Common Bluetooth operations like device discovery can take several seconds to perform, so it's important to do them on separate threads and to make the operations cancelable whenever you can. This would also be a good time to review Doug Lea's excellent Concurrent Programming in Java: Design Principles and Patterns.

After you've read about Bluetooth programming, take some time to look through the Bluetooth examples that ship with J2ME Wireless Toolkit 2.2. Open the BluetoothDemo project with KToolbar and run two instances of the application. Select the BluetoothDemo MIDlet in each instance, but run one as a server and one as a client. You can then get the server instance to publish one or more images and have the client instance find and download those images.

Then study the source files BTImageClient.java and BTImageServer.java carefully, because you'll need to use many of the techniques you find there in your own programming.

You can expect more specific Bluetooth topics in future articles and tech tips here on the Java mobility site.