2012-01-04 83 views
1

我正在使用BluetoothChat示例(3.1)代碼示例來測試兩個Samsung Galaxy Tabs之間的通信。我知道這已被問了很多次,但我仍然困惑,當我這樣做:嘗試連接藍牙設備時拒絕連接

BluetoothSocket mmSocket 
... 
mmSocket.connect(); 

這產生了一個拒絕連接。我已經多次嘗試在程序的外部和內部配對並取消配對,但沒有任何結果。有沒有人有任何想法?

更新(從答案補充):

的代碼在這兩個客戶端&服務器是一樣的。這些實際上是兩個三星Galaxy Tabs(3.1)。試圖連接到遠程設備時發生錯誤。

 public ConnectThread(BluetoothDevice device, boolean secure) { 
     mmDevice = device; 
     BluetoothSocket tmp = null; 
     mSocketType = secure ? "Secure" : "Insecure"; 

     // Get a BluetoothSocket for a connection with the 
     // given BluetoothDevice 
     try { 
      if (secure) { 
       //tmp = device.createRfcommSocketToServiceRecord(
         //MY_UUID_SECURE); 
//     tmp = device.createRfcommSocketToServiceRecord(
//       UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); 
       Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}); 
       tmp = (BluetoothSocket) m.invoke(device, 1);      

      } else {      
        Method m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] {int.class}); 
        tmp = (BluetoothSocket)m.invoke(device, UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));     

      } 
     } catch (Exception e) { 
      Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e); 
     } 
     mmSocket = tmp; 
    } 

    public void run() { 
     Log.i(TAG, "BEGIN mConnectThread SocketType:" + mSocketType); 
     setName("ConnectThread" + mSocketType); 

     // Always cancel discovery because it will slow down a connection 
     mAdapter.cancelDiscovery(); 

     // Make a connection to the BluetoothSocket 
     try { 
      // This is a blocking call and will only return on a 
      // successful connection or an exception 
      mmSocket.connect(); 
     } catch (IOException e) { 
      // Close the socket 
      try { 
       Log.e("Ali", "Error interacting with remote device. Here is the cause: "+ e.getMessage()); 
       mmSocket.close(); 
      } catch (IOException e2) { 
       Log.e(TAG, "unable to close() " + mSocketType + 
         " socket during connection failure", e2); 
      } 
      connectionFailed(); 
      return; 
     } 

     // Reset the ConnectThread because we're done 
     synchronized (BluetoothChatService.this) { 
      mConnectThread = null; 
     } 

     // Start the connected thread 
     connected(mmSocket, mmDevice, mSocketType); 
    } 

聽音部分如下:最後在這裏

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

    public AcceptThread(boolean secure) { 
     BluetoothServerSocket tmp = null; 
     mSocketType = secure ? "Secure":"Insecure"; 

     // Create a new listening server socket 
     try { 
      if (secure) { 
        tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE,UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); 
      } else { 

       tmp = mAdapter.listenUsingInsecureRfcommWithServiceRecord(
         NAME_INSECURE, UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));      
      } 
     } catch (IOException e) { 
      Log.e(TAG, "Socket Type: " + mSocketType + "listen() failed", e); 
     } 
     mmServerSocket = tmp; 
    } 

    public void run() { 
     if (D) Log.d(TAG, "Socket Type: " + mSocketType + 
       "BEGIN mAcceptThread" + this); 
     setName("AcceptThread" + mSocketType); 

     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 
       socket = mmServerSocket.accept(); 
      } catch (IOException e) { 
       Log.e("Ali", "Error while connecting to device because: "+ e.getStackTrace()); 
       break; 
      } 

      // If a connection was accepted 
      if (socket != null) { 
       synchronized (BluetoothChatService.this) { 
        switch (mState) { 
        case STATE_LISTEN: 
        case STATE_CONNECTING: 
         // Situation normal. Start the connected thread. 
         connected(socket, socket.getRemoteDevice(), 
           mSocketType); 
         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; 
        } 
       } 
      } 
     } 
     if (D) Log.i(TAG, "END mAcceptThread, socket Type: " + mSocketType); 

    } 

你有logcat的:

  • 十月一日至五日:34:07.749 :E/BluetoothChat(22495):+++ ON CREATE +++ 01-05 012 E + BluetoothChat(22599):++ ON START ++ 01-05
  • 10:37:41.109:E/BluetoothChat(22599):++ ON START ++ 01-05
  • 10:37:50.009:d/BluetoothChat(22599):onActivityResult -1 01-05
  • 10:37:50.009:d/BluetoothChatService(22599):連接到:
  • 60:D0:A9: 85:AE:6A 01-05 10:37:50.009:D/BluetoothChatService(22599):
  • setState()1 - > 2 01-05 10:37:50.009:E/BluetoothChat(22599):+ ON
  • RESUME + 01-05 10:37:50.029:I/Bluetooth ChatService(22599):BEGIN
  • mConnectThread SocketType:安全十月1日至5日:37:50.029:
  • I/BluetoothChat(22599):MESSAGE_STATE_CHANGE:2月1日至五日10:37:55.169:
  • E /阿里(22599):錯誤與遠程設備交互。原因如下:Host is down 01-05 10:37:55.169:
  • D/BluetoothChatService(22599):start 01-05 10:37:55.169:
  • D/BluetoothChatService(22599):setState() 2 - > 1月1日至5日10:37:55.189:
  • I/BluetoothChat(22599):MESSAGE_STATE_CHANGE:1月1日至5日10:37:55.219:
  • d/dalvikvm(22599):GC_CONCURRENT釋放171K,4- %免費6522K/6791K,暫停2ms + 3ms 01-05 10:38:02.749:
  • W/PhoneWindow(22599):無法獲取音頻管理器01-05 10:38:04.969:
  • E/BluetoothChat (22599): - 暫停 - 01-05 10:38:04.989:
  • I/ApplicationPackageManager(22599):cscCountry不是德文:TPH 01-05 10:38:07.059:
  • D/DeviceListActivity(22599):doDiscovery()01-05 10:38:10。(22599):連接到:F0:08:F1:5E:51:67 01 - - 10: 05 10:38:10.609:
  • D/BluetoothChatService(22599):setState()1 - > 2 01-05 10:38:10.609:
  • E/BluetoothChat(22599):+ ON RESUME + 01-05 10:38:10.609:
  • I/BluetoothChatService(22599):BEGIN mConnectThread SocketType:安全十月1日至5日:38:10.629:
  • I/BluetoothChat(22599):MESSAGE_STATE_CHANGE:2月1日至五日10時38: 12.089:
  • E/BluetoothChat(22599): - 暫停 - 01-05 10:38:12.329:
  • D /剪貼板(22599):在開始輸入時隱藏剪貼板對話框:由別人完成...! 10月1日至5日:38:12.339:
  • W/IInputConnectionWrapper(22599):showStatusIcon上無活性InputConnection 10月1日至5日:38:19.589:
  • E/BluetoothChat(22599):+ ON RESUME + 10月1日至5日:38:20.799:
  • E/Ali(22599):錯誤與遠程設備交互。這裏是原因:連接被拒絕10月1日至五日:38:20.799:
  • d/BluetoothChatService(22599):開始10月1日至五日:38:20.799:
  • d/BluetoothChatService(22599):的setState( )2 - > 1月1日至5日10:38:20.799:
  • I/BluetoothChat(22599):MESSAGE_STATE_CHANGE:1月1日至5日10:39:23.489:
  • E/BluetoothChat(22599): - ON PAUSE -
+0

請發佈相關代碼 - 服務器端和客戶端。還記錄了錯誤.. – 2012-01-04 16:59:55

回答

0

有被拒絕不同的原因: -the錯誤RFCOMM端口 - 黑色名單 - 其他設備的mac地址錯誤

+0

那麼,解決方案是什麼。我已經通過手動輸入mac地址測試了mac地址,並且我知道mac地址不是問題。我該如何本地化這個問題? – 2012-01-04 07:28:42

+0

我甚至用反射方法進行了測試,但沒有結果。 – 2012-01-04 10:32:15

+0

我可以將兩臺設備配對,但設備無法連接。請幫助 – 2012-01-04 11:27:00