蓝牙服务器Android - 客户端Java Bluecove。 UUID?

时间:2013-06-24 23:03:46

标签: java android bluetooth bluecove

我正在编写一个应用程序,使用蓝牙设备在我的智能手机和计算机之间进行通信。

我正在使用Bluecove管理计算机上的蓝牙,以及我的Android设备的android API。

然而,当我调试时,没有任何反应。我认为问题在于UUID是错误的。我不确定如何让设备相互识别,以建立连接。

我已经阅读了有关这些标签的其他一些“问题”,但我尝试的并没有解决我的问题:

这是我到目前为止所写的内容:

  1. 对于android(服务器)(这是将建立连接的函数)

    public void connectSocket(){     blueAdapter.cancelDiscovery(); //取消发现,因为它会降低连接速度

    final BluetoothServerSocket serverSocket;
    BluetoothServerSocket sSocket= null;
    try{
        sSocket = blueAdapter.listenUsingRfcommWithServiceRecord("BluetoothJANE", MY_UUID);
    }catch(IOException e){}
    serverSocket = sSocket;
    
    Thread acceptThread = new Thread(new Runnable() {
    
        @Override
        public void run() {
            BluetoothSocket socket = null;
            while(true){
                try{
                    socket = serverSocket.accept();
                }catch(IOException e){
                    break;
                }
                if(socket != null){
                    try{
                        iStream = socket.getInputStream();
                        oStream = socket.getOutputStream();
                    } catch(IOException e){}
                }
            }
        }
    });
    acceptThread.start();
    

    }

  2. 对于PC上的Java应用程序(这是该类的构造函数(它在一个独立的线程上),它将管理蓝牙连接)

    public ModuleBluetooth(){

    StreamConnectionNotifier notifier = null;
    StreamConnection connection = null;
    
    try {
        blueDevice = LocalDevice.getLocalDevice();
        blueDevice.setDiscoverable(DiscoveryAgent.GIAC);
    
        String url = "btspp://localhost:" + MY_UUID.toString()
                + ";name=RemoteBluetooth";
        notifier = (StreamConnectionNotifier) Connector.open(url);
    
    } catch (BluetoothStateException e) {
        System.out
                .println("ModuleBluetooth: Error getting the bluetooth device");
    } catch (IOException e) {
    }
    System.out.println("waiting for connection...");
    try {
        connection = notifier.acceptAndOpen();
        System.out.println("Conenction created");
    } catch (IOException e) {
        System.out.println("Can not create the connection");
    }
    

    }

  3. 有人可以帮帮我吗?任何想法都会非常感激。

    我也尝试使用一些函数来获取UUID(在android中),如[fetchUuidsWithSdp] [2](以及相关函数)但是eclipse不能识别那些函数(似乎它们没有存在于“我的”API中。

    http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#fetchUuidsWithSdp%28%29

1 个答案:

答案 0 :(得分:0)

试试这个例子,http://luugiathuy.com/2011/02/android-java-bluetooth/。我也遇到了与UUID相关的问题,在本例中,将UUID转换为 00001101-0000-1000-8000-00805F9B34FB 开箱即用。请看这个链接:http://www.avetana-gmbh.de/avetana-gmbh/produkte/doc/javax/bluetooth/UUID.html