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.

  • http://hafisme.net kopok

    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?

  • anne

    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

  • Shamit Jain

    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

  • rotit

    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

  • ahlem

    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)

    • Saadmostafa89

      guys download bluecove-2.1.1.jar

  • Tosin

    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

  • Ricardo

    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

  • zubair

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

  • Alberto

    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!!!

  • muralidhararao

    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.

  • kisu

    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

  • rossy

    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.

  • Arun Kumar

    Thanks for this great article

    • areef

      Hi,
      Whenever i run this application i am getting String index out of bounds exception -2 .Please help me

  • Daniel

    Hello,
    Where can I find the API javax.comm para Windows?
    Or tell a solution to solve this problem?
    Thank you.
    Great job.
    Best regards.

  • http://twitter.com/tombrito Tom Brito

    Thank you for this great hints! My project is startint to work now!

  • Ales

    When I started my application I had a problem:
    javax.bluetooth.BluetoothStateException: Unable to load HCIManager. org.javabluetooth.stack.hci.HCIException: BluetoothStack not initalized.

    I’m using Bluecove…

    How can I solve this problem?

  • Areefnadaf

    I want to know whether this code works fro multiple connections? I want to make pc as a server and multiple phone as a clients. Please help me in this

  • KK Paliwal

    I have same problem which Ales have.
    When I started application the following error comes.

    ERROR: Error in main: Unable to load HCIManager. org.javabluetooth.stack.hci.HCIException: BluetoothStack not initalized.
    debug:
    BUILD SUCCESSFUL (total time: 1 minute 18 seconds)

    I’m using Bluecove…

    How can I solve this problem?

  • KK Paliwal

    I have same problem which Ales have.
    When I started application the following error comes.

    LOG: Start discovery
    LOG: Discovery end
    ERROR: Error in main: Unable to load HCIManager. org.javabluetooth.stack.hci.HCIException: BluetoothStack not initalized.
    debug:
    BUILD SUCCESSFUL (total time: 1 minute 18 seconds)

  • Meena131090

     give the full step by step guide to me.because i don’t know ho implement the program and how to compile the java bluetooth.please help me……………….n 

    email: meena131090@gmail:disqus .com
    thanks & Regards
    meena