title: Bluetooth data transfer between two Android devices
body: <p>I have been following <a href="http://developer.android.com/guide/topics/connectivity/bluetooth.html" rel="noreferrer">this Android guide</a> for Bluetooth communication </p>
  
  <p>To explain exactly what I want to do, when the two devices are paired, two different activities open up on each device (server and client) where on the server activity I have different buttons, and on the client activity there is just a textview.
  I want to be able to press a button on the server device and display it on the client.</p>
  
  <p>I have managed to establish a connection between the two devices, but now I want to send data which I have not been able to do.</p>
  
  <p>They give this code for data transfer:</p>
  
  <pre><code>private class ConnectedThread extends Thread {
  private final BluetoothSocket mmSocket;
  private final InputStream mmInStream;
  private final OutputStream mmOutStream;
  
  public ConnectedThread(BluetoothSocket socket) {
      mmSocket = socket;
      InputStream tmpIn = null;
      OutputStream tmpOut = null;
  
      // Get the input and output streams, using temp objects because
      // member streams are final
      try {
          tmpIn = socket.getInputStream();
          tmpOut = socket.getOutputStream();
      } catch (IOException e) { }
  
      mmInStream = tmpIn;
      mmOutStream = tmpOut;
  }
  
  public void run() {
      byte[] buffer = new byte[1024];  // buffer store for the stream
      int bytes; // bytes returned from read()
  
      // Keep listening to the InputStream until an exception occurs
      while (true) {
          try {
              // Read from the InputStream
              bytes = mmInStream.read(buffer);
              // Send the obtained bytes to the UI activity
              mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
                      .sendToTarget();
          } catch (IOException e) {
              break;
          }
      }
  }
  
  /* Call this from the main activity to send data to the remote device */
  public void write(byte[] bytes) {
      try {
          mmOutStream.write(bytes);
      } catch (IOException e) { }
  }
  
  /* Call this from the main activity to shutdown the connection */
  public void cancel() {
      try {
          mmSocket.close();
      } catch (IOException e) { }
  }
  }
  </code></pre>
  
  <p>But this line generates an error</p>
  
  <pre><code>// Send the obtained bytes to the UI activity
              mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();
  </code></pre>
  
  <p>And is not explained in the guide. I don't know what the mHandler is or does.</p>
  
  <p>Apart from the error, I don't even really understand where to put this code. Should it be in the second activities (server and client) that I open or in the main? If in the Server activity, should it be in the onClick method for all the buttons with a different byte code to send for each button? And in this code, how do we distinguish who is sending and who is receiving?</p>