@Override
 public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
   Log.i("onConnectionStateChange", "Status: " + status);
   switch (newState) {
     case BluetoothProfile.STATE_CONNECTED:
       Log.i("gattCallback", "STATE_CONNECTED");
       gatt.discoverServices();
       break;
     case BluetoothProfile.STATE_DISCONNECTED:
       Log.e("gattCallback", "STATE_DISCONNECTED");
       break;
     default:
       Log.e("gattCallback", "STATE_OTHER");
   }
 }
  @Override
  public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

    Log.d(
        LOG_TAG,
        "onConnectionStateChange da "
            + status
            + " a "
            + newState
            + " peripheral:"
            + device.getAddress());

    this.gatt = gatt;

    if (newState == BluetoothGatt.STATE_CONNECTED) {

      connected = true;
      gatt.discoverServices();

    } else if (newState == BluetoothGatt.STATE_DISCONNECTED) {

      if (connected) {
        connected = false;

        if (gatt != null) {
          gatt.close();
          gatt = null;
        }

        WritableMap map = Arguments.createMap();
        map.putString("peripheral", device.getAddress());
        sendEvent("BleManagerDisconnectPeripheral", map);
        Log.d(LOG_TAG, "BleManagerDisconnectPeripheral peripheral:" + device.getAddress());
      }
      if (connectFailCallback != null) {
        connectFailCallback.invoke();
        connectFailCallback = null;
        connectCallback = null;
      }
    }
  }