title: How do I dump serial communication as file through Android bluetooth serial RFCOMM
body: <p>My Android app receive serial communication over bluetooth from desktop application. I have no control over what that desktop application is doing.</p>
  
  <p>Using Google BluetoothChat sample code (<a href="https://github.com/googlesamples/android-BluetoothChat" rel="nofollow noreferrer">https://github.com/googlesamples/android-BluetoothChat</a>). I manage to make it working and to some extent I can simply took the bytes coming to my android app and write a file.</p>
  
  <p>However, the code function is something like</p>
  
  <p>1 - Android listen</p>
  
  <p>2 - PC send data, Android accept connection read data and write to file</p>
  
  <p>3 - While reading, Android encounter exception (client close connection?) and close the connection</p>
  
  <p>4 - Back to one and listen for next few bytes</p>
  
  <p>My problem is that I need to know when this process ends to process the file and display some result in the UI.</p>
  
  <p>The code is this one file </p>
  
  <p><a href="https://github.com/googlesamples/android-BluetoothChat/blob/master/Application/src/main/java/com/example/android/bluetoothchat/BluetoothChatService.java" rel="nofollow noreferrer">https://github.com/googlesamples/android-BluetoothChat/blob/master/Application/src/main/java/com/example/android/bluetoothchat/BluetoothChatService.java</a></p>
  
  <p>Specifically</p>
  
  <pre class="lang-java prettyprint-override"><code>    public void run() {
          Log.i(TAG, "BEGIN mConnectedThread");
          byte[] buffer = new byte[1024];
          int bytes;
  
          // Keep listening to the InputStream while connected
          while (mState == STATE_CONNECTED) {
              try {
                  // Read from the InputStream
                  bytes = mmInStream.read(buffer);
  
                  // ==&gt; This is where I wrote to file
                  // ==&gt; Standard Android Java code to write 
                  // ==&gt; to output file stream
  
                  // Send the obtained bytes to the UI Activity
                  // ==&gt; Commented out because this can not be done here
                  // ==&gt; NEED TO BE DONE WHEN ALL DATA IS READ
                  // mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
                  //      .sendToTarget();
              } catch (IOException e) {
                  Log.e(TAG, "disconnected", e);
                  connectionLost();
                  break;
              }
          }
      }
  </code></pre>
  
  <p>Expected there should be a way in Android RFCOMM or Bluetooth Communication to signify that client already finish sending all the communication.</p>
  
  <p>Previously I try to use IoException  as place to signal the UI that reading data from PC is done. That is a disaster because for some reason, exception only happen at the beginning of communication. And I have no idea why my codes still keep writing to file beyond that.</p>
  
  <p>I am going to repeat that I have no control whatsoever on the PC/Desktop application. So please don't suggest anything that involve changing how desktop app operate or send data.</p>
  
  <p>Need to handle this on Android side.</p>