title: Android bluetooth socket data transfer and lifecycle
body: <p>I am currently trying to send some data between two android devices using Bluetooth. I've read plenty of questions regarding bluetooth transfer, sockets, and streams. So far without any luck.
  The connection part is working. I get the device address then open a connection using the following :</p>
  
  <pre><code>BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(myOtherDeviceAdress);
  BluetoothSocket socket = device.createRfcommSocketToServiceRecord(UUID.fromString(myUUID));
  socket.connect();
  </code></pre>
  
  <p>And then try to send some data using the <code>OutputStream</code></p>
  
  <pre><code>OutputStream mmout=tmp.getOutputStream();
  byte[] toSend="Hello World!".getBytes();
  mmout.write(toSend);
  mmout.flush();
  </code></pre>
  
  <p>On the receiving end:</p>
  
  <pre><code>mBluetoothServerSocket = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("ccv_prototype", UUID.fromString(myUUID));
  mBluetoothSocket = mBluetoothServerSocket.accept(3 * 1000);
  InputStream is = mBluetoothSocket.getInputStream();
  BufferedReader r = new BufferedReader(new InputStreamReader(is));
  </code></pre>
  
  <p>And then, different version trying to read the buffer, currently:</p>
  
  <pre><code>int c;
  StringBuilder response = new StringBuilder();
  try {
      while ((c = r.read()) != -1) {
          //Since c is an integer, cast it to a char. If it isn't -1, it will be in the correct range of char.
          response.append((char) c);
      }
  } catch (IOException e) {
      e.printStackTrace();
  }
  String result = response.toString();
  Log.d("MyTag", "Received String: " + result);
  </code></pre>
  
  <p>My issue here is that if I don't close the <code>OutputStream</code>, the receiving end never receives the EOF, but if I add <code>mmout.close();</code>, it closes before it even had time to read the message I wanted to send. So far, my only idea is to send a specific token as an EOF but this doesn't sound right.</p>
  
  <p>What did I miss ?
  Any help appreciated.</p>