服务器异常:找不到btspp的连接实现

时间:2014-08-05 17:46:09

标签: java bluetooth raspberry-pi bluetooth-lowenergy bluecove

我正在使用Raspberry开发蓝牙服务器,bluecove-2.1.0.jar
java版“1.8.0_06”
Java(TM)SE运行时环境(版本1.8.0_06-b23)
Java HotSpot(TM)客户端VM(版本25.6-b23,混合模式)

这里是服务器java代码:

public class BluetoothServer extends Thread {

    UUID uuid = new UUID("0000110100001000800000805F9B34FB", false); // This is a magic number

    String url = "btspp://localhost:" + uuid.toString() + ";name=xxxx";

    Security security = new Security();

    /** Constructor */

    public BluetoothServer() {

    }

    @Override
    public void run() {

        waitForConnection();

    }

    /** Waiting for connection from devices */

    private void waitForConnection() {

        // retrieve the local Bluetooth device object

        LocalDevice local = null;

        StreamConnectionNotifier notifier = null;

        StreamConnection connection = null;

        // setup the server to listen for connection

        try {

            local = LocalDevice.getLocalDevice();           

            local.setDiscoverable(DiscoveryAgent.GIAC); // generally discoverable, discoveryTimeout should be disabled - but isn't.

            notifier = (StreamConnectionNotifier) Connector.open(url);
            // Bluetooth may go undiscoverable after 180sec default discoveryTimeout, so reset it on separate thread every 179sec.
            new Timer().schedule(new TimerTask(){

                @Override
                public void run() {
                    try {
                        LocalDevice.getLocalDevice().setDiscoverable(DiscoveryAgent.GIAC);
                    } catch (BluetoothStateException e) {
                        System.out.println("TimerTask exception: " + e.getMessage());
                        e.printStackTrace();
                    }

                }

            }, 0, 179000);
        } catch (Exception e) {
            System.out.println("Server exception: " + e.getMessage());

            e.printStackTrace();

        }

        // waiting for connection

        while (true) {

            try {

                System.out.println("waiting for connection...");

                connection = notifier.acceptAndOpen();

                Thread processThread = new Thread(new ProcessConnectionThread(
                        connection, this));

                processThread.start();
                System.out.println("processThread.started...");
                // bluetooth may have been set undiscoverable after connection
                local.setDiscoverable(DiscoveryAgent.GIAC);
            } catch (Exception e) {
                System.out.println("Server exception: " + e.getMessage());

                e.printStackTrace();

                 return;

            }

        }

    }
    public static void main(String[] args) {

        System.out.println("Server startup now");
        new BluetoothServer().start();

    }

    public Security getSecurity() {
        return security;
    }

}

当我在命令行上启动服务器时:

sudo java -cp /jars:/jars/bluetoothserver.jar:/jars/bluecove-2.1.0.jar:/jars/pi4j-core.jar com.bluetoothserver.BluetoothServer
我收到这个错误:

BlueCove version 2.1.0 on bluez
Server exception: The connection implementation for btspp cannot be found.
javax.microedition.io.ConnectionNotFoundException: The connection implementation for btspp cannot be found.
    at javax.microedition.io.Connector.open(Connector.java:238)
    at javax.microedition.io.Connector.open(Connector.java:181)
    at javax.microedition.io.Connector.open(Connector.java:162)
    at com.bluetoothserver.BluetoothServer.waitForConnection(BluetoothServer.java:56)
    at com.bluetoothserver.BluetoothServer.run(BluetoothServer.java:32)
waiting for connection...
Server exception: null
java.lang.NullPointerException
    at com.bluetoothserver.BluetoothServer.waitForConnection(BluetoothServer.java:87)
    at com.bluetoothserver.BluetoothServer.run(BluetoothServer.java:32)

有什么想法吗?感谢

1 个答案:

答案 0 :(得分:3)

有同样的问题。 JDK 1.8.0_06在其中的javax.microedition.io.Connector中实现了它的rt.jar。在我的例子中,解决方案是从rt.jar中删除javax.microedition包以使用BlueCove实现。