0

I am developing a BluetoothServer using Raspberry, bluecove-2.1.0.jar
java version "1.8.0_06"
Java(TM) SE Runtime Environment (build 1.8.0_06-b23)
Java HotSpot(TM) Client VM (build 25.6-b23, mixed mode)

Here the server java code:

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;
    }

}

When I Start the server on the command line:

sudo java -cp /jars:/jars/bluetoothserver.jar:/jars/bluecove-2.1.0.jar:/jars/pi4j-core.jar com.bluetoothserver.BluetoothServer
I get this error:

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)

Any ideas ? Thanks

4

1 回答 1

3

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

于 2015-02-08T20:51:32.120 回答