title: how to read from the InputStream of a bluetooth on Android
body: <p>I am trying to test <a href="https://stackoverflow.com/questions/15343369/sending-a-string-via-bluetooth-from-a-pc-as-client-to-a-mobile-as-server">this bluetooth communication example between a PC and an Android phone</a>. My SPP client is exactly the one from there and it works fine. I am new to Android and I didn't want to make it run in a separate thread because I don't know how, so I just did everything in the <code>onCreate()</code> method. If this is not the best way, feel free to point me to a better way, but this is not my main problem. </p>
  
  <p>The problem is I wanted to display the text received via bluetooth on a <code>textView</code> and I don't know how to read from <code>InputStream</code>. When the code is left like that, it displays something like <code>java.io.DataInputStream@41b0cb68</code>
  I tried it like <a href="https://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string">here</a> it didn't display anything, also I don't know what encoding is being used.</p>
  
  <p>here's my android app's code:</p>
  
  <pre><code>import android.app.Activity;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import java.io.DataInputStream;
  import java.io.DataOutputStream;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.util.UUID;
  
  import android.app.Activity;
  import android.bluetooth.*;
  import android.util.Log;
  import android.widget.RelativeLayout;
  import android.widget.TextView;
  
  public class MainActivity extends Activity {
  
  //based on java.util.UUID
  private static UUID MY_UUID = UUID.fromString("446118f0-8b1e-11e2-9e96-0800200c9a66");
  
  // The local server socket
  private BluetoothServerSocket mmServerSocket;
  
  // based on android.bluetooth.BluetoothAdapter
  private BluetoothAdapter mAdapter;
  private BluetoothDevice remoteDevice;
  TextView text;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      text = (TextView) findViewById(R.id.textView_Text);
  
      BluetoothSocket socket = null;
      mAdapter = BluetoothAdapter.getDefaultAdapter();        
  
      // Listen to the server socket if we're not connected
     // while (true) {
  
          try {
              // Create a new listening server socket
              Log.d((String) this.getTitle(), ".....Initializing RFCOMM SERVER....");
  
              // MY_UUID is the UUID you want to use for communication
              mmServerSocket = mAdapter.listenUsingRfcommWithServiceRecord("MyService",    MY_UUID);
              //mmServerSocket =  mAdapter.listenUsingInsecureRfcommWithServiceRecord(NAME, MY_UUID); // you can also try  using In Secure connection...
  
              // This is a blocking call and will only return on a
              // successful connection or an exception
              socket = mmServerSocket.accept();
  
          } catch (Exception e) {
  
          }
  
          try {
              Log.d((String) this.getTitle(), "Closing Server Socket.....");
              mmServerSocket.close();
  
              InputStream tmpIn = null;
              OutputStream tmpOut = null;
  
              // Get the BluetoothSocket input and output streams
  
              tmpIn = socket.getInputStream();
              tmpOut = socket.getOutputStream();
  
              DataInputStream mmInStream = new DataInputStream(tmpIn);
              DataOutputStream mmOutStream = new DataOutputStream(tmpOut);
  
              // here you can use the Input Stream to take the string from the client  whoever is connecting
              //similarly use the output stream to send the data to the client
  
  
              text.setText(mmInStream.toString());
          } catch (Exception e) {
              //catch your exception here
          }
     // }
  }
  }
  </code></pre>
  
  <p>I commented out the <code>while(true)</code> loop because I think it was making my app crash when <code>onPause()</code> was being called. I know this is not the best implementation but I really want to read from the bluetooth I feel like I am very close :), other aspects will be dealt with afterwards (like working with threads and so on).</p>