2010-11-18 91 views
1

我正在使用來自android網站的藍牙聊天示例應用程序。我想發送特定格式的長信息(在代碼中提到),並在另一端接收並在我的手機上顯示(HTC Desire)。我將進一步解析這個消息,提取一些東西並將其用於我的應用程序。藍牙的問題/問題HTC Desire的SPP配置文件會影響藍牙聊天應用程序嗎?

我無法接收上述消息中的所有字符(少數字符會被隨機省略)。是因爲藍牙套接字的InputStream「read」方法出現問題,或者藍牙SPP配置文件在HTC Desire或其他原因中存在錯誤和問題?我已經嘗試過InputStream是一個字節緩衝區,一個字符緩衝區,然後從緩衝區內容中創建一個字符串(使用普通的新字符串,StringBuilder和StringBuffer)。但不知何故,我無法在手機上以所需格式顯示整個消息。

任何幫助/建議,非常感謝。

謝謝您的時間

乾杯, 馬杜南丹

/** * 此線程運行與遠程設備的連接過程。 *它處理所有傳入和傳出的傳輸。 */

private class ConnectedThread extends Thread { 
    private final BluetoothSocket mmSocket; 
    private final InputStream mmInStream; 
    private final OutputStream mmOutStream; 

    public ConnectedThread(BluetoothSocket socket) { 
     Log.d(TAG, "create ConnectedThread"); 
     mmSocket = socket; 
     InputStream tmpIn = null; 
     OutputStream tmpOut = null; 

     // Get the BluetoothSocket input and output streams 
     try { 
      tmpIn = socket.getInputStream(); 
      tmpOut = socket.getOutputStream(); 
     } catch (IOException e) { 
      Log.e(TAG, "temp sockets not created", e); 
     } 

     mmInStream = tmpIn; 
     mmOutStream = tmpOut; 
    } 

    public void run() { 
     Log.i(TAG, "BEGIN mConnectedThread"); 
     char[] buffer = new char[1024]; 
     int bytes; 
     //Writer writer = new StringWriter(); 

     // Keep listening to the InputStream while connected 
     while (true) { 
      try { 
       // Read from the InputStream 
      Reader reader = new BufferedReader(
         new InputStreamReader(mmInStream, "UTF-8")); 
       //int n; 
       while ((bytes = reader.read(buffer)) != -1) 
       //writer.write(buffer, 0, bytes); 
       //String str = writer.toString(); 

       // 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; 
      } 
     } 
    } 

case MESSAGE_READ:    
     char[] readBuf = (char[]) msg.obj;    
      // construct a string from the valid bytes in the buffer 
     //StringBuffer sb = new StringBuffer(""); 
     String Incoming = new String (readBuf, 0, msg.arg1); 
     StringBuilder sb = new StringBuilder("");    
      String readMessage = sb.append(Incoming).toString();    
     //String readMessage = new String(sb.append(Incoming)); 
      mConversationArrayAdapter.add(mConnectedDeviceName+": " + readMessage); 
      mTitle.setText(readMessage); 
      //String readMessage = new String(sb); 
      //Make sure there is a received message. Extract it's name and value 
      //int msgLen = readMessage.length(); 
      //if(msgLen!= 0) 

      //Message format: <MSG><N>xxx<!N><V>yyy<!V><!MSG> (xxx-name of the //message, yyy-value of the message) 
      if (readMessage.matches("<MSG><N>.*<!N><V>.*<!V><!MSG>"))     
      extractData(readMessage); 
      //else mTitle.setText(R.string.floor_it); 
      sb.setLength(0); 

回答

0

我發現從計算器我碰巧另一篇文章的解決問題的方法爲:Data Transmisison error using SPP over Bluetooth on Android解決方法是不字節緩衝區傳遞給主類和然後構造字符串。相反,在接收字節緩衝區並傳遞字符串的相同類中構造字符串。它適用於我這種方式。

但是現在我又遇到了一個小問題。我通過Windows Hypertermial與藍牙聊天應用程序進行通信。當我發送像這樣的消息<MSG><N>msgName<!N><V>msgValue<!V><!MSG>並在手機上接收它時,我收到所有字符,但它們顯示在多行中。我想比較上述格式的收到消息,然後提取msgName和msgValue以供我的應用程序進一步使用。由於收到的消息以多行顯示,所以比較失敗,但我以正確的格式發送消息。

任何想法爲什麼發生這種情況?感謝您對我的問題提出的任何建議和時間。

乾杯,

馬杜