title: Android: Sending data >20 bytes by BLE
body: <p>I am able to send data upto 20 bytes by connecting to an external BLE device. How do I send data greater than 20 bytes. I have read that we have to either fragment the data or split characteristic to required parts. If I assume my data is 32 bytes, could you tell me changes I need to make in my code to get this working? Following are the required snippets from my code:</p>
  
  <pre><code>public boolean send(byte[] data) {
      if (mBluetoothGatt == null || mBluetoothGattService == null) {
          Log.w(TAG, "BluetoothGatt not initialized");
          return false;
      }
  
      BluetoothGattCharacteristic characteristic =
              mBluetoothGattService.getCharacteristic(UUID_SEND);
  
      if (characteristic == null) {
          Log.w(TAG, "Send characteristic not found");
          return false;
      }
  
      characteristic.setValue(data);
      characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
      return mBluetoothGatt.writeCharacteristic(characteristic);
  }
  </code></pre>
  
  <p>This is the code I used for sending the data. The "send" function is used in the following onclick event.</p>
  
  <pre><code>sendValueButton = (Button) findViewById(R.id.sendValue);
      sendValueButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              String text = dataEdit.getText().toString();                           
              yableeService.send(text.getBytes());
          }
      });
  </code></pre>
  
  <p>When the <code>String text</code> is greater than 20 bytes then only the first 20 bytes are received. How to rectify this?</p>
  
  <p>To test sending multiple characteristics I tried this:</p>
  
  <pre><code>sendValueButton = (Button) findViewById(R.id.sendValue);
  sendValueButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          String text = "Test1";                           
          yableeService.send(text.getBytes());
  
          text = "Test2";                           
          yableeService.send(text.getBytes());
  
          text = "Test3";                           
          yableeService.send(text.getBytes());
      }
  });
  </code></pre>
  
  <p>But I only received "Test3" i.e. the last characteristic. What mistake did I commit? I am new to BLE so please ignore any naiveness</p>
  
  <p><strong><em>Edit:</em></strong></p>
  
  <p><strong><em>After accepting answer</em></strong> for anyone who views this later.</p>
  
  <p>There are <strong>two</strong> ways to accomplish this.
  1. Split up your data and write in a loop as the selected answer does.
  2. Split up your data and write using callback i.e. <code>onCharacterisitcWrite()</code>. This will save you from errors if there were any during writing.</p>
  
  <p>But <strong><em>most important</em></strong> between the writes use a <code>Thread.sleep(200)</code> if you are only writing and not waiting for a response from the firmware. This will ensure that all of your data reaches. Without the <code>sleep</code> I was always getting the last packet. If you notice the accepted answer he has also used <code>sleep</code> in between.</p>