HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML

This is Part 1 of a two-part article that will show you how to write wireless J2ME applications using Bluetooth. If you're a J2ME developer, this will quite likely be your first introduction to Bluetooth. Perhaps you've heard a lot about it, but you're not sure what it is or how it works. Well, you've come to the right place.

First, we'll look at the Bluetooth protocol, then compare it to another common wireless technology: 802.11b. Next, we'll look at the anatomy of a Java Bluetooth­enabled device and examine its components in detail. Later on, we'll dive into the details and explain the purpose of the Bluetooth stack and profiles (not to be confused with J2ME profiles). Finally, we'll wrap up by providing some example code that shows how to initialize your Bluetooth stack according to the Java APIs for Bluetooth (JSR-821.0a).

What Is Bluetooth?
Simply stated, Bluetooth is a wireless communication protocol that you'd use to connect two or more Bluetooth-capable devices. In this sense it's like any other communication protocol, such as HTTP, FTP, SMTP, or IMAP. Bluetooth is also similar to these protocols in that it has a client/server architecture. In Bluetooth, the one who initiates the connection is master (the client), and the one who receives the connection (the server) is the slave. However, what makes Bluetooth so special is that it's wireless. You can connect two Bluetooth devices to share data or transfer files without using messy cables.

Bluetooth vs 802.11b
If you've heard of Bluetooth, you've certainly heard of 802.11b (the wireless LAN protocol) ­ both of which are wireless communication protocols. Bluetooth and 802.11b are geared to accomplish two different goals, although the technologies operate in the same frequency band: 2.4GHz. The question many people ask is: If both technologies operate at the same frequency, won't they interfere when placed in range with each other? Not so, according to a Forrester Research study in 2001.

Another common question: If they both do the same thing, won't one eventually replace the other? The key point is that they're not intended for the same purpose. Bluetooth will replace 802.11b (or vice versa) when, for example, the microwave replaces the conventional oven.

Microwave vs Oven
The oven is great at cooking, but bad at heating food quickly ­ and it's also pretty expensive. Conversely, the microwave is cheap and great at heating food quickly, but it's bad at cooking. Both devices have their trade-offs, although either could be used for heating and cooking. How does all this compare to wireless communication?

It's pretty simple. Wireless LAN (802.11b) is good at connecting two relatively large devices with lots of power at high speeds. A good use of the technology is connecting two laptops at 11Mb/s. Wireless LAN is also good at connecting those devices at long distances (up to 300 ft).

Bluetooth is ideal if you want to connect small devices at slower speeds (1Mb/s) and within a shorter range (30 ft.). Why slower speeds? Isn't faster better? Isn't long-range communication a good thing? Not necessarily, especially when you consider the memory and power constraints on smaller devices. Faster connection speeds and longer communication ranges equate to larger power requirements. Why use an 11Mb/s connection if you're transferring a 50K file between two PDAs?

One of Bluetooth's strengths is its ability to function as a cable replacement technology. If you have multiple peripherals connected to your computer using RS-232 or USB, then Bluetooth is the ideal solution if you want to use those devices wirelessly. It's difficult (if not impossible) to connect computer peripherals using 802.11b technology (except for printers). Bluetooth even has a built-in capability for wireless audio communication. To put things succinctly: Bluetooth will never replace 802.11b because it's bad for:

  • Large file transfers between devices
  • Long-range communication
On the other hand, 802.11b will never replace Bluetooth because:
  • It can't be used to communicate to peripherals.
  • It requires too much power for small devices.
  • It's overkill for small data transfers.
  • It wasn't designed for voice communication.
For the moment in the wireless communications arena, there's no technology that's best suited for every possible application. Either Bluetooth or 802.11b can be utilized for wireless communication between computers. Both have their place in the market, and both can perform remarkably well in their niches.

Bluetooth Specification
For Bluetooth devices to communicate properly, they need to conform to the Bluetooth Specification. This specification, like any other spec, defines the standard that a Bluetooth device should adhere to, as well as rules that need to be enforced when communicating. You can download the specification documents from www.bluetooth.com.

Java Bluetooth Application Anatomy
Let's look at the anatomy of a Java Bluetooth application. Figure 1, section A, shows a Bluetooth-enabled phone that includes the CLDC MIDP platform; section B contains the anatomy of the device shown in section A. The bottom layer of the diagram in section B represents the actual Bluetooth device that sends out your voice and data over the air: the Bluetooth radio. Section A shows the Bluetooth device as a peripheral, but it could easily be integrated into the device by the OEM. Bluetooth radios come in many shapes and sizes; Figure 2 shows the popular Bluetooth radio made by 3Com. 3Com also makes a handy network browser with their Bluetooth device (see Figure 3).

The Bluetooth Protocol Stack
The next essential component of a Java Bluetooth application is the Bluetooth stack, (again, see Figure 1, section B). The Bluetooth stack is the software layer with direct access to the Bluetooth radio as well as control over such things as device settings, communication parameters, and power levels. The stack consists of many layers, and each layer has a specific task in the overall Bluetooth device (see Figure 4). The Bluetooth device manufacturer is not required to use all the layers, so let's cover the main ones that should be in most Bluetooth devices.

  • Host Controller Interface (HCI): The interface between the radio and the host computer

  • Logical Link Control and Adaptation Protocol (L2CAP): The multiplexer of all data passing through the unit; audio signals, however, have direct access to the HCI

  • Service Discovery Protocol (SDP): Used to find services on remote Bluetooth devices

  • RFCOMM: Widely known as the virtual serial port protocol

  • OBEX: Object exchange protocol

    Figure 1
    Figure 1

    Figure 2
    Figure 2

    Figure 3
    Figure 3

    Figure 4
    Figure 4

    Bluetooth Profiles
    The next level in Figure 1, section B, is Bluetooth profiles. They were created to allow different Bluetooth devices to interoperate. For instance, let's say you own a Bluetooth-enabled PDA and a Bluetooth-enabled wireless phone. Both devices have Bluetooth stacks. How can you tell if those two devices will allow you to synchronize the phone lists between each one? How will you know if you can send a phone number from the PDA to the phone? And, most important, how can you determine if these devices will allow you to browse the Internet on the PDA using the phone as a wireless modem?

    A Bluetooth profile is a designed set of functionality for Bluetooth devices. For instance, using the examples listed above, the phone and the PDA must both support the Synchronization Profile in order to synchronize data between themselves. To send object data like a .vcf from the PDA to the phone, both devices need to have the Object Push Profile implemented. Finally, the PDA and the wireless phone must both support the Dialup Networking Profile in order for the PDA to wirelessly browse the Internet from the phone. If you want your Bluetooth-enabled devices to interact, having a Bluetooth stack is not good enough: they need to conform to a particular profile.

    Bluetooth Profiles vs J2ME Profiles
    Do not confuse Bluetooth profiles with J2ME profiles. J2ME profiles are a set of Java classes that extend the functionality of a J2ME configuration. For example, both the PDA and MID profiles are a set of Java classes that extend the functionality of the Connected Limited Device Configuration.

    A Bluetooth profile can be implemented in any language and on any platform because it refers to a defined set of functionality for a Bluetooth-enabled device. So the Object Push Profile can be implemented on a Palm OS PDA in C++ as well as on a Bluetooth-enabled printer in Assembler; it's just a defined set of functionality.

    The next two layers of software in Figure 1, section B, are the KVM and the Java Bluetooth API libraries. With J2ME and the standard Java Bluetooth implementation, you have direct access to the profiles and the stack of your Bluetooth device. The Java Bluetooth API (JSR-82) works with any Java platform that supports the Generic Connection Framework (GCF). Unfortunately, that means you can't use J2SE with the official Java Bluetooth API. Hopefully, the GCF will be incorporated into JDK 1.5. Until then, you'll have to use nonstandard libraries if you want to write Java Bluetooth code for desktop applications. The last layer in Figure 1, section B, is your application code, the wireless killer app that will make you famous.

    Java Bluetooth Application Concepts
    The basic concepts of any Bluetooth application (Java or otherwise) consist of the following components:

    • Stack initialization
    • Device management
    • Device discovery
    • Service discovery
    • Service registration
    • Communication
    The Java Bluetooth Specification adds a special component to the mix called the Bluetooth Control Center (BCC), which is outside the scope of this article.

    Stack Initialization
    Before you can do anything, you need to initialize your stack. Remember, the stack is the piece of software that controls your Bluetooth device. Stack initialization can consist of a number of things, but its main purpose is to get the Bluetooth device ready to start wireless communication. Every vendor handles stack initialization differently; Listing 1 shows how to initialize the stack using the Atinav Java Bluetooth SDK.

    Hopefully, this article has stimulated your interest in application development with Java and Bluetooth. Now that the preliminaries are out of the way, in Part 2 we can dive into more application code that will show you how to wirelessly communicate between two Java Bluetooth­enabled devices. Don't worry if you don't have any Bluetooth equipment yet; I'll also show where and how to obtain the hardware and software you'll need.

    Author Bio
    Bruce Hopkins is a senior Java consultant at Great Lakes Technologies Group in Southfield, MI. He has worked with Java for over six years, and has researched in wireless networking for four. Bruce is the coauthor of an upcoming book entitled Java and Bluetooth by Apress (November 2002). [email protected]

    Listing 1
    import javax.bluetooth.*;
    import javax.microedition.io.*;
    import com.atinav.bcc.*;
    public class WirelessDevice implements DiscoveryListener {
        LocalDevice localDevice = null;
        public WirelessDevice (){
     //setting the port number using Atinav's BCC
    //setting the baud rate using Atinav's BCC
    //connectable mode using Atinav's BCC
    //Set discoverable mode using Atinav's BCC
     localDevice = LocalDevice.getLocalDevice();
     catch (BluetoothStateException exp) {
     // implementation of methods in DiscoveryListenerclass
    // of javax.bluetooth goes here
    // now do some work

    All Rights Reserved
    Copyright ©  2004 SYS-CON Media, Inc.
      E-mail: [email protected]

    Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.