gmseed
Posts: 50
Joined: Sat Aug 29, 2015 7:16 pm

Bluetooth and Java on RPi

Thu Sep 10, 2015 5:37 pm

Hi

What's the best setup for using Java and Bluetooth?

I downloaded the BlueCove 2.1.0jar from http://bluecove.org/ but got my doubts about ii as it seems several years old.

I copied&pasted their RemoteDeviceDiscovery example into BlueJ:

http://bluecove.org/bluecove/apidocs/ov ... eDiscovery

and get the runtime javax.bluetooth.BluetoothStateException exception:

BlueCove com.intel.bluetooth.BluetoothStackBlueZ not available

Also, looking inside the jar I see dlls, and hence got my doubts about this working on RPi.

I'd like to hear what others use.

User avatar
Douglas6
Posts: 5197
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, USA

Re: Bluetooth and Java on RPi

Thu Sep 10, 2015 6:38 pm

I haven't used it myself, but there's lot's of info on Bluecove that a search of these forums (or Google) should find.

gmseed
Posts: 50
Joined: Sat Aug 29, 2015 7:16 pm

Re: Bluetooth and Java on RPi

Fri Sep 11, 2015 9:16 am

I stumbled on this article:

http://lukealderton.co.uk/blog/posts/20 ... pbian.aspx

and downloading his prebuilt jars and .so in the provided link [http://lukealderton.co.uk/media/1002/bl ... -02-15.zip] and then extracting and adding to the BlueJ lib folder [/usr/share/bluej/userlib] then code now runs and the runtime exception goes away.

I now have the problem that the provided BlueCove example doesn't work and doesn't find any nearby paired Bluetooth devices!! Onto the next hurdle...

gmseed
Posts: 50
Joined: Sat Aug 29, 2015 7:16 pm

Re: Bluetooth and Java on RPi

Fri Sep 11, 2015 10:50 am

Using the example from:

http://bluecove.org/bluecove/apidocs/ov ... eDiscovery

When I run the example from within BlueJ I get:

"
wait for device inquiry to complete...
Device Inquiry completed!
0 device(s) found "

and when I run from the terminal window using:

pi@raspberrypi ~/java/bluetooth_jar $ /usr/lib/jvm/jdk-8-oracle-arm-vfp-hflt/jre/bin/java -jar bluetooth_jar.jar

I get:

" BlueCove version 2.1.1-SNAPSHOT on bluez BluetoothStateException exception: javax.bluetooth.BluetoothStateException: Bluetooth Device is not ready. [1] Operation not permitted BlueCove stack shutdown completed "

Why the exception is not thrown when using BlueJ I don't understand but from the commandline it appears unable to detect nearby devices because the device is not ready. However, I don't understand this error message since I can send files using Blueman Manager to nearby a Android tablet and Win7 laptop.

The example code I'm using is pasted below:

Code: Select all

import java.io.IOException;
import java.util.ArrayList;
import javax.bluetooth.*;

/**
 * Minimal Device Discovery example.
 */
public class RemoteDeviceDiscovery {

    protected ArrayList<RemoteDevice> devicesDiscovered = new ArrayList();
    protected final Object inquiryCompletedEvent = new Object();

    public RemoteDeviceDiscovery()
    {
        DiscoveryListener listener = new MyDiscoveryListener();

        synchronized(inquiryCompletedEvent) {
            try
            {
                LocalDevice local = LocalDevice.getLocalDevice();
                local.setDiscoverable(DiscoveryAgent.GIAC);
                DiscoveryAgent discoveryAgent = local.getDiscoveryAgent();
                // note: GIAC: The inquiry access code for General/Unlimited Inquiry Access Code (GIAC).
                boolean startedInquiry = discoveryAgent.startInquiry(DiscoveryAgent.GIAC, listener);
                if (startedInquiry)
                {
                    System.out.println("wait for device inquiry to complete...");
                    inquiryCompletedEvent.wait();
                    System.out.println(devicesDiscovered.size() +  " device(s) found");
                }
            }
            catch ( BluetoothStateException e)
            {
                System.out.println("BluetoothStateException exception: " + e);
            }
            catch (InterruptedException e)
            {
                System.out.println("InterruptedException exception: " + e);
            }
        }        
    }

    public static void main(String[] args)
    {

        RemoteDeviceDiscovery rmd = new RemoteDeviceDiscovery();
    }

    class MyDiscoveryListener implements DiscoveryListener
    {
        public MyDiscoveryListener()
        {
        }

        public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
            System.out.println("Device " + btDevice.getBluetoothAddress() + " found");
            devicesDiscovered.add(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) {
        }    
    } // class MyDiscoveryListener
} // class RemoteDeviceDiscovery

gmseed
Posts: 50
Joined: Sat Aug 29, 2015 7:16 pm

Re: Bluetooth and Java on RPi

Fri Sep 11, 2015 12:55 pm

Discovered that if I run the jar with superuser rights then it works as expected:

pi@raspberrypi ~ sudo /java/bluetooth_jar $ /usr/lib/jvm/jdk-8-oracle-arm-vfp-hflt/jre/bin/java -jar bluetooth_jar.jar

with output:

BlueCove version 2.1.1-SNAPSHOT on bluez
wait for device inquiry to complete...
Device C4850852975B found
name GMSEED-PC
Device Inquiry completed!
1 device(s) found
BlueCove stack shutdown completed

and if I start BlueJ as superuser from the commandline rather than through the desktop menu item Menu|Programming|BlueJ; ie:

pi@raspberrypi ~ $ sudo bluej

and then the example app gives the same output.

Yippie!!

Onto the next set of problems!!...

gmseed
Posts: 50
Joined: Sat Aug 29, 2015 7:16 pm

Re: Bluetooth and Java on RPi

Fri Sep 11, 2015 1:08 pm

And the next problem!

It detects the nearby Win7 laptop but not the Android tablet.

Weird...

gmseed
Posts: 50
Joined: Sat Aug 29, 2015 7:16 pm

Re: Bluetooth and Java on RPi

Sat Sep 12, 2015 12:24 pm

Tried running the EchoServer example at:

http://www.miniware.net/mobile/articles ... .php?id=22

which imports:

import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnectionNotifier;
import javax.microedition.io.StreamConnection;

which cause the runtime library exception:

Setting device to be discoverable...
BlueCove version 2.1.1-SNAPSHOT on bluez
Start advertising service...
Exception Occured: javax.bluetooth.BluetoothStateException: can not load native library libbluetooth.so

I tried the tip at:

http://lukealderton.co.uk/blog/posts/20 ... pbian.aspx

of:

pi@raspberrypi ~ $ sudo apt-get install libbluetooth
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package libbluetooth

which doesn't seem to work.

Thus, not sure how I link my BlueJ project to the libbluetooth.so.

User avatar
Douglas6
Posts: 5197
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, USA

Re: Bluetooth and Java on RPi

Sat Sep 12, 2015 1:38 pm

I don't use Bluecove, but you might want to

Code: Select all

sudo apt-get install libbluetooth-dev

gmseed
Posts: 50
Joined: Sat Aug 29, 2015 7:16 pm

Re: Bluetooth and Java on RPi

Mon Sep 14, 2015 12:44 pm

I already tried that too and no luck.

I'm coming to the conclusion that Bluetooth on the RPi is for Linux and Java gurus. I'm still not getting expected behaviour from the Blueman Manager and finding the BlueCove API a little out of date and with an effectively dead forum not much use. All of the 4 examples on the BlueCove website do not work on the RPi, and as I say, would probably require a guru to figure out why.

I've now implemented what I wanted using server and client sockets with a swing client GUI for transferring files, and seems to work fine. A pity I couldn't get bluetooth working, but found another workaround.

I'll keep an eye out on other bluetooth articles but I do think that bluetooth is a basic service that is well supported and easy to setup and use on Windows and Android, but incredibly dificult on RPi. If it does work and and can be used with Java development such as BlueJ, Pi4J and BlueCove then a really detailed article would be most useful.

Graham

Colateral
Posts: 6
Joined: Tue Apr 12, 2016 10:54 am

Re: Bluetooth and Java on RPi

Wed Jul 20, 2016 7:56 am

You have to be careful with Bluecove and what you are doing using this library. It is under GPL license, so you need to publish your code.

Return to “Java”