title: Message handler gets overwritten by next message
body: <p>Not new to programming, but fairly new to Java and Android.  App is a Bluetooth SPP receive getting short packets once per second.  Occasionally the Bluetooth gives me a portion of a message and then rapidly after gives me the remainder of the message.  When this happens, my receive thread sends the same type of message to my parser, but with different contents.  However the parser receives the "data" from the second message with the "length" of the first message.</p>
  
  <p>Here's my code and I'm wondering what I should do to differentiate the messages, or if I need to check in my receiver thread as to whether or not the message has been handled and sleep for a few milliseconds and try again.  Opinions?  And sorry, I'm learning by the examples I see, I'm sure there are several ways to do what I'm doing here.</p>
  
  <p>The receiver:</p>
  
  <pre><code>protected void receive() {
      byte[] recvBuffer = new byte[128];
      while(inStream != null) {
          try {
              int bytesReceived = inStream.read(recvBuffer);
              System.out.println("BT-In: ");
              String outString = new String(recvBuffer, 0, bytesReceived);
              System.out.println(outString);
              processReceive.obtainMessage(NEW_DEVICE_DATA, bytesReceived, -1, recvBuffer)
                            .sendToTarget();
          }
          catch (IOException e){
              WarningBox("Warning", "Receive over Bluetooth did not work, check connection.");      
              break;
          }
      }
  };
  </code></pre>
  
  <p>The Handler:</p>
  
  <pre><code>Handler processReceive = new Handler() {
  @Override
      public void handleMessage(Message msg) {
          switch(msg.what) {
          default:
              break;
          case NEW_DEVICE_DATA:
              byte[] parseInfo = (byte[])msg.obj;
              String outString = new String(parseInfo, 0, msg.arg1);
              System.out.println("Parse-In: ");
              System.out.println(outString);
              //ParseReceivedData(parseInfo);
              break;
          }
      }
  };
  </code></pre>
  
  <p>For now I'm not parsing the data, but trying to validate that I get everything over the Bluetooth from my remote device and that I'm properly sending that over to the parser which will eventually update my screen fields.</p>
  
  <p>My log might look something like this:</p>
  
  <p>BT-In:
  ABCabc</p>
  
  <p>Parse-In:
  ABCabc</p>
  
  <p>BT-In:
  12</p>
  
  <p>BT-In:
  3456789</p>
  
  <p>Parse-In:
  34</p>
  
  <p>Parse-In:
  3456789</p>
  
  <p>The error there is that I have two Bluetooth receives near each other, say the whole string was "123456789", but I got it in two parts "12" and then "3456789" and I sent those two parts to my parser (main thread), the handler saw two messages, but the first message was accepted properly in size, but improperly in content.</p>