2013-06-24 75 views
0

我在寫智能手機和使用藍牙設備的計算機之間進行通信的應用程序。藍牙服務器Android - 客戶端Java Bluecove。 UUID?

我使用Bluecove來管理計算機上的藍牙,以及我的android設備的android API。

但是,當我在調試時,沒有任何反應。我認爲問題在於UUID是錯誤的。我不確定如何讓設備相互識別,以建立連接。

我看了一下這些標籤的一些 「問題」,但我已經試過沒有解決我的問題:

這是我到目前爲止寫的:

  1. 對於壽的Android(服務器)(這是將建立連接的功能)

    公共無效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應用程序慢下來(這是類的構造函數(它是在一個獨立的線程)將管理藍牙連接)

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

    }

有人可以幫我嗎?任何想法將非常感激。我也嘗試使用一些函數來獲取UUID(在android中),例如[fetchUuidsWithSdp] [2](和相關的函數),但eclipse不能識別這些函數(看起來他們不'') t存在於「我的」API中)。

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

回答

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

+0

謝謝!我也讀過這些頁面,但我跳過了他說他的代碼(在Android上)是基於BluetoothChat示例的句子!再次感謝!我很高興 – Bardo91

+1

最後我管理這個問題。正是在這個例子中,JAVA代碼像android一樣充當服務器。但我需要將JAVA代碼編程爲客戶端。我用這個例子:http://www.miniware.net/mobile/articles/viewarticle.php?id=22 – Bardo91

相關問題