Using Bluetooth stack in desktop application
I tried to find information about developing desktop applications with Bluetooth support using Java but it seems that most of the articles are concentrated in mobile J2ME implementations. Now that I wanted to develop desktop bluetooth application I thought that I’d write down how I managed to do so. Read on, if you are interested…

Step 1. Find Bluetooth Stack Implementation for J2SE
There aren’t many JSR-82 implementations available for the desktop environment. Most of them are commercial but I was able to find the open source version from www.javabluetooth.org. So the first thing was to download the source code and try to compile it. It wouldn’t compile as it depended on the Java Communications API (javax.comm.*). After I had downloaded the comm library I added comm.jar to my class path and I was able to compile the JavaBluetooth library. You can download the compiled binary here.
But… It turned out that JavaBluetooth library only supports Bluetooth devices with serial connection and thus it didn’t work with my laptop that has a built-in Bluetooth adapter.
This led me to another great site, www.javabluetooth.com, which has a small list of Java Bluetooth SDKs and it included a library called BlueCove. This library works only on Windows platform but it will do for now as there aren’t other free options to choose from.
Step 2. Hello Bluetooth World
Next thing was to try it out by coding a simple application that would sniff any nearby bluetooth devices using the laptops built-in bluetooth adapter. This was easier then I thought.
You have to create class that will implement DiscoveryListener interface and the use DiscoveryAgent to search for devices. You can get the discovery agent from LocalDevice object as seen in the following code sample:
public class BluetoothBrowser
implements DiscoveryListener {
private LocalDevice localDevice;
private DiscoveryAgent agent;
/** Creates a new instance of BluetoothBrowser */
public BluetoothBrowser()
throws BluetoothStateException {
localDevice = LocalDevice.getLocalDevice();
agent = localDevice.getDiscoveryAgent();
}
/** Start inquiry */
public void inquiry()
throws BluetoothStateException {
agent.startInquiry(DiscoveryAgent.GIAC, this);
}
...implementations for DiscoveryListener...
Step 3. Done
You can download the source code from here. The package also includes project files for the NetBeans IDE 6.0 M10.So this should get you started… I’ll might write more about using Bluetooth with desktop applications in the future posts.
July 22nd, 2007 at 15:29
Thanks for this wonderfull support. i’m having a problem in running this application as m not much good in java.
The other thing is that i want to send images from my PC to several mobile phones at a time. As my goal is to advertise in a shopping mall by creating a bluetooth network. if u can help me in my project so i’l be very thankfull to you. i don have much time left because i hv to submit my project in a month. so please if you have anything that can help me so plz do mail me.
Thanking You
Regards,
Romail
July 22nd, 2007 at 16:00
Hi,
I try this code but i am getting Error as below….
I am getting Blue tooth Stack is not intialized.. i dont know how to slove this problem. Plz can u help me !!
Output (Error):
LOG: Start discovery
class.getName() javax.bluetooth.LocalDevice
localDevicejavax.bluetooth.LocalDevice
error BluetoothStack not detected
com.intel.bluetooth.BlueCoveImpl.(BlueCoveImpl.java:136)
Exception in thread “AWT-EventQueue-0″ java.lang.ExceptionInInitializerError
at com.intel.bluetooth.BlueCoveImpl.instance(BlueCoveImpl.java:195)
at javax.bluetooth.LocalDevice.(LocalDevice.java:65)
at javax.bluetooth.LocalDevice.getLocalDevice(LocalDevice.java:80)
at com.substanceofcode.bluetooth.BluetoothBrowser.(BluetoothBrowser.java:43)
at com.substanceofcode.presence.Controller$1.run(Controller.java:45)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:234)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Caused by: java.lang.RuntimeException: BluetoothStack not detected
at com.intel.bluetooth.BlueCoveImpl.(BlueCoveImpl.java:137)
at com.intel.bluetooth.BlueCoveImpl.(BlueCoveImpl.java:35)
at com.intel.bluetooth.BlueCoveImpl$SingletonHolder.(BlueCoveImpl.java:82)
… 12 more
Thanks & Regards
M.S.HariKumar
July 22nd, 2007 at 23:28
HariKumar:
The error message seems to indicate that it didn’t recognize the local bluetooth device. What adapter are you using? USB? Integrated?
Romail:
I understand what you are trying to achieve but I don’t have experience either on developing that kind of a service. I suppose that it could be implemented so that the server would scan the surrounding devices like every 10 seconds and if it finds any new devices it would then try to send an image to the client. I assume that you are able to send a file to one phone at once or maybe when you’d had several bluetooth adapters then you could do parallel transmissions with each adapter. I’d be very interested in seeing your code if you are willing to share it?
August 10th, 2007 at 13:35
Superb! A good article. Simple yet understandable. Cheers, Wyell
August 16th, 2007 at 09:51
hi ,
I am trying to connect pc-pc through bluetooth and J2se .
U r program provide me wonderful support but I have another problem that How I find Address of other connected device ?
plz , help my.
Thanks
August 19th, 2007 at 22:46
Hi Tommi,
im still unable to achieve my goal, please if u have any solutions please do mail me. pleaseeeeeeee….
Best Regards
Romail
August 20th, 2007 at 22:47
Hi Tommi,
Thanks for your reply.
Just to remind you that my project scenario is to develop a software which can send images from a PC to several mobile phones concurrently through bluetooth. i need to develop it on J2SE.
I send u the code twice but both the time i got delivery failure…
My code need a plug n play bluetooth usb dongle but m using a driver one. second thing is that this code only send to single device but i hv to send it to multiple device concurrently. if you can help me il be very thankful to you.
Best Regards,
Romail
August 23rd, 2007 at 05:40
hi ,
Tommy sir ,
I solve my problem myself .
bye.
August 23rd, 2007 at 07:47
Arifa: That is great! Would you like to tell how you did it?
August 25th, 2007 at 08:58
hi,
i’m trying to develop a bluetooth application,but the first thing i need is the javax.bluetooth library or more specifically the jsr-82 implementation.i’ve tried hard to get it but i can’t get it.so plzzz arifa can u u tell me where u got urs?
i’m hanging on ur words,reply soon
August 25th, 2007 at 09:22
Quratulain: You should get the JSR-82 implementation from http://www.javabluetooth.org/ atleast if you are targeting for the Windows platform. You should be able to find more implementations using the google search: http://www.google.fi/search?q=jsr-82+implementation+download
August 25th, 2007 at 09:27
thanx a lot sir!
i’ll check them out and i daresay,again!
August 27th, 2007 at 12:38
Sure,
In BluetoothBrowser.java
public void deviceDiscovered(RemoteDevice device, DeviceClass devClass) {
try {
Log.add(“Device discovered Name: ” + device.getFriendlyName(false));
Log.add(“Device discovered Address: ” + device.getBluetoothAddress());
} catch (IOException ex) {
Logger.getLogger(“global”).log(Level.SEVERE, null, ex);
}
}
September 5th, 2007 at 00:59
Dear Tommy, I follow the steps but I getting de same error that HariKumar. I using USB adapter and test the code under Linux platform and Windows but its the same error. Its very important, thanks.
September 5th, 2007 at 23:44
Richard, the BlueCove driver only works with Windows XP SP2 and above. But as it says in the BlueCove’s project page it should also support Linux in upcoming releases: http://code.google.com/p/bluecove/
September 6th, 2007 at 14:33
Thank you for your answer, yesterday searching information about the error I found a jsr-82 implementation that works under Linux y its wonderful. But thank to your comments and the rest of the people I could clear several doubts about how develop desktop applications with Bluetooth support using Java Standard Edition.
September 7th, 2007 at 07:53
Richard: Would you be willing to share what driver did you find for the Linux platform? Does anyone know any multi platform drivers that are free to use?
September 7th, 2007 at 15:33
Sure, http://sourceforge.net/projects/avetanabt/ from here can you download de jsr-82 implementation, under Linux platform its free to use but in Windows and Mac OS don’t. http://sourceforge.net/project/showfiles.php?group_id=176926&package_id=206506 here can you download an example of the implementation, its very simple to use and with that the only limitation for the desktop applications with Bluetooth support using J2SE it the programmer imagination. This is the home page of Avetana
http://www.avetana-gmbh.de/avetana-gmbh/produkte/jsr82.eng.xml and the API’s can you download from here http://www.avetana-gmbh.de/avetana-gmbh/produkte/doc.tgz. I hope that it is useful for you and the rest of the people. Any doubts just ask and don’t forget of sharing because is the essence of the community.
September 12th, 2007 at 00:25
Hi Tommy!
very good example to read here. i have got to do the same kind of job so requesting for your help. . . i have to develop a software which will send a mobile application to the mobile phones from PC using bluetooth so later on the mobile phones could use that application (also to as many mobile phones at a time as they come into bluetooth range). can you please guide me where i have to start from to get this finished asap coz i’ve got v short time. thanks and waiting for your thoughtful reply.
regards
Muhammad.
September 13th, 2007 at 16:26
Hello! I have problem: my IO:
C:\Documents and Settings\Root\Pulpit\java\bluetoothpresence\src\com\substanceofcode\bluetooth\BluetoothBrowser.java:15: package javax.bluetooth does not exist
import javax.bluetooth.BluetoothStateException;
C:\Documents and Settings\Root\Pulpit\java\bluetoothpresence\src\com\substanceofcode\bluetooth\BluetoothBrowser.java:16: package javax.bluetooth does not exist
import javax.bluetooth.DeviceClass;
C:\Documents and Settings\Root\Pulpit\java\bluetoothpresence\src\com\substanceofcode\bluetooth\BluetoothBrowser.java:17: package javax.bluetooth does not exist
import javax.bluetooth.DiscoveryAgent;
C:\Documents and Settings\Root\Pulpit\java\bluetoothpresence\src\com\substanceofcode\bluetooth\BluetoothBrowser.java:18: package javax.bluetooth does not exist
import javax.bluetooth.DiscoveryListener;
C:\Documents and Settings\Root\Pulpit\java\bluetoothpresence\src\com\substanceofcode\bluetooth\BluetoothBrowser.java:19: package javax.bluetooth does not exist
import javax.bluetooth.LocalDevice;
C:\Documents and Settings\Root\Pulpit\java\bluetoothpresence\src\com\substanceofcode\bluetooth\BluetoothBrowser.java:20: package javax.bluetooth does not exist
import javax.bluetooth.RemoteDevice;
C:\Documents and Settings\Root\Pulpit\java\bluetoothpresence\src\com\substanceofcode\bluetooth\BluetoothBrowser.java:21: package javax.bluetooth does not exist
import javax.bluetooth.ServiceRecord;
C:\Documents and Settings\Root\Pulpit\java\bluetoothpresence\src\com\substanceofcode\bluetooth\BluetoothBrowser.java:27: cannot find symbol
symbol: class DiscoveryListener
public class BluetoothBrowser implements DiscoveryListener {
C:\Documents and Settings\Root\Pulpit\java\bluetoothpresence\src\com\substanceofcode\bluetooth\BluetoothBrowser.java:29: cannot find symbol
symbol : class LocalDevice
location: class com.substanceofcode.bluetooth.BluetoothBrowser
private LocalDevice localDevice;
C:\Documents and Settings\Root\Pulpit\java\bluetoothpresence\src\com\substanceofcode\bluetooth\BluetoothBrowser.java:30: cannot find symbol
symbol : class DiscoveryAgent
location: class com.substanceofcode.bluetooth.BluetoothBrowser
private DiscoveryAgent agent;
C:\Documents and Settings\Root\Pulpit\java\bluetoothpresence\src\com\substanceofcode\bluetooth\BluetoothBrowser.java:33: cannot find symbol
symbol : class BluetoothStateException
location: class com.substanceofcode.bluetooth.BluetoothBrowser
public BluetoothBrowser() throws BluetoothStateException {
C:\Documents and Settings\Root\Pulpit\java\bluetoothpresence\src\com\substanceofcode\bluetooth\BluetoothBrowser.java:38: cannot find symbol
symbol : class BluetoothStateException
location: class com.substanceofcode.bluetooth.BluetoothBrowser
public void inquiry() throws BluetoothStateException {
C:\Documents and Settings\Root\Pulpit\java\bluetoothpresence\src\com\substanceofcode\bluetooth\BluetoothBrowser.java:42: cannot find symbol
symbol : class RemoteDevice
location: class com.substanceofcode.bluetooth.BluetoothBrowser
public void deviceDiscovered(RemoteDevice device, DeviceClass devClass) {
C:\Documents and Settings\Root\Pulpit\java\bluetoothpresence\src\com\substanceofcode\bluetooth\BluetoothBrowser.java:42: cannot find symbol
symbol : class DeviceClass
location: class com.substanceofcode.bluetooth.BluetoothBrowser
public void deviceDiscovered(RemoteDevice device, DeviceClass devClass) {
C:\Documents and Settings\Root\Pulpit\java\bluetoothpresence\src\com\substanceofcode\bluetooth\BluetoothBrowser.java:50: cannot find symbol
symbol : class ServiceRecord
location: class com.substanceofcode.bluetooth.BluetoothBrowser
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
C:\Documents and Settings\Root\Pulpit\java\bluetoothpresence\src\com\substanceofcode\bluetooth\BluetoothBrowser.java:34: cannot find symbol
symbol : variable LocalDevice
location: class com.substanceofcode.bluetooth.BluetoothBrowser
localDevice = LocalDevice.getLocalDevice();
C:\Documents and Settings\Root\Pulpit\java\bluetoothpresence\src\com\substanceofcode\bluetooth\BluetoothBrowser.java:39: cannot find symbol
symbol : variable DiscoveryAgent
location: class com.substanceofcode.bluetooth.BluetoothBrowser
agent.startInquiry(DiscoveryAgent.GIAC, this);
17 errors
BUILD FAILED (total time: 3 seconds)
What i must do????
September 14th, 2007 at 13:37
ur code doesn’t work.
localDevice = LocalDevice.getLocalDevice(); ==> return null
I tried to follow that line with:
System.out.println(localDevice.getBluetoothAddress()); ==> result: 000000000000
FYI, I’m using desktop pc with usb dongle from IVT corporation bluesoleil.
The driver is version 1.4 I think.
I’m already using XP SP2.
Why the code doesn’t work?
September 14th, 2007 at 13:43
Hi Alex! What JSR-82 implementation are you using? Are you using the BlueCove library that I mentioned in this entry or some other implementation?
September 14th, 2007 at 14:13
yes, it is. I’m using bluecove-2.0.1.jar
any idea Tom?
September 14th, 2007 at 14:22
Alex, you could try out using other implementations like Avetana, BlueSock or ElectricBlue.
September 14th, 2007 at 16:50
Tom, I still cannot do the following code:
LocalDevice localDevice = LocalDevice.getLocalDevice();
with all those implementations. I dunno why but BlueSock and ElectricBlue always generate a runtime exception.
Is there anything I should write before that line?