title: Send and receive string values via bluetooth android
body: <p>I'm developing an app where I need to send 3 seekbar's values to a PCB via bluetooth. I've done all the bluetooth code based on the bluetoothchat example. I first modified it to send a string with these 3 values. But now, I need to do something more dificult and i don't know how to do it.</p>
  
  <p>First of all, in the app i modify the seekbars and then i click on the send button. In the code, I need to set a string for each seekbar's value, because I need to access to the MCU variables and set each variable address, value, CRC etc...</p>
  
  <p>So, I need to know the correct way to do this. Here is the code where i define the send function:</p>
  
  <pre><code>/**
   * SEND THREAD
   */
  /**[Start Thread + Send command + Nº bytes thread + Nº bytes variable + Address + Variable value + CRC]*/
  
  public void sendValues() {
  
      /**Set the seekbars values into a string*/
      send_value1 = Integer.toString(savedProgress1);
      send_value2 = Integer.toString(savedProgress2);
      send_value3 = Integer.toString(savedProgress3);
  
  
  
      String message1 = start_thread+" "+send_command+" "+num_byte_trama1+ " "+num_byte_variable+" "+pos_reg_1+" "+Value+" "+CRC;
      String message2 = start_thread+" "+send_command+" "+num_byte_trama1+ " "+num_byte_variable+" "+pos_reg_2+" "+Value+" "+CRC;
      String message3 = start_thread+" "+send_command+" "+num_byte_trama1+ " "+num_byte_variable+" "+pos_reg_3+" "+Value+" "+CRC;
      String message4 = start_thread+" "+send_command+" "+num_byte_trama2+ " "+num_byte_variable+" "+pos_reg_save_request+" "+Value+" "+CRC;
      String message5 = start_thread+" "+send_command+" "+num_byte_trama2+ " "+num_byte_variable+" "+pos_reg_save_status+" "+Value+" "+CRC;
  
  
  
      /**Check that we're actually connected before trying anything*/
      if (GlobalVar.mTransmission.getState() != GlobalVar.STATE_CONNECTED) {
          Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show();
          return;
      }
  
      /**Get the message bytes and tell the Transmission to write*/
      byte[] send = message.getBytes();
      GlobalVar.mTransmission.write(send);
  
      /**Reset out string buffer to zero*/
      GlobalVar.mOutStringBuffer.setLength(0);
  }
  </code></pre>
  
  <p>There are these few things that I ask you to help me:</p>
  
  <pre><code>1- Need to know how to calculate the CRC
  2- I need to send these 5 strings together when pressing the send button. 
  </code></pre>
  
  <p>In the part where i get the bytes to send, I don't know If the right way to do this would be to add these 5 strings on 1 and send this one (maybe it would be to long if I do this), or to create a function to send these 5 separately but at the same time.</p>
  
  <p>This is the edited code to send each message one by one:</p>
  
  <pre><code>    /**Conversion to decimal of the seekbar's % value*/
      send_int1 = ((savedProgress1 * 20480) / 100) * -1;
      send_int2 = ((savedProgress2 * 20480) / 100) * -1;
      send_int3 = ((savedProgress3 * 20480) / 100) * -1;
  
      /**Conversion to string of the previous values to send in the string message*/
      sendValue1 = Integer.toString(send_int1);
      sendValue2 = Integer.toString(send_int1);
      sendValue3 = Integer.toString(send_int1);
  
  
      String message1 = start_thread+" "+send_command+" "+num_byte_trama1+" "+num_byte_variable+" "+pos_reg_1+" "+sendValue1+" " ;
      String message2 = start_thread+" "+send_command+" "+num_byte_trama1+" "+num_byte_variable+" "+pos_reg_2+" "+sendValue2+" " ;
      String message3 = start_thread+" "+send_command+" "+num_byte_trama1+" "+num_byte_variable+" "+pos_reg_3+" "+sendValue3+" " ;
      String message4 = start_thread+" "+send_command+" "+num_byte_trama2+" "+num_byte_variable+" "+pos_reg_save_request+" " ;
      String message5 = start_thread+" "+send_command+" "+num_byte_trama2+" "+num_byte_variable+" "+pos_reg_save_status+" " ;
  
  
  
      /**Check that we're actually connected before trying anything*/
      if (GlobalVar.mTransmission.getState() != GlobalVar.STATE_CONNECTED) {
          Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show();
          return;
      }
  
      /**Get the message bytes and tell the Transmission to write*/
      byte[] send1 = message1.getBytes();
      GlobalVar.mTransmission.write(send1);
  
      //Wait untill I receive the confirmation from the MCU
  
      byte[] send2 = message2.getBytes();
      GlobalVar.mTransmission.write(send2);
  
      byte[] send3 = message3.getBytes();
      GlobalVar.mTransmission.write(send3);
  
      byte[] send4 = message4.getBytes();
      GlobalVar.mTransmission.write(send4);
  
      byte[] send5 = message5.getBytes();
      GlobalVar.mTransmission.write(send5);
  
  
      /**Reset out string buffer to zero*/
      GlobalVar.mOutStringBuffer.setLength(0);
  }
  </code></pre>