Create, test, and deploy applications on Java platforms for mobile and embedded devices Mobility Tech Tips

Monday Jun 02, 2008

By Richard Marejka

Question: How do you programmatically detect the presence of the JSR 82 Bluetooth APIs?

Answer: It would be nice if the answer were this:

import	javax.microedition.*;
import	javax.bluetooth.*;

...

String	BluetoothVersion = System.getProperty( "bluetooth.api.version" );
boolean	isBluetoothHere  = false;

...

if ( BluetoothVersion != null )
	isBluetoothHere = true;

On a Bluetooth v1.0 device, the previous code will always result in:

BluetoothVersion = null;
isBluetoothHere  = false;

that is, a negative or a false negative detection of Bluetooth.

The reason it is not as simple as the first code sample lies in the first version of JSR 82 Bluetooth APIs. In the original Bluetooth specification, JSR 82 v1.0a (Apr 5, 2002), section 3.3.4 Device Properties states: "This API defines the additional system properties that may be retrieved by a call to LocalDevice.getProperty(), as shown in Table 3-2."

The table in question contains a bluetooth.api.version property that will return 1.0 if Bluetooth is supported. To correctly discover Bluetooth on a v1.0 device, the required code follows:

import	javax.bluetooth.*;

...

String	BluetoothVersion = null;
boolean	isBluetoothHere  = true;

...

try {
	Class.forName( "javax.bluetooth.LocalDevice" );    // does the class exist?

	BluetoothVersion = javax.bluetooth.LocalDevice.getProperty( "bluetooth.api.version" );

} catch( ClassNotFoundException cnf ) {    // class does not exist -> no Bluetooth
	isBluetoothHere  = false;
}

Access to javax.bluetooth.LocalDevice is required to determine if Bluetooth is present. The Class.forName() method is used to test for the presence of a class in the run-time environment. If the class is found, then LocalDevice can be safely accessed to retrieve the Bluetooth version.

This somewhat non-intuitive detection method was changed in JSR 82 v1.1 (Sep 02, 2006, Maintenance Release 2). Section 3.3.4 has been revised to include the sentence: "Additionally, all properties defined in the Bluetooth API and available through the LocalDevice.getProperty method MUST also be available through the CLDC System.getProperty() method."

The same section also defines the property/value pairs in Table 3-2:

bluetooth.api.version  "1.1"
obex.api.version       "1.1"

which means the System.getProperty() method will work as expected.

As for JSR 248 MSA: Bluetooth is conditionally mandatory; that is, if there is Bluetooth hardware then the JSR 82 APIs are present. Bluetooth v1.1 is part of both MSA and MSA Subset.

Lastly, note that the first code sample will still work and produce the correct answer on both Bluetooth 1.0 and 1.1 devices. In cases where there is no Bluetooth, the result is:

isBluetoothHere  = false;
BluetoothVersion = null;

or if Bluetooth is present, the result is either

isBluetoothHere  = true;
BluetoothVersion = "1.0";

or

isBluetoothHere  = true;
BluetoothVersion = "1.1";
Comments:

Hi Christine would you join my project to develop better wi fi api

Posted by Raj Nongkhlaw on June 22, 2008 at 04:43 PM PDT #

hi! i am in search of a project of voice transfering trough bluetooth device.

Posted by Pawan on July 02, 2008 at 12:47 PM PDT #

your content is good

Posted by hi iam thara on July 14, 2008 at 01:15 AM PDT #

Great! I needed this one. I wish Sun would invest a lot of effort in creating tutorials like this one. (Google-ling would give me very old articles...)

Posted by thirdy on August 13, 2008 at 08:34 AM PDT #

Thank you. We invite you to send us specific suggestions for tutorials you'd like to see. You can email me directly at christine.dorffi@sun.com or leave comments here.

Posted by Christine Dorffi on August 13, 2008 at 11:16 AM PDT #

Please, do you have any tutorial that can help out. I want to integrate data from two applications residing on a desktop and a palm top using a bluetooth technology.
thanks.
kunle

Posted by Kunle Oyerinde on December 16, 2008 at 06:55 AM PST #

Post a Comment:
  • HTML Syntax: NOT allowed