title: How to separate data being received over bluetooth in android
body: <p>I have modified the BluetoothChat example code to connect to a generic bluetooth transceiver which I have connected to the UART on a TI MSP430 development board. I have established communication and can send and receive a single string and display the value in a TextView. Below is the C code that I'm using to send the 1-3 digit value for pressure, temp1 and temp 2. It is fairly straightforward, and I is working as designed.</p>
  
  <pre><code>  for(int i = 0; i &lt;= 2; i++)     // send pressure value
    {
      UCA0TXBUF = pressureString[i];
      while(!(IFG2 &amp; UCA0TXIFG));
    }
    for(int i = 0; i &lt;= 2; i++)     // send temp1 value
    {
      UCA0TXBUF = tempOneString[i];
      while(!(IFG2 &amp; UCA0TXIFG));
    }
    for(int i = 0; i &lt;= 2; i++)     // send temp2 value
    {
      UCA0TXBUF = tempTwoString[i];
      while(!(IFG2 &amp; UCA0TXIFG));
    }
  </code></pre>
  
  <p>Now I want to send multiple pieces of data to the android device and have them displayed according to their data type in a separate TextView for each value. For right now I am measuring two temperature sensors and a pressure sensor. I have sent all of the data to the android device with no problems, but all the values just overwrite each other in the TextView so that only the last string sent is displayed. </p>
  
  <p>This is the portion of code that runs while connected to a remote device:</p>
  
  <pre><code>/**
   * This thread runs during a connection with a remote device.
   * It handles all incoming and outgoing transmissions.
   */
  private class ConnectedThread extends Thread {
      private final BluetoothSocket mmSocket;
      private final InputStream mmInStream;
      private final OutputStream mmOutStream;
  
      public ConnectedThread(BluetoothSocket socket) {
          Log.d(TAG, "create ConnectedThread");
          mmSocket = socket;
          InputStream tmpIn = null;
          OutputStream tmpOut = null;
  
          // Get the BluetoothSocket input and output streams
          try {
              tmpIn = socket.getInputStream();
              tmpOut = socket.getOutputStream();
          } catch (IOException e) {
              Log.e(TAG, "temp sockets not created", e);
          }
  
          mmInStream = tmpIn;
          mmOutStream = tmpOut;
      }
  
      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);
  
                  // 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>
  
  <p>This is the code that reads the message and displays it in the TextView:</p>
  
  <pre><code>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);
                  mTextView.setText(readMessage);                         //added by AMJ in attempt to display variable in textview
                  break;
  </code></pre>
  
  <p>I can't seem to figure out how to program the android application to be able to tell the difference between strings, so that when I receive the Temp1 string it goes to the Temp1TextView, and Temp2 string goes to Temp2TextView, etc. Should I add a special character as the first bit sent from the MSP430, and reference that bit in Android to identify where it should go? Just a thought.</p>
  
  <p>Any help is much appreciated. </p>
  
  <p><strong>EDIT:</strong> I figured I could try and convert the int to a string, then use the tokenizer to separate it, and then convert it back to an int. However, the application is now crashing when it receives data over bluetooth. Here is the code that I'm using to convert it. Any idea why it may be crashing?</p>
  
  <pre><code>bytes = mmInStream.read(buffer);
  
                  byteString = String.valueOf(bytes);
  
                  StringTokenizer tokens = new StringTokenizer(byteString, ":");
                  String first = tokens.nextToken();      // this will contain exhaust temp
                  String second = tokens.nextToken();     // this will contain damper position
  
                  separatebytes1 = Integer.valueOf(first);
                      separatebytes2 = Integer.valueOf(second);
  
                  // Read from the InputStream
                //  bytes = mmInStream.read(buffer);
  
                  // Send the obtained bytes to the UI Activity
             //     mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
             //             .sendToTarget();
  
                  mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, separatebytes1, -1, buffer)
                  .sendToTarget();
  </code></pre>
  
  <p>This is the logcat from the crash:</p>
  
  <pre><code>W/dalvikvm(24850): threadid=11: thread exiting with uncaught exception (group=0x
  411ae300)
  E/AndroidRuntime(24850): FATAL EXCEPTION: Thread-1286
  E/AndroidRuntime(24850): java.util.NoSuchElementException
  E/AndroidRuntime(24850):        at java.util.StringTokenizer.nextToken(StringTok
  enizer.java:208)
  E/AndroidRuntime(24850):        at com.example.android.BluetoothChat.BluetoothCh
  atService$ConnectedThread.run(BluetoothChatService.java:411)
  W/ActivityManager(  270):   Force finishing activity com.example.android.Bluetoo
  thChat/.BluetoothChat
  </code></pre>