title: Receive string via Bluetooth android (without line feed or Carriage Return)
body: <p>There are a lot of questions on stack overflow on how to receive data (string) from a Bluetooth device but I have never seen a question on how to receive data (string) without checking for LF or CR.</p>
  
  <p>This code works perfectly when the device send a LF at the end of the String:</p>
  
  <pre><code>final Handler handler = new Handler();
      final byte delimiter = 10; //ASCII code for Line Feed
  
      stopWorker = false;
      readBufferPosition = 0;
      readBuffer = new byte[1024];
  
      try {
          inStream = btSocket.getInputStream();
      } catch (IOException e) {
          Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
      }
  
  
  
      Thread workerThread = new Thread(new Runnable() {
          @Override
          public void run() {
              while (!Thread.currentThread().isInterrupted() &amp;&amp; !stopWorker) {
                  try {
                      int bytesAvailable = inStream.available();
                      if (bytesAvailable &gt; 0) {
                          byte[] packetBytes = new byte[bytesAvailable];
                          inStream.read(packetBytes);
                          for (int i = 0; i &lt; bytesAvailable; i++) {
                              byte b = packetBytes[i];
                              if (b == delimiter) {
                                  byte[] encodedBytes = new byte[readBufferPosition];
                                  System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
                                  final String data = new String(encodedBytes, "US-ASCII");
                                  readBufferPosition = 0;
                                  handler.post(new Runnable() {
                                      public void run() {
                                          //test
                                          textview1.setText(data);
                                      }
                                  });
                              } else {
                                  readBuffer[readBufferPosition++] = b;
                              }
                          }
                      }
                  } catch (IOException ex) {
                      stopWorker = true;
                  }
              }
          }
      });
      workerThread.start();
  </code></pre>
  
  <p>But the device that sends the string does not send a Line feed neither a CR at the end. I have tried many things (also the Bluetooth guide from Google (<a href="http://developer.android.com/guide/topics/connectivity/bluetooth.html" rel="nofollow">http://developer.android.com/guide/topics/connectivity/bluetooth.html</a>)). I want to receive all the data that is send to the app (and for testing display the received String in a textview).</p>
  
  <p>Thanks in advance,
  Michielvk</p>