title: Error InputStream in Bluetooth on Android
body: <p>I am writing an application that needs to exchange data with an Arduino HC-06 Bluetooth.  Here is the data exchange method:</p>
  
  <pre><code>void ListenForBluetoothData()
  {
      final Handler handler = new Handler();
      final byte delimiter = 10; //This is the ASCII code for a newline character
  
      stopWorker = false;
      readBufferPosition = 0;
      readBuffer = new byte[1024];
      workerThread = new Thread(new Runnable()
      {
          public void run()
          {
              while(!Thread.currentThread().isInterrupted() &amp;&amp; !stopWorker) {
                  try {
                      if(BTinStream!=null)
                      {
                      int bytesAvailable = BTinStream.available();
                      if (bytesAvailable &gt; 0) {
                          byte[] packetBytes = new byte[bytesAvailable];
                          BTinStream.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()
                                      {
                                          myLabel1.setText(data);
                                      }
                                  });
                              } else {
                                  readBuffer[readBufferPosition++] = b;
                              }
                          }
                      }
                    }else {
                          continue;
                      }
                  }
                  catch (IOException ex)
                  {
                      stopWorker = true;
                  }
              }
          }
      });
  
      workerThread.start();
  }
  </code></pre>
  
  <p>The main error that I received is:</p>
  
  <pre><code>06-19 11:21:23.011 17542-17910/com.example.farok.bluetoothcommunicationarduino E/AndroidRuntime: FATAL EXCEPTION: Thread-41375
  Process: com.example.farok.bluetoothcommunicationarduino, PID: 17542
  java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
      at android.os.Handler.&lt;init&gt;(Handler.java:200)
      at android.os.Handler.&lt;init&gt;(Handler.java:114)
      at com.example.farok.bluetoothcommunicationarduino.MainActivity.ListenForBluetoothData(MainActivity.java:179)
      at com.example.farok.bluetoothcommunicationarduino.MainActivity$1.run(MainActivity.java:69)
  </code></pre>
  
  <p>Please help, I've tried many solutions and they have not worked.</p>