2016-09-20 17 views
-2

我想讀通過藍牙連接接收bytes並將其轉換爲String字符串不顯示,如果「 n」是內部

監視器輸入流

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(MainActivity.MESSAGE_READ, bytes, -1, buffer) 
          .sendToTarget(); 
       } catch (IOException e) { 
        Log.e(TAG, "disconnected", e); 
        connectionLost(); 
        // Start the service over to restart listening mode 
        BluetoothChatService.this.start(); 
        break; 
       } 
      } 
     } 

MainActivity當我收到時,如果有多行,日誌語句似乎不會被調用。如果它僅僅是一個單一的線,它的罰款

case MESSAGE_READ: 
        byte[] readBuf = (byte[]) msg.obj; 
        String readMessage = new String(readBuf, 0, msg.arg1); 
        Log.d(TAG, "Incoming message: " + readMessage); // does not get called when there's a linebreak. 

當我嘗試檢查,如果字符串中包含的某些信息,因此,它不會被調用即使我收到的字符串中包含它。

例如,如果我收到readMessage = "Obstacle String\nStart of 2nd line" 下面的語句不會被調用

if (readMessage.contains("Obstacle String")) 

但是,當我嘗試將readMessage設置一些TextView,它能夠顯示的字符串給出正確。

+3

你用調試器來檢查字符串包含你認爲它包含? – immibis

+0

去調試,它實際上讀取'「障礙字符串\ r \ n第二行開始」實際上包含所需的字符串條件進入我的'if'循環。那爲什麼不這樣做? –

+0

'當我試圖檢查字符串是否包含某些信息'如何? – EJP

回答

0

出於某種原因,Android的logcat的似乎並不處理\r

所以,我修剪的字符串作爲這樣

readMessage = readMessage.replaceAll("\r", ""); 
相關問題