简体   繁体   English

蓝牙,BlueCove和Raspberry PI

[英]Bluetooth, BlueCove and Raspberry PI

I setup using Java using BlueJ and Bluetooth using Blueman on my RaspberryPI and am using the BlueCove API. 我在RaspberryPI上使用Java使用BlueJ进行设置,并使用Blueman进行了蓝牙设置,并且正在使用BlueCove API。

I took the example RemoteDeviceDiscovery from: 我从以下示例中获取了RemoteDeviceDiscovery示例:

http://bluecove.org/bluecove/apidocs/overview-summary.html#DeviceDiscovery http://bluecove.org/bluecove/apidocs/overview-summary.html#DeviceDiscovery

When I run the example from within BlueJ I get: 当我从BlueJ中运行示例时,我得到:

" wait for device inquiry to complete... Device Inquiry completed! 0 device(s) found " and when I run from the terminal window using: “等待设备查询完成...设备查询完成!找到0个设备”,当我从终端窗口运行时,使用:

pi@raspberrypi ~/java/bluetooth_jar $ /usr/lib/jvm/jdk-8-oracle-arm-vfp-hflt/jre/bin/java -jar bluetooth_jar.jar 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 " “ bluez上的BlueCove版本2.1.1-SNAPSHOT BluetoothStateException异常:javax.bluetooth.BluetoothStateException:蓝牙设备未准备好。[1]不允许操作BlueCove堆栈关闭已完成”

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. 为什么在使用BlueJ时未引发异常,我不明白,但是在命令行中,由于设备未准备好,它似乎无法检测附近的设备。 However, I don't understand this error message since I can send files using Blueman Manager to nearby a Android tablet and Win7 laptop. 但是,我不明白此错误消息,因为我可以使用Blueman Manager将文件发送到附近的Android平板电脑和Win7笔记本电脑。

import java.io.IOException;

import java.util.ArrayList; 导入java.util.ArrayList; import javax.bluetooth.*; 导入javax.bluetooth。*;

/** * Minimal Device Discovery example. / ** *最小设备发现示例。 */ public class RemoteDeviceDiscovery { * /公共类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 } //类RemoteDeviceDiscovery

Discovered that if I run the jar with superuser rights then it works as expected: 发现如果我以超级用户权限运行jar,则可以按预期工作:

pi@raspberrypi ~ sudo /java/bluetooth_jar $ /usr/lib/jvm/jdk-8-oracle-arm-vfp-hflt/jre/bin/java -jar bluetooth_jar.jar 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! bluez上的BlueCove版本2.1.1-SNAPSHOT等待设备查询完成...设备C4850852975B发现名称GMSEED-PC设备查询已完成! 1 device(s) found BlueCove stack shutdown completed 找到1个设备BlueCove堆栈关闭已完成

and if I start BlueJ as superuser from the commandline rather than through the desktop menu item Menu|Programming|BlueJ; 如果我以超级用户身份从命令行而不是通过桌面菜单项Menu | Programming | BlueJ启动BlueJ; ie: 即:

pi@raspberrypi ~ $ sudo bluej pi @ raspberrypi〜$须藤bluej

and then the example app gives the same output. 然后示例应用程序将提供相同的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM