/** * Returns the callback that is registered for value changes (notifications) of given * characteristic. After assigning the notifications callback, notifications must be * enabled using {@link #enableNotifications(BluetoothGattCharacteristic)}. * This applies also when they were already enabled on the remote side. * <p> * To remove the callback, disable notifications using * {@link #disableNotifications(BluetoothGattCharacteristic)}. * * @param characteristic characteristic to bind the callback with. If null, the returned * callback will not be null, but will not be used. * @return The callback. */ @MainThread @NonNull protected final ValueChangedCallback setNotificationCallback(@Nullable final BluetoothGattCharacteristic characteristic) { ValueChangedCallback callback = mNotificationCallbacks.get(characteristic); if (callback == null) { callback = new ValueChangedCallback(); if (characteristic != null) { mNotificationCallbacks.put(characteristic, callback); } } return callback.free(); }
/** * This method enables notifications on the Battery Level characteristic. * * @deprecated Use {@link #setNotificationCallback(BluetoothGattCharacteristic)} and * {@link #enableNotifications(BluetoothGattCharacteristic)} instead. */ @SuppressWarnings("ConstantConditions") @Deprecated protected void enableBatteryLevelNotifications() { if (mBatteryLevelNotificationCallback == null) { mBatteryLevelNotificationCallback = new ValueChangedCallback() .with((device, data) -> { if (data.size() == 1) { final int batteryLevel = data.getIntValue(Data.FORMAT_UINT8, 0); mBatteryValue = batteryLevel; final BleManagerGattCallback callback = mGattCallback; if (callback != null) { callback.onBatteryValueReceived(mBluetoothGatt, batteryLevel); } mCallbacks.onBatteryValueReceived(device, batteryLevel); } }); } Request.newEnableBatteryLevelNotificationsRequest().setManager(this) .done(device -> log(Log.INFO, "Battery Level notifications enabled")) .enqueue(); }