2011-02-02 97 views
1

我一直在這裏經歷了幾個線程,並沒有遇到我遇到的問題的答案。安卓藍牙聊天應用程序(設備立即失去連接)

我的設置: 我有一臺Mac電腦,我用作虛擬串行端口與我的android Nexus S電話進行通信。在手機上運行藍牙聊天應用程序,並將其用作客戶端,與我設置的virt通信對話。

最初我嘗試了2個android手機的藍牙聊天應用程序來確認它的工作原理。我可以來回發送文本。

我的使用案例: 我有一個設備讀取RFid標籤並將數據發送到android手機以收集信息。

我現在用我的電腦代表我的設備。

++++++++++++++++++ 確定的問題,

我試圖從我的手機連接到PC,最初我得到一個「連接。 ...「狀態欄更新和15秒左右後,我會得到一個吐司消息,說:」我已連接到電腦「,但在我收到」設備丟失連接「吐司後立即發出。然後狀態欄變爲「未連接」

當我使用調試程序時,它似乎在藍牙聊天應用程序的以下部分失敗。具體地,本線(bytes = mmInStream.read(buffer);

public void run() { 
     Log.i(TAG, "BEGIN mConnectedThread"); 
     byte[] buffer = new byte[1024]; 
     int bytes; 

     // Keep listening to the InputStream while connected 
     while (true) { 
      try { 
       // Read from the InputStream 
       bytes = mmInStream.read(buffer); 

       // Send the obtained bytes to the UI Activity 
       mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer) 
         .sendToTarget(); 
      } catch (IOException e) { 
       Log.e(TAG, "disconnected", e); 
       connectionLost(); 
       break; 
      } 
     } 
    } 

當我看在logcat中,在I/O的例外是 「software caused connection abort」 有關的InputStream讀()。

問題: 這是否與我的虛擬端口沒有設置正確?我有終端和等待接收/dev/tty.Nexus輸入.... 使用屏幕命令@ 9600波特

否則,我想也許輸入流連接到的插座是不可用的。我打印的日誌,它似乎不是NULL。每當我一步一步雖然它死在ConnectThread不在ConnectedThread

下面的代碼部分:具體地,本線(mmSocket.connect();

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

     // 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) { 
      connectionFailed(); 
      // Close the socket 
      try { 
       mmSocket.close(); 
      } catch (IOException e2) { 
       Log.e(TAG, "unable to close() socket during connection failure", e2); 
      } 
      // Start the service over to restart listening mode 
      BluetoothChatService.this.start(); 
      return; 
     } 

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

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

不知插座變量被丟失範圍由於多線程和套接字正在傳遞?

謝謝

回答

0

如何在Mac PC上設置虛擬串行文章。由於您已經嘗試在兩部手機上運行應用程序,並且它正在工作,所以我認爲問題出在PC上。

我發佈了一個關於Android和Java藍牙here的條目。希望它會有所幫助。