title: How to receive a complete arduino string in android
body: <p>I am doing an app for my arduino sensor I wanted to know how to actually obtain the data from arduino to android studio via bluetooth because in my arduino I put <code>Serial.println(&quot;occupied&quot;)</code> but when receive in android it somehow mixed with some numbers/bytes like &quot;o2cuppied&quot; or else it will receive seperately in logcat. I don't know what is wrong with it.</p>
  <p>Arduino Code:</p>
  <pre><code>void setup() {
    serial1.begin(9600); //for the bluetooth module
  }
  void loop() {
   
    //send data to Bluetooth module//
    if (dist[0] &lt; dist_threshold) {
      serial1.print(&quot;Occupied\n&quot;);
     
    }
   if (dist[1] &lt; dist_threshold) {
      
      serial1.print(&quot;Occupied2\n&quot;);
      
    }
  </code></pre>
  <p>Android</p>
  <pre><code> @Override
      protected void onCreate(Bundle savedInstanceState) {
   h = new Handler() {
              public void handleMessage(android.os.Message msg) {
  
                  switch (msg.what) {
                      case RECEIVE_MESSAGE:                             // if receive massage
                          byte[] readBuf = (byte[]) msg.obj;
                          String strIncom = new String(readBuf, 0, msg.arg1);   // create string from bytes array
                                                       // and clear
                              txtArduino.setText(&quot;Data from Arduino: &quot; + strIncom);
  
                              break;
                  }
              }
          };
  }
  
  private class ConnectedThread extends Thread {
          private final BluetoothSocket bluetoothSocket;
          private final InputStream mmInStream;
          private final OutputStream mmOutStream;
  
          public ConnectedThread(BluetoothSocket socket) {
              InputStream tmpIn = null;
              OutputStream tmpOut = null;
              bluetoothSocket = socket;
  
              // Get the input and output streams, using temp objects because
              // member streams are final
              try {
                  tmpIn = socket.getInputStream();
                  tmpOut = socket.getOutputStream();
              } catch (IOException e) {
                  e.printStackTrace();
              }
  
              mmInStream = tmpIn;
              mmOutStream = tmpOut;
          }
  
          public void run() {
              byte[] buffer = new byte[1024];  // buffer store for the stream
              int bytes; // bytes returned from read()
  
              // Keep listening to the InputStream until an exception occurs
              while (true) {
                  try {
                      // Read from the InputStream
                      bytes = mmInStream.read(buffer);        // Get number of bytes and message in &quot;buffer&quot;
                      String incomingMessage = new String(buffer, 0, bytes);
                      Log.d(TAG, &quot;InputStream: &quot; + incomingMessage);
                      h.obtainMessage(RECEIVE_MESSAGE, bytes, -1, buffer).sendToTarget(); // Send to message queue Handler
                     
                  } catch (IOException e) {
                      Log.e(TAG, &quot;write: Error reading Input Stream. &quot; + e.getMessage() );
                      break;
                  }
              }
          }
      }
  </code></pre>
  <p>Logcat</p>
  <blockquote>
  <p>2019-11-27 12:28:13.505 12733-12893/com.example.fyp D/MainActivity: InputStream: O</p>
  <p>2019-11-27 12:28:13.508 12733-12893/com.example.fyp D/MainActivity: InputStream: ccupied</p>
  </blockquote>