title: Error in reading data from InputStream in Bluetooth on Android
body: <p>I'm working on an Android app, which uses bluetooth connection to transfer data between my android smartphone and a non-android bluetooth module, using SPP profile. I've used Bluetooth Chat Example from Android Developer site as reference.</p>
  
  <p>I've successfully made two devices connect to each other and sent simple strings from the smart phone to the bluetooth module. But I've got some error in reading data sent back from the module. I've used the following code, which is exactly the same as in Bluetooth Chat Example, to read data from InputStream</p>
  
  <pre><code>while (true) {
      try {
          // Read from the InputStream
          bytes = mmInStream.read(buffer);
          String str = new String(buffer);
          Log.i(TAG, "mmInStream - " + str);
          // 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;
      }
  }
  </code></pre>
  
  <p>When my bluetooth module send a simple string to the phone, that string is not received correctly. It is splited into several pieces in random ways. For example, if I send "1234567890abcdef1234567890abcdef0123456789" for three times to the phone, the Logcat on Eclipse will log these:</p>
  
  <pre><code>mmInstream - 12345678910abcdef��������(continuing null)
  mmInstream - 1��������(continuing null)
  mmInstream - 2345678910abcdef0123456789��������(continuing null)
  </code></pre>
  
  <p>for the first time. In the second and the third time data is transmitted, it is received in a difference pieces:</p>
  
  <pre><code>mmInstream - 1234567891�������(continuing null)
  mmInstream - 0abcdef012�������(continuing null)
  mmInstream - 3456789���������(continuing null)
  
  mmInstream - 1234567891����������������(continuing null)
  mmInstream - 0abcdef0123456789������������(continuing null)
  </code></pre>
  
  <p>I don't know why this happen and how to solve this problem. If data is received in a arbitrary way like this, I can't get necessary data to process. How can I get it in one pieces?</p>
  
  <p>Any help would be appreciated.</p>
  
  <p>Many Thanks.</p>