Reputation: 852
I setup using Java using BlueJ and Bluetooth using Blueman on my RaspberryPI and am using the BlueCove API.
I took the example RemoteDeviceDiscovery from:
http://bluecove.org/bluecove/apidocs/overview-summary.html#DeviceDiscovery
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.
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
Upvotes: 1
Views: 2330
Reputation: 852
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.
Upvotes: 1