0

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

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

但是,当我调试时,什么也没有发生。我认为问题在于 UUID 错误。我不确定如何让设备相互识别,以建立连接。

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

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

  1. 对于 tho android (Server) (这是建立连接的函数)

    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 应用程序(这是管理蓝牙连接的类的构造函数(它在一个独立的线程上))

    公共模块蓝牙(){

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

    }

有人可以帮我吗?任何想法将不胜感激。

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

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

4

1 回答 1

0

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

于 2013-06-25T11:48:19.940 回答