javax.bluetooth.BluetoothStateException:BlueCove com.intel.bluetooth.BluetoothStackBlueZ不可用

时间:2018-06-05 15:39:42

标签: java bluetooth bluecove

我正在处理 Java BlueCove 以查找蓝牙设备。

我已经创建了一个 Maven 项目,并且我添加了BlueCove依赖项:

<dependency>
    <groupId>net.sf.bluecove</groupId>
    <artifactId>bluecove</artifactId>
    <version>2.1.0</version>
</dependency>

我正在使用 Ubuntu ,我已经安装了以下软件包:

sudo apt-get install bluez libbluetooth-dev
sudo apt-get build-dep bluez-tools

我已经复制并粘贴了一个示例from here,基本上如下:

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;

public class RemoteDeviceDiscovery {

    public Vector getDevices() {
    /* Create Vector variable */
    final Vector devicesDiscovered = new Vector();
    try {
        final Object inquiryCompletedEvent = new Object();
        /* Clear Vector variable */
        devicesDiscovered.clear();

        /* Create an object of DiscoveryListener */
        DiscoveryListener listener = new DiscoveryListener() {

            public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
                /* Get devices paired with system or in range(Without Pair) */
                devicesDiscovered.addElement(btDevice);
            }

            public void inquiryCompleted(int discType) {
                /* Notify thread when inquiry completed */
                synchronized (inquiryCompletedEvent) {
                    inquiryCompletedEvent.notifyAll();
                }
            }

            /* To find service on bluetooth */
            public void serviceSearchCompleted(int transID, int respCode) {
            }

            /* To find service on bluetooth */
            public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
            }
        };

        synchronized (inquiryCompletedEvent) {
            /* Start device discovery */
            boolean started = LocalDevice.getLocalDevice().getDiscoveryAgent().startInquiry(DiscoveryAgent.GIAC, listener);
            if (started) {
                System.out.println("wait for device inquiry to complete...");
                inquiryCompletedEvent.wait();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    /* Return list of devices */
    return devicesDiscovered;
    }
}

但是当我运行程序时,我收到错误:

javax.bluetooth.BluetoothStateException: BlueCove com.intel.bluetooth.BluetoothStackBlueZ not available

在这一行:

boolean started = LocalDevice.getLocalDevice().getDiscoveryAgent().startInquiry(DiscoveryAgent.GIAC, listener);

我已经看到similar question的其他答案没有成功。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

好的,我知道我来晚了,但是我在搜索相同的问题。 这在ubuntu 18.04上对我有用:

sudo apt install blueman libbluetooth* bluez*
sudo systemctl start bluetooth

可以检查蓝牙状态之后:

sudo systemctl status bluetooth

希望您能看到这个,如果可以,这对您有帮助:)