This discussion is archived
3 Replies Latest reply: Sep 3, 2009 3:12 PM by 843810 RSS

Java & Search for bluetooth devices

843810 Newbie
Currently Being Moderated
Hi, I'm trying to detect bluetooth devices with a Java Program but I keep having the same errors namely :

Exception in thread "main" javax.bluetooth.BluetoothStateException: Unable to load
HCIManager. org.javabluetooth.stack.hci.HCIException: BluetoothStack not initalized.

at javax.bluetooth.LocalDevice.<init>(LocalDevice.java:49)
at javax.bluetooth.LocalDevice.getLocalDevice(LocalDevice.java:62)
at BluetoothDeviceDiscovery.main(BluetoothDeviceDiscovery.java:32)

I am using eclipse and the javax.bluetooth package... No errors in the code (like mistakes), only exceptions that prevent building.

This is my test module (sorry about just dropping the code here but I couldn't attach it as a file) :

import java.io.IOException;
import java.util.Vector;
 
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
 
 
/**
* Class that discovers all bluetooth devices in the neighbourhood
* and displays their name and bluetooth address.
*/
public class BluetoothDeviceDiscovery implements DiscoveryListener{
    
    
    //object used for waiting
    private static Object lock=new Object();
    
    //vector containing the devices discovered
    private static Vector vecDevices=new Vector();
    
    
    //main method of the application
    public static void main(String[] args) throws IOException {
        
        //create an instance of this class
        BluetoothDeviceDiscovery bluetoothDeviceDiscovery=new BluetoothDeviceDiscovery();
        
        //display local device address and name
        LocalDevice localDevice = LocalDevice.getLocalDevice();
        System.out.println("Address: "+localDevice.getBluetoothAddress());
        System.out.println("Name: "+localDevice.getFriendlyName());
        
        //find devices
        DiscoveryAgent agent = localDevice.getDiscoveryAgent();
      
        System.out.println("Starting device inquiry...");
        agent.startInquiry(DiscoveryAgent.GIAC, bluetoothDeviceDiscovery);
        
        try {
            synchronized(lock){
                lock.wait();
            }
        } 
        catch (InterruptedException e) {
            e.printStackTrace();
        }
        
        
        System.out.println("Device Inquiry Completed. ");
        
        //print all devices in vecDevices
        int deviceCount=vecDevices.size();
        
        if(deviceCount <= 0){
            System.out.println("No Devices Found .");
        }
        else{
            //print bluetooth device addresses and names in the format [ No. address (name) ]
            System.out.println("Bluetooth Devices: ");
            for (int i = 0; i <deviceCount; i++) {
                RemoteDevice remoteDevice=(RemoteDevice)vecDevices.elementAt(i);
                System.out.println((i+1)+". "+remoteDevice.getBluetoothAddress()+" ("+remoteDevice.getFriendlyName(true)+")");
            }
        }
        
        
    }//end main
 
    //methods of DiscoveryListener
    
    /**
     * This call back method will be called for each discovered bluetooth devices.
     */
    public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
        System.out.println("Device discovered: "+btDevice.getBluetoothAddress());
        //add the device to the vector
        if(!vecDevices.contains(btDevice)){
            vecDevices.addElement(btDevice);
        }
    }
 
    //no need to implement this method since services are not being discovered
    public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
    }
 
    //no need to implement this method since services are not being discovered
    public void serviceSearchCompleted(int transID, int respCode) {
    }
 
    
    /**
     * This callback method will be called when the device discovery is 
     * completed.
     */
    public void inquiryCompleted(int discType) {
        synchronized(lock){
            lock.notify();
        }
        
        switch (discType) {
            case DiscoveryListener.INQUIRY_COMPLETED :
                System.out.println("INQUIRY_COMPLETED");
                break;
                
            case DiscoveryListener.INQUIRY_TERMINATED :
                System.out.println("INQUIRY_TERMINATED");
                break;
                
            case DiscoveryListener.INQUIRY_ERROR :
                System.out.println("INQUIRY_ERROR");
                break;
 
            default :
                System.out.println("Unknown Response Code");
                break;
        }
    }//end method
}//end class 
I could really use some help,I'm quite sure it has to do with the bluetooth stack but I can't figure it out. If you want the complete package with all the sources, just ask me and I'll email them to you. You can also contact me on jelle.vanregenmortel@student.denayer.wenk.be

Hope someone is way smarter then me...

Cheers and thanks in advance!
  • 1. Re: Java & Search for bluetooth devices
    843810 Newbie
    Currently Being Moderated
    Sorry, can't see any JavaHelp code in what you posted.

    Bye.
  • 2. Re: Java & Search for bluetooth devices
    843810 Newbie
    Currently Being Moderated
    Hi Luca-Sanna

    Thank you for the reply, but what do you mean with JavaHelp code? Is my question not clear or can't you see my code? Or can't you see a problem with my code? (sorry I'm just new on the forum).

    I've uploaded my entire project to my server so you guys can download it if you want : http://users.skynet.be/fa101028/Bluetooth.rar

    I develop in Eclipse, if that would matter. The main function is the BluetoothDeviceDiscovery.class and all the other files are libraries (mainly the javax.bluetooth).

    Thank you so much in advance!

    Jelle
  • 3. Re: Java & Search for bluetooth devices
    843810 Newbie
    Currently Being Moderated
    I mean you're posting in the wrong forum: this is not a forum providing help on java related problems, there's a reason why JavaHelp is written in camel case as a single word. It's a java based help system (like the ones you usually get by pressing F1). I'm sure you can find a better place in the sun forums to ask your question.

    Bye.