X

Geertjan's Blog

  • February 13, 2011

BlueTooth on Linux on the NetBeans Platform

Geertjan Wielenga
Product Manager
Small hello world scenario for BlueTooth in a NetBeans Platform application on Linux.

  1. sudo apt-get install libbluetooth-dev

  2. Download these JARs from the bluecove site:
    • bluecove-2.1.0.jar
    • bluecove-gpl-2.1.0.jar

  3. Create a new NetBeans Platform application.

  4. Create a new module and wrap the above two JARs into it (go to the Properties dialog of the module, then to the Libraries panel, then to Wrapped JARs and wrap the above two JARs).

  5. Copy this class into your module, it comes straight from the samples on the bluecove site:
    public class BlueTooth {
    public static final Vector/\*<RemoteDevice>\*/ devicesDiscovered = new Vector();
    public static void main(String[] args) throws IOException, InterruptedException {
    final Object inquiryCompletedEvent = new Object();
    devicesDiscovered.clear();
    DiscoveryListener listener = new DiscoveryListener() {
    public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
    System.out.println("Device " + btDevice.getBluetoothAddress() + " found");
    devicesDiscovered.addElement(btDevice);
    try {
    System.out.println(" name " + btDevice.getFriendlyName(false));
    } catch (IOException cantGetDeviceName) {
    }
    }
    public void inquiryCompleted(int discType) {
    System.out.println("Device Inquiry completed!");
    synchronized(inquiryCompletedEvent){
    inquiryCompletedEvent.notifyAll();
    }
    }
    public void serviceSearchCompleted(int transID, int respCode) {
    }
    public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
    }
    };
    synchronized(inquiryCompletedEvent) {
    boolean started = LocalDevice.getLocalDevice().getDiscoveryAgent().startInquiry(DiscoveryAgent.GIAC, listener);
    if (started) {
    System.out.println("wait for device inquiry to complete...");
    inquiryCompletedEvent.wait();
    System.out.println(devicesDiscovered.size() + " device(s) found");
    }
    }
    }
    }

  6. Use the New File dialog to create a new Module Installer.

  7. Define the Installer like this:
    public class Installer extends ModuleInstall {
    @Override
    public void restored() {
    try {
    BlueTooth.main(null);
    } catch (IOException ex) {
    Exceptions.printStackTrace(ex);
    } catch (InterruptedException ex) {
    Exceptions.printStackTrace(ex);
    }
    }
    }

Now run the application and the Output window of NetBeans IDE will show something like this:

BlueCove version 2.1.0 on bluez
wait for device inquiry to complete...
Device 5C57C81C1E63 found
name Geertjan Wielenga
Device 5C17D3967ADA found
name LG GU230
Device 00145190B6AB found

Join the discussion

Comments ( 4 )
  • Aleix Sunday, February 13, 2011

    Thanks Geertjan !

    The last samurai on client side java !! thanks for all your job and articles promoting netbeans platform (For me the best java IDE by far, I use netbeans since it was Forte for java!) ! But as the base system 'Swing' does not get the attention it deserves from Oracle I will switch to Apache pivot for my daily projects ( And NETBEANS FOR SURE) !

    cheers !


  • Geertjan Wielenga Sunday, February 13, 2011

    Interesting. Pivot doesn't provide anything that the NetBeans Platform provides, not one single thing (e.g., window system, modularity, etc, etc, etc), so good luck.


  • Kovica Sunday, February 13, 2011

    Yes, and NetBeans was called NetBeans always, Forte was just a Sun's "packaging" of NetBeans.


  • Jesse Glick Monday, February 14, 2011

    I doubt you need libbluetooth-dev; probably just libbluetooth would suffice. (You aren't trying to compile against C header files or link in a static library, are you?)


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.