private PeripheralError setCharacteristicNotification()

in rx-central-ble/src/main/java/com/uber/rxcentralble/core/CorePeripheral.java [449:489]


  private PeripheralError setCharacteristicNotification(
      BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic characteristic, boolean enable) {

    BluetoothGattDescriptor cccd = characteristic.getDescriptor(CCCD_UUID);
    if (cccd == null) {
      return new PeripheralError(PeripheralError.Code.SET_CHARACTERISTIC_NOTIFICATION_CCCD_MISSING);
    }

    if (!bluetoothGatt.setCharacteristicNotification(characteristic, enable)) {
      return new PeripheralError(PeripheralError.Code.SET_CHARACTERISTIC_NOTIFICATION_FAILED);
    }

    int properties = characteristic.getProperties();
    byte[] value;

    if (enable) {
      if ((properties & BluetoothGattCharacteristic.PROPERTY_NOTIFY)
          == BluetoothGattCharacteristic.PROPERTY_NOTIFY) {
        value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE;
      } else if ((properties & BluetoothGattCharacteristic.PROPERTY_INDICATE)
          == BluetoothGattCharacteristic.PROPERTY_INDICATE) {
        value = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE;
      } else {
        return new PeripheralError(PeripheralError.Code.SET_CHARACTERISTIC_NOTIFICATION_MISSING_PROPERTY);
      }
    } else {
      value = BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
    }

    characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);

    if (!cccd.setValue(value)) {
      return new PeripheralError(CHARACTERISTIC_SET_VALUE_FAILED);
    }

    if (!bluetoothGatt.writeDescriptor(cccd)) {
      return new PeripheralError(PeripheralError.Code.WRITE_DESCRIPTOR_FAILED, ERROR_STATUS_CALL_FAILED);
    }

    return null;
  }