@Override
 public void onDescriptorWrite(
     BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
   super.onDescriptorWrite(gatt, descriptor, status);
   Log.d(TAG, "onDescriptorWrite");
   switch (status) {
     case BluetoothGatt.GATT_SUCCESS:
       getBluetoothLe()
           .getBleEventCallback()
           .onCharacteristicNotificationEnabled(gatt, descriptor);
       break;
     case BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION:
     case BluetoothGatt.GATT_INSUFFICIENT_ENCRYPTION:
       // bonds automatically
       Log.w(
           TAG,
           "Failed to write descriptor because of "
               + "GATT_INSUFFICIENT_AUTHENTICATION"
               + " and "
               + "GATT_INSUFFICIENT_ENCRYPTION: "
               + descriptor.getUuid());
       getBluetoothLe()
           .getBleEventCallback()
           .onCharacteristicNotificationEnablingFail(gatt, descriptor, status);
       break;
     default:
       Log.w(TAG, "Failed to write descriptor: " + descriptor.getUuid());
       getBluetoothLe()
           .getBleEventCallback()
           .onCharacteristicNotificationEnablingFail(gatt, descriptor, status);
       break;
   }
 }
  @Override
  public void onCharacteristicWrite(
      BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
    super.onCharacteristicWrite(gatt, characteristic, status);
    // Log.d(LOG_TAG, "onCharacteristicWrite " + characteristic);

    if (writeCallback != null) {

      if (writeQueue.size() > 0) {
        byte[] data = writeQueue.get(0);
        writeQueue.remove(0);
        // Log.d(LOG_TAG, "rimangono in coda: " + writeQueue.size());
        doWrite(characteristic, data);
      } else {

        if (status == BluetoothGatt.GATT_SUCCESS) {
          writeCallback.invoke();
          // Log.e(LOG_TAG, "writeCallback invocato");
        } else {
          // writeCallback.error(status);
          Log.e(LOG_TAG, "errore onCharacteristicWrite:" + status);
          writeFailCallback.invoke("Error writing status: " + status);
        }

        writeCallback = null;
        writeFailCallback = null;
      }
    } else Log.e(LOG_TAG, "Nessun callback su write");
  }
 @Override
 public void onServicesDiscovered(BluetoothGatt gatt, int status) {
   super.onServicesDiscovered(gatt, status);
   connectCallback.invoke();
   connectCallback = null;
   connectFailCallback = null;
 }
    /** Callback invoked by Android framework when a characteristic write completes */
    @Override
    public void onCharacteristicWrite(
        BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
      super.onCharacteristicWrite(gatt, characteristic, status);
      if (status == 0) {
        try {
          processCharacteristicWrite(characteristic);
        } catch (Throwable t) {
          Log.e(TAG, "error", t);
        }
      }

      mRequestQueue.next(); // Execute the next queued request, if any
    }
  @Override
  public void onCharacteristicChanged(
      BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
    super.onCharacteristicChanged(gatt, characteristic);

    // Log.d(LOG_TAG, "onCharacteristicChanged " + characteristic);
    byte[] dataValue = characteristic.getValue();
    Log.d(LOG_TAG, "Letto:" + BleManager.bytesToHex(dataValue) + " da:" + device.getAddress());

    WritableMap map = Arguments.createMap();
    map.putString("peripheral", device.getAddress());
    map.putString("characteristic", characteristic.getUuid().toString());
    map.putString("value", BleManager.bytesToHex(dataValue));
    sendEvent("BleManagerDidUpdateValueForCharacteristic", map);
  }
 @Override
 public void onServicesDiscovered(BluetoothGatt gatt, int status) {
   super.onServicesDiscovered(gatt, status);
   LogTool.d(TAG, "on services discovered success ? " + (status == BluetoothGatt.GATT_SUCCESS));
   if (status == BluetoothGatt.GATT_SUCCESS) {
     displayGattServices(gatt.getServices());
     if (gatt.getServices() == null || gatt.getServices().isEmpty()) {
       LogTool.e(TAG, "No gatt service found!");
       onServicesDiscoveredFailed(gatt);
       return;
     }
     if (!checkCloudWatchServiceSupported(gatt)) {
       LogTool.e(TAG, "Clouder watch services does not support!");
       onServicesDiscoveredFailed(gatt);
       gatt.disconnect();
       return;
     }
     onServicesDiscoveredSuccess(gatt);
   } else {
     LogTool.e(TAG, "Can not found gatt services!");
     onServicesDiscoveredFailed(gatt);
   }
 }
  @Override
  public void onCharacteristicRead(
      BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
    super.onCharacteristicRead(gatt, characteristic, status);
    Log.d(LOG_TAG, "onCharacteristicRead " + characteristic);

    if (readCallback != null) {

      if (status == BluetoothGatt.GATT_SUCCESS) {
        byte[] dataValue = characteristic.getValue();
        String value = BleManager.bytesToHex(dataValue);

        if (readCallback != null) {
          readCallback.invoke(value);
          readCallback = null;
        }
      } else {
        readFailCallback.invoke("Error reading " + characteristic.getUuid() + " status=" + status);
        readFailCallback = null;
      }

      readCallback = null;
    }
  }
 @Override
 public void onCharacteristicChanged(
     BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
   super.onCharacteristicChanged(gatt, characteristic);
 }
 @Override
 public void onCharacteristicWrite(
     BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
   super.onCharacteristicWrite(gatt, characteristic, status);
 }
 @Override
 public void onDescriptorWrite(
     BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
   super.onDescriptorWrite(gatt, descriptor, status);
   Log.d(LOG_TAG, "onDescriptorWrite " + descriptor);
 }