2012-02-10 17 views
4

我正在開發基於BluetoothChat exemple的Android藍牙應用程序。我正在啓動藍牙服務器,並在不安全的rfcomm連接上監聽設備(而不是電話)以連接到我的應用程序。即使配對設備也不返回BluetoothServerSocket.accept()

private class AcceptThread extends Thread { 
    // The local server socket 
    private final BluetoothServerSocket mmServerSocket; 

    public AcceptThread(boolean secure) { 
     BluetoothServerSocket tmp = null; 

     // Create a new listening server socket 
     try { 
      tmp = mAdapter.listenUsingInsecureRfcommWithServiceRecord(mServiceName, MY_UUID_INSECURE); 
     } catch (Exception e) { 
      Log.e(TAG, ".AcceptThread # listen() failed", e); 
     } 
     mmServerSocket = tmp; 
    } 

    public void run() { 
     BluetoothSocket socket = null; 

     // Listen to the server socket if we're not connected 
     while (mState != STATE_CONNECTED) { 
      try { 
       // This is a blocking call and will only return on a 
       // successful connection or an exception 
       Log.d(TAG, ".AcceptThread.run # ...accepting server socket conn"); 

       socket = mmServerSocket.accept(); //FIXME: it blocks here 

       Log.d(TAG, ".AcceptThread.run # server socket connection accepted"); 
      } catch (Exception e) { 
       MMLog.e(TAG, ".run # accept() failed: "+e); 
       connectionFailed(); 
       break; 
      } 

      // If a connection was accepted 
      if (socket != null) { 
       synchronized (BluetoothService.this) { 
        switch (mState) { 
        case STATE_LISTEN: 
        case STATE_CONNECTING: 
         // starting the thread where i will receive input 
         // streams from the other device 
         connected(socket, socket.getRemoteDevice()); 
         break; 
        case STATE_NONE: 
        case STATE_CONNECTED: 
         // Either not ready or already connected. Terminate new socket. 
         try { 
          socket.close(); 
         } catch (IOException e) { 
          Log.e(TAG, "Could not close unwanted socket", e); 
         } 
         break; 
        } 
       } 
      } 
     } 

    } 

    public void cancel() { 
     try { 
      if(mmServerSocket != null) { 
       mmServerSocket.close(); 
      } 
     } catch (IOException e) { 
      Log.e(TAG, ".cancel # Could not close server socket: ", e); 
     } 
    } 
} 

我正在使用HTC Desire S,android 2.3.5。設備已配對,但我沒有收到數據,因爲連接在'.accept()'方法中被阻止。它只是繼續等待。

socket = mmServerSocket.accept(); //...and等待

  • 爲何仍等待,如果設備配對?
  • 我如何建立連接,因爲我也試過反射,但仍然沒有結果
  • HTC的藍牙堆棧有問題嗎?有沒有人建立連接也許使用另一個Android手機?
+0

您是否有能力在您嘗試連接的設備上編輯藍牙軟件? – JPM 2012-02-14 00:23:13

+0

您需要在兩臺設備上設置您的應用,之後您會看到他們可以互相溝通 – 2014-01-15 17:40:21

回答

0

我覺得有什麼不對你的代碼,它不應該是socket = tmp.accept();這裏是我必須做出一個socket服務器連接:

BluetoothServerSocket serverSocket = null; 
BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter(); 
try { 
    serverSocket = bta.listenUsingInsecureRfcommWithServiceRecord("BluetoothChatInsecure", UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); 
} catch (IOException e) { e.printStackTrace(); } 

while(!Thread.interrupted()) { 
    try { 
     socket = serverSocket.accept(); 
     if (socket != null) { 
      Log.d("CONNECTED", "Connected bluetooth"); 

     /// do your stuff 
+0

感謝您的重播。現在我沒有設備來測試它,但我會在星期一回復你。 – user975349 2012-02-10 18:17:54

+0

同時,我在原始問題中添加了我測試的完整代碼。 – user975349 2012-02-10 18:28:02

+0

我注意到了這一點,也許它與摩托羅拉的2.3.5的Android的特殊版本和自動配對與createInsecureRfcommSocketToServiceRecord不起作用,所以也許listenUsingInsecureRfcommWithServiceRecord不起作用。 – JPM 2012-02-10 23:08:07

0

機會是,這與做你的其他設備(這發生在我身上)。您的Android已經完成了列出傳入連接的工作。您的特殊設備無法正確連接到您的Android手機的原因有很多:

  • 該設備神祕地切換到其他藍牙配置文件,例如, HDP而不是SPP
  • 該設備以某種方式記憶其內存中的其他Android手機(它最後連接到或類似的東西),並不斷嘗試連接到該手機,但不是你正在使用的那個。

我想你最好的機會是查詢特殊設備的製造商/銷售商的詳細規格和/或軟件/驅動程序來配置/測試它。

相關問題