The Java Bluetooth API

The Java Bluetooth API is designed to run in J2ME environments: cell phones, PDAs, and the like. In particular, it requires the Connected Limited Device Configuration (CLDC) with at least 512K of memory available to Java. It also requires vendor support. Currently, almost all the devices that support the Java Bluetooth API are mobile phones. At the time of this writing, they include the Motorola A1000; several Nokia phones, including the 6260; the Sony Ericsson P900, P908, and P910; and the Siemens S65, S66, and SK65.

The Java Bluetooth API supports four (out of a couple of dozen) Bluetooth profiles:

Generic Access Profile (GAP)

Supplies the bare minimum of functionality all other services require

Service Discovery Application Profile (SDAP)

Enables clients to find out which services the device supports

Serial Port Profile (SPP)

Uses the RFCOMM protocol to emulate an RS-232 serial port

Generic Object Exchange Profile (GOEP)

Uses OBEX to transfer data such as contact databases, pictures, and phone logs between devices

These primarily enable networking use cases such as uploading pictures from camera phones and downloading games to phones. The Java Bluetooth API doesn support any of the other profiles, such as the Advanced Audio Distribution Profile or the Basic Printing Profile. However, many of these services may be available through other APIs and the host operating system.

In many cases, Java doesn care whether a device is connected via Bluetooth, USB, the network, or something else. Java treats a Bluetooth mouse or keyboard the same as it does one plugged into the serial port or a USB hub. A Bluetooth LAN adapter can be accessed via the Generic Connection Framework (GCF). A Bluetooth printer may be accessible through the Java Printing API. Direct Bluetooth connections are usually necessary only for relatively special-purpose devices, like GPS receivers and laboratory thermometers that don have existing Java drivers.

.2.1. UUIDs

Bluetooth uses Universal Unique Identifiers (UUIDs) to identify protocols and service classes. A UUID is a 128-bit number that is almost certainly unique among other 128-bit numbers, barring deliberate attempts to create clashes. For example, the UUID for the RFCOMM protocol is 0x0000000300001000800000805F9B34FB.

Because space is at a premium in many Bluetooth devices, many services are identified with only 16 or 32 bits rather than the full 128 a UUID requires. For example, the 16-bit UUID for the RFCOMM protocol is 0x0003. The Bluetooth specification converts these shortened UUIDs into full 128-bit UUIDs by starting with the 16-byte base address 0000-0000-0000-1000-8000-0080-5F9B-34FB (in hexadecimal) and then replacing the third and fourth bytes with the two bytes of the 16-bit UUID.

The javax.bluetooth.UUID class recognizes all three widths of UUID (16-bit, 32-bit, and 128-bit) and can convert between them as necessary. Most methods in the Bluetooth API that take a UUID as an argument expect to see it in the form of a UUID object rather than a string or a number.

Java 5 added a java.util.UUID class that conflicts with this class. Normally, Bluetooth applications should use only javax.bluetooth.UUID. If its necessary to use both types in the same class, be sure to use fully qualified package names for both.

To create a Bluetooth-savvy UUID, just use one of these two constructors:

public UUID(long uuidValue)
public UUID(String uuidValue, boolean shortUUID)

The first constructor is used for the 16- and 32-bit short forms of UUIDs. A long is ironically not long enough (at 64 bits) for a full 128-bit UUID, so just pass a String argument containing its hexadecimal form to the second constructor instead and pass false for the second argument. Pass TRue for the second argument if the first argument uses the short form.

Other than these constructors, this UUID class merely defines the usual equals( ), hashCode( ), and toString( ) methods, which are used to properly compare the short and long UUIDs.

.2.2. The Bluetooth Control Center

One of the least well-defined parts of the Java Bluetooth API is the Bluetooth Control Center (BCC). The BCC is a class, program (possibly native), default set of properties, or something that enables the user, vendor, or developer to:

  • Specify security preferences.
  • List previously known remote Bluetooth devices.
  • List trusted remote Bluetooth devices.
  • Pair two devices.
  • Authorize connection requests.

Some BCCs offer additional functionality, such as changing the Bluetooth device name, setting timeouts, resetting devices, initializing the stack, and listing the services on the local device.

The Java Bluetooth API specification deliberately doesn say much about what the BCC really is. It varies from one implementation to the next. It can even be a fixed set of unchangeable defaults compiled into the implementation-specific code.

.2.3. Initialization

There are many different implementations of the Java Bluetooth API from many different vendors. Atinav has one. Rococo has one. Blue Cove has an open source implementation, and there are others. Some run on PDAs and cell phones. Some run on standard desktop hardware. They are available for a variety of operating systems, including Linux, Windows, Mac OS X, Palm OS, PocketPC, and Symbian OS. While working on this chapter, I mostly used Avetana GMBHs implementation from An open source version of this stack for Linux can be found at

Some of these implementations require initialization before any of the classes and methods discussed in this chapter will work. This initialization is normally done once in any given program. For example, if you e using Atinavs aveLink BT SDK for Java, you have to place the following static initializer block in the class that starts the application:

static {

In this implementation, BCC is the class that represents the Bluetooth Control Center. However, this part varies from one implementation to the next. Youll need to consult the documentation for your implementation. (One of the nice features of the Avetana implementation is that its self-initializing. No explicit initialization is required.) This means most code you write will have at least some platform-dependent details.

Basic I/O

Introducing I/O

Output Streams

Input Streams

Data Sources

File Streams

Network Streams

Filter Streams

Filter Streams

Print Streams

Data Streams

Streams in Memory

Compressing Streams

JAR Archives

Cryptographic Streams

Object Serialization

New I/O



Nonblocking I/O

The File System

Working with Files

File Dialogs and Choosers


Character Sets and Unicode

Readers and Writers

Formatted I/O with java.text


The Java Communications API


The J2ME Generic Connection Framework


Character Sets

show all menu

Java I/O
Java I/O
ISBN: 0596527500
EAN: 2147483647
Year: 2004
Pages: 244
Similar book on Amazon © 2008-2017.
If you may any questions please contact us: