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…

Desktop Bluetooth Example (J2SE)

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.

This entry was posted in Java, Tips and Tricks. Bookmark the permalink.

133 Responses to Using Bluetooth stack in desktop application

  1. kopok says:

    hi tommy and everyone..

    my q’s:

    1. how to create looping in java? please urgent! give me sample plz

    my situatuion is my bluetooth on desktop application just detected a device nearby(cellphone). but, how to make a loop to btooth find another devices nearby?

  2. anne says:

    hi tommi.
    your code is superb.
    how is it if i want to save the all the device name that have discovered to text file?eg:annenokia,juanokia,etc..hope u can answer to me a.s.a.p
    thank in advanced

  3. Shamit Jain says:

    hi,

    This is really a great article.

    I want to communicate J2SE server PC and J2ME client. I use the Bluecove.jar and Bluesoliel USB bluetooth dongle. My J2ME client sends the text to the J2SE server. Its connect to the Bluesoliel dongle and shows the text transfer through serial port. But on server I get nothing even no error.

    On server side it shows:

    BlueCove version 2.1.0 on bluesoleil
    Waiting for connection…

    Nothing happend here even no exception.

    Please help me if any one has expertise on this.

    Thanks in advance

  4. rotit says:

    Hi,

    Thanks for this man.
    But I am facing an issue when I tried to run the code on MAC having a built in Bluetooth..
    as I ran the code it shows the following error

    LOG: Start discovery
    ERROR: Error in main: Unable to load HCIManager. org.javabluetooth.stack.hci.HCIException: BluetoothStack not initalized.
    LOG: Discovery end

    can you please help me?

    Regards,
    Rohit Mandiwal

  5. ahlem says:

    Hello I tried similare code using blucove package but i get some errors
    try to help me if u have any ideas plz
    output errors
    Exception in thread “main” javax.bluetooth.BluetoothStateException: BluetoothStack not detected
    at com.intel.bluetooth.BlueCoveImpl.detectStack(BlueCoveImpl.java:334)
    at com.intel.bluetooth.BlueCoveImpl
    .access$100(BlueCoveImpl.java:64)
    at com.intel.bluetooth.BlueCoveImpl$1.run(BlueCoveImpl.java:502)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.intel.bluetooth.BlueCoveImpl.detectStackPrivileged(BlueCoveImpl.java:500)
    at com.intel.bluetooth.BlueCoveImpl.getBluetoothStack(BlueCoveImpl.java:489)
    at javax.bluetooth.LocalDevice.(LocalDevice.java:64)
    at javax.bluetooth.LocalDevice.getLocalDeviceInstance(LocalDevice.java:71)
    at javax.bluetooth.LocalDevice.getLocalDevice(LocalDevice.java:86)
    at DeviceDiscovery.BluetoothDeviceDiscovery.main(BluetoothDeviceDiscovery.java:42)

  6. Tosin says:

    I am trying to compile your code but it is complaining about the GroupLayout. I am using Netbeans and also it is complaining about the javaComm package.

    Pls could you help out.

    Thanks

  7. Ricardo says:

    Hi.

    The bluetooth example works great!

    I want to send a “hello world!” from my desktop (USB dongle) to a cell phone without pairing them. Is that possible?

    Any ideas where to find info. about this?

    Regards

  8. zubair says:

    plz can any one give the code of how to send file from my laptop to mobile via bluetooth using avetana API’S

  9. Alberto says:

    Hey Tommi, this tutorial is exactly what I needed, I’m working on communicating my Laptop with my Cellphone thru Bluetooth and having a Widcomm Bluetooth adapter I downloaded their SDK but it’s kind of hard to develop software with their classes (i’m familiar with C++ but not with their classes). I didn’t know how to implement bluetooth in a Laptop computer.
    Thank you very much!!!

  10. muralidhararao says:

    Hi, thanq for your example.. can we create group connection…

    bd1—>bd2—>bd3
    Note:bd-Bluetooth device.
    bd1 sends the data to bd2 and we need to store that data in db2 local device in the same way bd2 need to send same data to bd3.

  11. kisu says:

    hi tom,while compiling the program iam getting error that “bluesock not installed properly” could you please help me where to keep the bluesock.jar,bluesock.dll in java and the path,class path settings

  12. rossy says:

    hi,
    my project is sending message from pc to mobile via bluetooth.there r three steps
    1.finding the bluetooth mobiles which r around my pc.
    2.select one of them and sening an sms to that
    3.providing same facility to that mobile to again sending sms to me.
    .
    there r only few days to complete my project.
    i am requesting all of u to give me suggestions to complete my project plzz.. .i want the source code .it is almost peak time to complete my project.

    –>i am using net beans IDE 6.0.I can’t now how to add the packages like javax.bluetooth,and some other related to it .plzzz help me to start these primary steps.and i say sorry for my bad english.

    –>with in 3 days i have to show the code of device discovery (pc to mobiles)to my master.otherwise i have to face lot of problems
    my mail id is sweetrossy_20@yahoo.co.in

    thanks in advance.waiting for your valuable reply.

  13. Arun Kumar says:

    Thanks for this great article

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>