title: Bluetooth Chat - Pass the complete incoming message as string
body: <p>I am using Bluetooth Chat sample app from Android website. I want to pass the complete incoming message, (message format included in the code below as this website is parsing the message for me which is my job in the code as well, to a function which removes the tags and extracts message name and value and uses it for further action. The <code>readMessage</code> string (in the switch case Message_Read) sends selected characters and omits few special characters. Following is the code from Bluetooth Chat app (from Android website).</p>
  
  <p>I am not able to receive the complete message in the format which I have mentioned in the code. It gets displayed in multiple lines and many characters get deleted. Any suggestion why this is happening?</p>
  
  <pre><code>// The Handler that gets information back from the BluetoothChatService
  
  private final Handler mHandler = new Handler() {
      @Override
      public void handleMessage(Message msg) {
          switch (msg.what) {
          case MESSAGE_STATE_CHANGE:
              if(D) Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
              switch (msg.arg1) {
              case BluetoothChatService.STATE_CONNECTED:
                  mTitle.setText(R.string.title_connected_to);
                  mTitle.append(mConnectedDeviceName);
                  mConversationArrayAdapter.clear();
                  break;
              case BluetoothChatService.STATE_CONNECTING:
                  mTitle.setText(R.string.title_connecting);
                  break;
              case BluetoothChatService.STATE_LISTEN:
              case BluetoothChatService.STATE_NONE:
                  mTitle.setText(R.string.title_not_connected);
                  break;
              }
              break;
          case MESSAGE_WRITE:
              byte[] writeBuf = (byte[]) msg.obj;
              // construct a string from the buffer
              String writeMessage = new String(writeBuf);
              mConversationArrayAdapter.add("Me:  " + writeMessage);
              break;
          case MESSAGE_READ:
              byte[] readBuf = (byte[]) msg.obj;
              // construct a string from the valid bytes in the buffer
              String readMessage = new String(readBuf, 0, msg.arg1);
              mConversationArrayAdapter.add(mConnectedDeviceName+":  " + readMessage);                 
  
              //Make sure there is a received message. Extract it's name and value
              //int msgLen = readMessage.length();
              //if( msgLen!= 0)
              // Message format is &lt;MSG&gt;&lt;N&gt;shiftDirection&lt;!N&gt;&lt;V&gt;1&lt;!V&gt;&lt;!MSG&gt; 
              if (readBuf.equals("&lt;MSG&gt;&lt;N&gt;.*&lt;!N&gt;&lt;V&gt;.*&lt;!V&gt;&lt;!MSG&gt;"))  
               extractData(readMessage); 
              else mTitle.setText(R.string.floor_it);
  
              break;
          case MESSAGE_DEVICE_NAME:
              // save the connected device's name
              mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);
              Toast.makeText(getApplicationContext(), "Connected to "
                             + mConnectedDeviceName, Toast.LENGTH_SHORT).show();
              break;
          case MESSAGE_TOAST:
              Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST),
                             Toast.LENGTH_SHORT).show();
              break;
          }
      }
  };
  </code></pre>
  
  <hr>
  
  <p>The part of the code which reads the incoming stream of bytes into a buffer and passes the buffer object for display. </p>
  
  <pre><code>    public void run() {
          Log.i(TAG, "BEGIN mConnectedThread");
          byte[] buffer = new byte[1024];
          int bytes;
  
          // Keep listening to the InputStream while connected
          while (true) {
              try {
                  // Read from the InputStream
                  bytes = mmInStream.read(buffer);
                  //String Incoming = new String(buffer);
                  //Pass "Incoming" instead of "buffer"
                  // Send the obtained bytes to the UI Activity
                  mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
                          .sendToTarget();
              } catch (IOException e) {
                  Log.e(TAG, "disconnected", e);
                  connectionLost();
                  break;
              }
          }
      }
  </code></pre>