title: Reading data from bluetooth device in android
body: <p>I am using bluetooth chat in order to connect and recieve data from a bluetooth device.</p>
  
  <p>I use the following code for reading data:</p>
  
  <pre><code>public void run() {
      byte[] buffer = new byte[1024];
      int bytes;
      Log.v("MR", "start listening....");
  
      // Keep listening to the InputStream while connected
      while (true) {
          try {
              // Read from the InputStream
              Log.d("MR", "buffer in try");
  
              bytes = mmInStream.read(buffer);
              Log.d("MR", "input stream :"+(new String(buffer)));
              // Send the obtained bytes to the UI Activity
              mHandler.obtainMessage(Conn.MESSAGE_READ, bytes, -1, buffer).sendToTarget();
              Log.d("MR", "buffer after");
  
          } catch (Exception e) {
              Log.e("MR", "Error :"+e.getMessage());
              //
              connectionLost();
             // break;
          }
  
          Log.d("MR", "buffer after while");
      }
  }
  </code></pre>
  
  <p>The device is sending data all the time without stopping.</p>
  
  <p>With the above code I get the message of:</p>
  
  <pre><code>Log.d("MR", "buffer in try");
  </code></pre>
  
  <p>then it goes to the next line:</p>
  
  <pre><code>bytes=mmInStream.read(buffer);
  </code></pre>
  
  <p>and never returns from that call. I guess this is because it starts reading data from the device and doesn't stop until it disconnects. How can I read a certain amount of bytes at a time?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Unless it stay to the <code>bytes = mmInStream.read(buffer);</code> code due to that it don;t get any data back on from the device?</p>