title: How to read continuously connected bluetooth device?
body: <p>I am trying to connect my App to the bluetooth device and after that i am doing the functionality Read and Write. I am able to do connectivity and Write command to device. But i am not able to do Read functionality.</p>
  
  <p>The "Read" functionality is like, as soon as device socket will get connect to the App, it should send the device information continuously. But i my case, read() method is calling but every time i am getting the length on InputStream in 0. </p>
  
  <p>Below is the code what i am writing. Please check it where i am going wrong and please help me.</p>
  
  <pre><code>public class ConnectionThread implements Runnable {
  
      private static final String CLASSTAG = ConnectionThread.class.getSimpleName();
      public static BluetoothSocket mSocket;
  
      private InputStream inStream;
      private OutputStream outStream;
      private boolean canceled = false;
      private Context mContext;
  
      public ConnectionThread(Context context, BluetoothDevice device) {
          this.mContext = context;
          try {
              mSocket = device.createRfcommSocketToServiceRecord(UUID.fromString(Constants.mUUID));
          } catch (IOException e) {
              Log.e(CLASSTAG, "createRfcommSocketToServiceRecord", e);
          }
      }
  
      @SuppressLint("NewApi")
      @Override
      public void run() {
  
          // Cancel discovery because it will slow down the connection
          if(BluetoothAdapter.getDefaultAdapter().isDiscovering()) {
              BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
          }
  
          try {
              // Connect the device through the socket. This will block
              // until it succeeds or throws an exception
              mSocket.connect();
          } catch (IOException e) {
              Log.e(CLASSTAG, "connect failed", e);
              // Unable to connect; close the socket and get out
              disconnect();
              return;
          }
  
          // Get the input and output streams, using temp objects because
          // member streams are final
          try {
              inStream = mSocket.getInputStream();
              outStream = mSocket.getOutputStream();
          } catch (IOException e) {
              Log.e(CLASSTAG, "IO streams init failed", e);
              disconnect();
              return;
          }
  
          while (!canceled) {
              read();
              try {
                  Thread.sleep(200);
              } catch (InterruptedException e) {
                  Log.e(CLASSTAG, "sleep failed", e);
              }
          }
      }
  
      @SuppressLint("NewApi")
      public void applyCommand(String configCommand) throws IOException {
          byte[] bytes = null;
          bytes = new BigInteger(configCommand,16).toByteArray();
          if (mSocket.isConnected()) {
              //write(bytes);
              outStream.write(bytes);
              Log.d(CLASSTAG, "Command apply: " + bytes + " (" + configCommand + ")");
          }
          Toast.makeText(mContext, mContext.getString(R.string.cmd_updated), Toast.LENGTH_LONG).show();
      }
  
      private void read() {
  
          byte[] buffer = new byte[128];
  
          // Keep listening to the InputStream while connected
          try {
              // Read from the InputStream
              if (inStream.available() &gt; 0) {
                  int bytes = inStream.read(buffer);
                  if (bytes &gt; 0) {
                      Log.d(CLASSTAG, "Response: " + new String(buffer, 0, bytes));
                  }
              }
              // Send the obtained bytes to the UI Activity
              // mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();
          } catch (IOException e) {
              Log.e(CLASSTAG, "reading from input stream failed", e);
              disconnect();
          }
      }
  
      public void disconnect() {
          try {
              if (outStream != null) {
                  outStream.close();
              }
              if (inStream != null) {
                  inStream.close();
              }
              mSocket.close();
              canceled = true;
          } catch (IOException e) {
              Log.e(CLASSTAG, "close socket", e);
          }
      }
  }
  </code></pre>