Welcome, Guest. Please login or register.

Login with username, password and session length
September 07, 2010, 04:12:17 AM
Pages: [1]
  Print  
Topic: Java Bluetooth J2SE  (Read 1278 times)
« on: February 23, 2009, 11:36:35 AM »
mark305
Newbie

View Profile
*
Karma: +0/-0
Posts: 1



Hello,

I am trying to write a program in J2SE which will use Bluetooth and act as a server to wait for connections, and then send a file to any device that connects.

I am using ubuntu linux, and I am using the NetBeans IDE.

I am having a lot of trouble trying to create this program, and after hours of researching the bluetooth API's for linux, and trying all of them (AvetanaBT, jBlueZ, BlueZ, BlueCove) I can not seem to get any basic connectivity with my bluetooth dongle.

I haven't even got as far as listening for devices or discovering bluetooth devices.

The example code I am trying to run is this:
Code:
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

And when I run this program I get the following error:
Code:
Exception in thread "main" javax.bluetooth.BluetoothStateException: java.lang.NoClassDefFoundError: com/sun/kvem/jsr082/bluetooth/DiscoveryAgentImpl
        at javax.bluetooth.LocalDevice.getLocalDevice(LocalDevice.java:100)
        at BluetoothDeviceDiscovery.main(BluetoothDeviceDiscovery.java:66)
Java Result: 1

I would really appreciate if someone could help me out, by pointing me in the right direction or giving me some advice on what API's to use, or some good books which explain how to use Bluetooth API with J2SE.

Thank you.
Logged
« Reply #1 on: April 06, 2009, 02:53:47 PM »
cookdav
Newbie

View Profile
*
Karma: +0/-0
Posts: 5



Mark -

On Linux, I have problems too using any Java-based code examples from this website,
and stuff that I wrote that works perfectly fine on Vista.

[I am using BlueCove...bluecove-2.1.0.jar on Windows-Vista.]

On Linux, you need one more JAR, namely, the '-gpl' jar for bluecove, according to:
http://code.google.com/p/bluecove/downloads/list
As I recall, I was getting YOUR symptom, before I learned of the 2nd needed JAR for
Linux.

That said, you'll probably NEXT see another problem.  My code COMPILES just fine on Linux,
and when I execute it, I get no actual error msgs or exceptions, but remote-device detection reports that no remote devices are paired or present, but of course, they are. (Yet, NON-Java apps DO detect the paired devices) Very strange!

For points of comparison, I'm using it on a different Debian-family-based Linux distro
called MEPIS (ver 8.0), which is a very new release of the MEPIS distro, based on Debian(Lenny) repositories.
So, the native-stack for bluetooth there is the default 'BlueZ'.

As, a comparison test (since I'm a bit of a distro-junkie), I also setup everything on
another Debian-based Linux distro named 'sidux', and see EXACTLY the same problem.

So, before I waste any more time on this, I would LOVE to hear from someone who
DOES HAVE JSR-82-JAVA-BLUETOOTH WORKING on some Linux-distro!  If so, please
tell us exactly which so-called 'java-stack' (e.g. BlueCove or whatever else).

[As an aside, the native bluetooth stack (NON-JAVA-based) stuff works fine on Linux,
and I just LOVE the great service-discovery tool called 'sdptool' that is included among
the bluetooth-tools.  If anyone has a pointer to 'sdptool' re-built for the Windows/XP/VISTA
platform, that would be very helpful.  (If not, I'm gonna tackle trying to rebuild sdptool
on Vista in another few weeks myself.)]

So, my bottom-line recommendation to you would be to (temporarily at least) do
your java-based-bluetooth development on Vista (if you have that avail to you).
[My laptop, like all my machines, is setup for dual-boot between various Linux
distros and either Win-XP or Win-Vista.  Vista-machines are now VERY EASY to
install Linux onto for dual-boot, by first using the Vista-builtin disk-tool to
'shrink' the existing C-drive/partition to make space so that Linux partitioners
can reformat the re-claimed space from 'shrink' operation into whatever new
partition types that are desired.]
Cheers...hope this helps...
« Last Edit: April 07, 2009, 08:16:52 AM by cookdav » Logged
 
Pages: [1]
  Print  
 
Jump to:  

Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC