title: Sending data to connected Bluetooth Low Energy device fails
body: <p>I am developing android application which requires sending and receiving data to ble devices. I have managed to do the scanning,pairing and connecting activity but failed to do the sending data.I am calling the gatt.discoverServices() as soon as the connection event is successful. Below is the part of my code where I try to send the data.</p>
  
  <pre><code>public class BluetoothLeUart extends BluetoothGattCallback implements BluetoothAdapter.LeScanCallback {
  
  // UUIDs for UART service and associated characteristics.
  public static UUID UART_UUID = UUID.fromString("6E400001-B5A3-F393-E0A9-E50E24DCCA9E");
  public static UUID TX_UUID   = UUID.fromString("6E400002-B5A3-F393-E0A9-E50E24DCCA9E");
  public static UUID RX_UUID   = UUID.fromString("6E400003-B5A3-F393-E0A9-E50E24DCCA9E");
  
  // Internal UART state.
  private Context context;
  private WeakHashMap&lt;Callback, Object&gt; callbacks;
  private BluetoothAdapter adapter;
  private BluetoothGatt gatt;
  private BluetoothGattCharacteristic tx;
  private BluetoothGattCharacteristic rx;
  private boolean connectFirst;
  private boolean writeInProgress; // Flag to indicate a write is currently in progress
  
  // Queues for characteristic read (synchronous)
  private Queue&lt;BluetoothGattCharacteristic&gt; readQueue;
  
  public BluetoothLeUart(Context context) {
      super();
      this.context = context;
      this.callbacks = new WeakHashMap&lt;Callback, Object&gt;();
      this.adapter = BluetoothAdapter.getDefaultAdapter();
      this.gatt = null;
      this.tx = null;
      this.rx = null;
      this.writeInProgress = false;
      this.readQueue = new ConcurrentLinkedQueue&lt;BluetoothGattCharacteristic&gt;();
  }    
  // Send data to connected UART device.
  public void send(byte[] data) {
      // Update TX characteristic value.  Note the setValue overload that takes a byte array must be used.
      tx.setValue(data);
      writeInProgress = true; // Set the write in progress flag
      gatt.writeCharacteristic(tx);
      while (writeInProgress); // Wait for the flag to clear in onCharacteristicWrite
  
  }
  
  //my onServicesDiscovered function
  @Override
  public void onServicesDiscovered(BluetoothGatt gatt, int status) {
      super.onServicesDiscovered(gatt, status);
      // Notify connection failure if service discovery failed.
      if (status == BluetoothGatt.GATT_FAILURE) {
          connectFailure();
          return;
      }
  
      // Save reference to each UART characteristic.
      tx = gatt.getService(UART_UUID).getCharacteristic(TX_UUID);
      rx = gatt.getService(UART_UUID).getCharacteristic(RX_UUID);
  
  
  
      // Setup notifications on RX characteristic changes (i.e. data received).
      // First call setCharacteristicNotification to enable notification.
      if (!gatt.setCharacteristicNotification(rx, true)) {
          // Stop if the characteristic notification setup failed.
          connectFailure();
          return;
      }
      // Next update the RX characteristic's client descriptor to enable notifications.
      BluetoothGattDescriptor desc = rx.getDescriptor(CLIENT_UUID);
      if (desc == null) {
          // Stop if the RX characteristic has no client descriptor.
          connectFailure();
          return;
      }
      desc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
      if (!gatt.writeDescriptor(desc)) {
          // Stop if the client descriptor could not be written.
          connectFailure();
          return;
      }
      // Notify of connection completion.
      notifyOnConnected(this);
  }
  </code></pre>