Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2dda064b authored by William Escande's avatar William Escande
Browse files

Framework HAP: use wrapper for readability

Bug: 311772251
Test: m Bluetooth
Flag: Exempt refactor - HAP is not launched yet
Change-Id: I7bce5ca0278af90e07336a81326e36d2744c3ca2
parent b1ba026a
Loading
Loading
Loading
Loading
+142 −208
Original line number Original line Diff line number Diff line
@@ -19,6 +19,7 @@ package android.bluetooth;


import static android.Manifest.permission.BLUETOOTH_CONNECT;
import static android.Manifest.permission.BLUETOOTH_CONNECT;
import static android.Manifest.permission.BLUETOOTH_PRIVILEGED;
import static android.Manifest.permission.BLUETOOTH_PRIVILEGED;
import static android.bluetooth.BluetoothUtils.callServiceIfEnabled;


import static java.util.Objects.requireNonNull;
import static java.util.Objects.requireNonNull;


@@ -564,8 +565,7 @@ public final class BluetoothHapClient implements BluetoothProfile, AutoCloseable
     * Set connection policy of the profile
     * Set connection policy of the profile
     *
     *
     * <p>The device should already be paired. Connection policy can be one of {@link
     * <p>The device should already be paired. Connection policy can be one of {@link
     * #CONNECTION_POLICY_ALLOWED}, {@link #CONNECTION_POLICY_FORBIDDEN}, {@link
     * #CONNECTION_POLICY_ALLOWED}, {@link #CONNECTION_POLICY_FORBIDDEN}
     * #CONNECTION_POLICY_UNKNOWN}
     *
     *
     * @param device Paired bluetooth device
     * @param device Paired bluetooth device
     * @param connectionPolicy is the connection policy to set to for this profile
     * @param connectionPolicy is the connection policy to set to for this profile
@@ -578,29 +578,22 @@ public final class BluetoothHapClient implements BluetoothProfile, AutoCloseable
    public boolean setConnectionPolicy(
    public boolean setConnectionPolicy(
            @NonNull BluetoothDevice device, @ConnectionPolicy int connectionPolicy) {
            @NonNull BluetoothDevice device, @ConnectionPolicy int connectionPolicy) {
        requireNonNull(device);
        requireNonNull(device);

        boolean defaultValue = false;
        final IBluetoothHapClient service = getService();
        if (!isValidDevice(device)
        if (service == null) {
                || (connectionPolicy != BluetoothProfile.CONNECTION_POLICY_FORBIDDEN
            Log.d(TAG, "setConnectionPolicy: Proxy not attached to service");
                        && connectionPolicy != BluetoothProfile.CONNECTION_POLICY_ALLOWED)) {
        } else if (mAdapter.isEnabled()
            return defaultValue;
                && isValidDevice(device)
                && (connectionPolicy == BluetoothProfile.CONNECTION_POLICY_FORBIDDEN
                        || connectionPolicy == BluetoothProfile.CONNECTION_POLICY_ALLOWED)) {
            try {
                return service.setConnectionPolicy(device, connectionPolicy, mAttributionSource);
            } catch (RemoteException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
        }
        }
        }
        return callServiceIfEnabled(
        return false;
                mAdapter,
                this::getService,
                s -> s.setConnectionPolicy(device, connectionPolicy, mAttributionSource),
                defaultValue);
    }
    }


    /**
    /**
     * Get the connection policy of the profile.
     * Get the connection policy of the profile.
     *
     *
     * <p>The connection policy can be any of: {@link #CONNECTION_POLICY_ALLOWED}, {@link
     * #CONNECTION_POLICY_FORBIDDEN}, {@link #CONNECTION_POLICY_UNKNOWN}
     *
     * @param device Bluetooth device
     * @param device Bluetooth device
     * @return connection policy of the device or {@link #CONNECTION_POLICY_FORBIDDEN} if device is
     * @return connection policy of the device or {@link #CONNECTION_POLICY_FORBIDDEN} if device is
     *     null
     *     null
@@ -610,17 +603,15 @@ public final class BluetoothHapClient implements BluetoothProfile, AutoCloseable
    @RequiresBluetoothConnectPermission
    @RequiresBluetoothConnectPermission
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    public @ConnectionPolicy int getConnectionPolicy(@Nullable BluetoothDevice device) {
    public @ConnectionPolicy int getConnectionPolicy(@Nullable BluetoothDevice device) {
        final IBluetoothHapClient service = getService();
        int defaultValue = BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
        if (service == null) {
        if (!isValidDevice(device)) {
            Log.d(TAG, "getConnectionPolicy: Proxy not attached to service");
            return defaultValue;
        } else if (mAdapter.isEnabled() && isValidDevice(device)) {
            try {
                return service.getConnectionPolicy(device, mAttributionSource);
            } catch (RemoteException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
        }
        }
        }
        return callServiceIfEnabled(
        return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
                mAdapter,
                this::getService,
                s -> s.getConnectionPolicy(device, mAttributionSource),
                defaultValue);
    }
    }


    /**
    /**
@@ -633,18 +624,13 @@ public final class BluetoothHapClient implements BluetoothProfile, AutoCloseable
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    @Override
    @Override
    public @NonNull List<BluetoothDevice> getConnectedDevices() {
    public @NonNull List<BluetoothDevice> getConnectedDevices() {
        final IBluetoothHapClient service = getService();
        return callServiceIfEnabled(
        if (service == null) {
                mAdapter,
            Log.d(TAG, "getConnectedDevices: Proxy not attached to service");
                this::getService,
        } else if (mAdapter.isEnabled()) {
                s ->
            try {
                        Attributable.setAttributionSource(
                return Attributable.setAttributionSource(
                                s.getConnectedDevices(mAttributionSource), mAttributionSource),
                        service.getConnectedDevices(mAttributionSource), mAttributionSource);
                Collections.emptyList());
            } catch (RemoteException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            }
        }
        return Collections.emptyList();
    }
    }


    /**
    /**
@@ -658,19 +644,14 @@ public final class BluetoothHapClient implements BluetoothProfile, AutoCloseable
    @Override
    @Override
    @NonNull
    @NonNull
    public List<BluetoothDevice> getDevicesMatchingConnectionStates(@NonNull int[] states) {
    public List<BluetoothDevice> getDevicesMatchingConnectionStates(@NonNull int[] states) {
        final IBluetoothHapClient service = getService();
        return callServiceIfEnabled(
        if (service == null) {
                mAdapter,
            Log.d(TAG, "getDevicesMatchingConnectionStates: Proxy not attached to service");
                this::getService,
        } else if (mAdapter.isEnabled()) {
                s ->
            try {
                        Attributable.setAttributionSource(
                return Attributable.setAttributionSource(
                                s.getDevicesMatchingConnectionStates(states, mAttributionSource),
                        service.getDevicesMatchingConnectionStates(states, mAttributionSource),
                                mAttributionSource),
                        mAttributionSource);
                Collections.emptyList());
            } catch (RemoteException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            }
        }
        return Collections.emptyList();
    }
    }


    /**
    /**
@@ -684,17 +665,15 @@ public final class BluetoothHapClient implements BluetoothProfile, AutoCloseable
    @Override
    @Override
    @BluetoothProfile.BtProfileState
    @BluetoothProfile.BtProfileState
    public int getConnectionState(@NonNull BluetoothDevice device) {
    public int getConnectionState(@NonNull BluetoothDevice device) {
        final IBluetoothHapClient service = getService();
        int defaultValue = BluetoothProfile.STATE_DISCONNECTED;
        if (service == null) {
        if (!isValidDevice(device)) {
            Log.d(TAG, "getConnectionState: Proxy not attached to service");
            return defaultValue;
        } else if (mAdapter.isEnabled() && isValidDevice(device)) {
            try {
                return service.getConnectionState(device, mAttributionSource);
            } catch (RemoteException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            }
        }
        }
        return BluetoothProfile.STATE_DISCONNECTED;
        return callServiceIfEnabled(
                mAdapter,
                this::getService,
                s -> s.getConnectionState(device, mAttributionSource),
                defaultValue);
    }
    }


    /**
    /**
@@ -714,23 +693,18 @@ public final class BluetoothHapClient implements BluetoothProfile, AutoCloseable
    @SystemApi
    @SystemApi
    @FlaggedApi(Flags.FLAG_SETTINGS_CAN_CONTROL_HAP_PRESET)
    @FlaggedApi(Flags.FLAG_SETTINGS_CAN_CONTROL_HAP_PRESET)
    @RequiresBluetoothConnectPermission
    @RequiresBluetoothConnectPermission
    @RequiresPermission(
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
            allOf = {
                BLUETOOTH_CONNECT,
                BLUETOOTH_PRIVILEGED,
            })
    public int getHapGroup(@NonNull BluetoothDevice device) {
    public int getHapGroup(@NonNull BluetoothDevice device) {
        final IBluetoothHapClient service = getService();
        requireNonNull(device);
        if (service == null) {
        int defaultValue = BluetoothCsipSetCoordinator.GROUP_ID_INVALID;
            Log.d(TAG, "getHapGroup: Proxy not attached to service");
        if (!isValidDevice(device)) {
        } else if (mAdapter.isEnabled() && isValidDevice(device)) {
            return defaultValue;
            try {
                return service.getHapGroup(device, mAttributionSource);
            } catch (RemoteException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            }
        }
        }
        return BluetoothCsipSetCoordinator.GROUP_ID_INVALID;
        return callServiceIfEnabled(
                mAdapter,
                this::getService,
                s -> s.getHapGroup(device, mAttributionSource),
                defaultValue);
    }
    }


    /**
    /**
@@ -745,17 +719,16 @@ public final class BluetoothHapClient implements BluetoothProfile, AutoCloseable
    @RequiresBluetoothConnectPermission
    @RequiresBluetoothConnectPermission
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    public int getActivePresetIndex(@NonNull BluetoothDevice device) {
    public int getActivePresetIndex(@NonNull BluetoothDevice device) {
        final IBluetoothHapClient service = getService();
        requireNonNull(device);
        if (service == null) {
        int defaultValue = PRESET_INDEX_UNAVAILABLE;
            Log.d(TAG, "getActivePresetIndex: Proxy not attached to service");
        if (!isValidDevice(device)) {
        } else if (mAdapter.isEnabled() && isValidDevice(device)) {
            return defaultValue;
            try {
                return service.getActivePresetIndex(device, mAttributionSource);
            } catch (RemoteException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            }
        }
        }
        return PRESET_INDEX_UNAVAILABLE;
        return callServiceIfEnabled(
                mAdapter,
                this::getService,
                s -> s.getActivePresetIndex(device, mAttributionSource),
                defaultValue);
    }
    }


    /**
    /**
@@ -770,18 +743,16 @@ public final class BluetoothHapClient implements BluetoothProfile, AutoCloseable
    @RequiresBluetoothConnectPermission
    @RequiresBluetoothConnectPermission
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    public @Nullable BluetoothHapPresetInfo getActivePresetInfo(@NonNull BluetoothDevice device) {
    public @Nullable BluetoothHapPresetInfo getActivePresetInfo(@NonNull BluetoothDevice device) {
        final IBluetoothHapClient service = getService();
        requireNonNull(device);
        if (service == null) {
        BluetoothHapPresetInfo defaultValue = null;
            Log.d(TAG, "getActivePresetInfo: Proxy not attached to service");
        if (!isValidDevice(device)) {
        } else if (mAdapter.isEnabled() && isValidDevice(device)) {
            return defaultValue;
            try {
                return service.getActivePresetInfo(device, mAttributionSource);
            } catch (RemoteException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            }
        }
        }

        return callServiceIfEnabled(
        return null;
                mAdapter,
                this::getService,
                s -> s.getActivePresetInfo(device, mAttributionSource),
                defaultValue);
    }
    }


    /**
    /**
@@ -799,16 +770,14 @@ public final class BluetoothHapClient implements BluetoothProfile, AutoCloseable
    @RequiresBluetoothConnectPermission
    @RequiresBluetoothConnectPermission
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    public void selectPreset(@NonNull BluetoothDevice device, int presetIndex) {
    public void selectPreset(@NonNull BluetoothDevice device, int presetIndex) {
        final IBluetoothHapClient service = getService();
        requireNonNull(device);
        if (service == null) {
        if (!isValidDevice(device)) {
            Log.d(TAG, "selectPreset: Proxy not attached to service");
            return;
        } else if (mAdapter.isEnabled() && isValidDevice(device)) {
            try {
                service.selectPreset(device, presetIndex, mAttributionSource);
            } catch (RemoteException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            }
        }
        }
        callServiceIfEnabled(
                mAdapter,
                this::getService,
                s -> s.selectPreset(device, presetIndex, mAttributionSource));
    }
    }


    /**
    /**
@@ -830,16 +799,10 @@ public final class BluetoothHapClient implements BluetoothProfile, AutoCloseable
    @RequiresBluetoothConnectPermission
    @RequiresBluetoothConnectPermission
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    public void selectPresetForGroup(int groupId, int presetIndex) {
    public void selectPresetForGroup(int groupId, int presetIndex) {
        final IBluetoothHapClient service = getService();
        callServiceIfEnabled(
        if (service == null) {
                mAdapter,
            Log.d(TAG, "selectPresetForGroup: Proxy not attached to service");
                this::getService,
        } else if (mAdapter.isEnabled()) {
                s -> s.selectPresetForGroup(groupId, presetIndex, mAttributionSource));
            try {
                service.selectPresetForGroup(groupId, presetIndex, mAttributionSource);
            } catch (RemoteException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            }
        }
    }
    }


    /**
    /**
@@ -856,16 +819,12 @@ public final class BluetoothHapClient implements BluetoothProfile, AutoCloseable
    @RequiresBluetoothConnectPermission
    @RequiresBluetoothConnectPermission
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    public void switchToNextPreset(@NonNull BluetoothDevice device) {
    public void switchToNextPreset(@NonNull BluetoothDevice device) {
        final IBluetoothHapClient service = getService();
        requireNonNull(device);
        if (service == null) {
        if (!isValidDevice(device)) {
            Log.d(TAG, "switchToNextPreset: Proxy not attached to service");
            return;
        } else if (mAdapter.isEnabled() && isValidDevice(device)) {
            try {
                service.switchToNextPreset(device, mAttributionSource);
            } catch (RemoteException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            }
        }
        }
        callServiceIfEnabled(
                mAdapter, this::getService, s -> s.switchToNextPreset(device, mAttributionSource));
    }
    }


    /**
    /**
@@ -885,16 +844,10 @@ public final class BluetoothHapClient implements BluetoothProfile, AutoCloseable
    @RequiresBluetoothConnectPermission
    @RequiresBluetoothConnectPermission
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    public void switchToNextPresetForGroup(int groupId) {
    public void switchToNextPresetForGroup(int groupId) {
        final IBluetoothHapClient service = getService();
        callServiceIfEnabled(
        if (service == null) {
                mAdapter,
            Log.d(TAG, "switchToNextPresetForGroup: Proxy not attached to service");
                this::getService,
        } else if (mAdapter.isEnabled()) {
                s -> s.switchToNextPresetForGroup(groupId, mAttributionSource));
            try {
                service.switchToNextPresetForGroup(groupId, mAttributionSource);
            } catch (RemoteException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            }
        }
    }
    }


    /**
    /**
@@ -911,16 +864,14 @@ public final class BluetoothHapClient implements BluetoothProfile, AutoCloseable
    @RequiresBluetoothConnectPermission
    @RequiresBluetoothConnectPermission
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    public void switchToPreviousPreset(@NonNull BluetoothDevice device) {
    public void switchToPreviousPreset(@NonNull BluetoothDevice device) {
        final IBluetoothHapClient service = getService();
        requireNonNull(device);
        if (service == null) {
        if (!isValidDevice(device)) {
            Log.d(TAG, "switchToPreviousPreset: Proxy not attached to service");
            return;
        } else if (mAdapter.isEnabled() && isValidDevice(device)) {
            try {
                service.switchToPreviousPreset(device, mAttributionSource);
            } catch (RemoteException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            }
        }
        }
        callServiceIfEnabled(
                mAdapter,
                this::getService,
                s -> s.switchToPreviousPreset(device, mAttributionSource));
    }
    }


    /**
    /**
@@ -940,23 +891,17 @@ public final class BluetoothHapClient implements BluetoothProfile, AutoCloseable
    @RequiresBluetoothConnectPermission
    @RequiresBluetoothConnectPermission
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    public void switchToPreviousPresetForGroup(int groupId) {
    public void switchToPreviousPresetForGroup(int groupId) {
        final IBluetoothHapClient service = getService();
        callServiceIfEnabled(
        if (service == null) {
                mAdapter,
            Log.d(TAG, "switchToPreviousPresetForGroup: Proxy not attached to service");
                this::getService,
        } else if (mAdapter.isEnabled()) {
                s -> s.switchToPreviousPresetForGroup(groupId, mAttributionSource));
            try {
                service.switchToPreviousPresetForGroup(groupId, mAttributionSource);
            } catch (RemoteException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            }
        }
    }
    }


    /**
    /**
     * Requests the preset info
     * Requests the preset info
     *
     *
     * @param device is the device for which we want to get the preset name
     * @param device is the device for which we want to get the preset
     * @param presetIndex is an index of one of the available presets
     * @param presetIndex is an index of an available presets
     * @return preset info
     * @return preset info
     * @hide
     * @hide
     */
     */
@@ -966,17 +911,16 @@ public final class BluetoothHapClient implements BluetoothProfile, AutoCloseable
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    @Nullable
    @Nullable
    public BluetoothHapPresetInfo getPresetInfo(@NonNull BluetoothDevice device, int presetIndex) {
    public BluetoothHapPresetInfo getPresetInfo(@NonNull BluetoothDevice device, int presetIndex) {
        final IBluetoothHapClient service = getService();
        requireNonNull(device);
        if (service == null) {
        BluetoothHapPresetInfo defaultValue = null;
            Log.d(TAG, "getPresetInfo: Proxy not attached to service");
        if (!isValidDevice(device)) {
        } else if (mAdapter.isEnabled() && isValidDevice(device)) {
            return defaultValue;
            try {
                return service.getPresetInfo(device, presetIndex, mAttributionSource);
            } catch (RemoteException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            }
        }
        }
        return null;
        return callServiceIfEnabled(
                mAdapter,
                this::getService,
                s -> s.getPresetInfo(device, presetIndex, mAttributionSource),
                defaultValue);
    }
    }


    /**
    /**
@@ -990,17 +934,16 @@ public final class BluetoothHapClient implements BluetoothProfile, AutoCloseable
    @RequiresBluetoothConnectPermission
    @RequiresBluetoothConnectPermission
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    public @NonNull List<BluetoothHapPresetInfo> getAllPresetInfo(@NonNull BluetoothDevice device) {
    public @NonNull List<BluetoothHapPresetInfo> getAllPresetInfo(@NonNull BluetoothDevice device) {
        final IBluetoothHapClient service = getService();
        requireNonNull(device);
        if (service == null) {
        List<BluetoothHapPresetInfo> defaultValue = Collections.emptyList();
            Log.d(TAG, "getAllPresetInfo: Proxy not attached to service");
        if (!isValidDevice(device)) {
        } else if (mAdapter.isEnabled() && isValidDevice(device)) {
            return defaultValue;
            try {
                return service.getAllPresetInfo(device, mAttributionSource);
            } catch (RemoteException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            }
        }
        }
        return Collections.emptyList();
        return callServiceIfEnabled(
                mAdapter,
                this::getService,
                s -> s.getAllPresetInfo(device, mAttributionSource),
                defaultValue);
    }
    }


    /**
    /**
@@ -1013,17 +956,16 @@ public final class BluetoothHapClient implements BluetoothProfile, AutoCloseable
    @RequiresBluetoothConnectPermission
    @RequiresBluetoothConnectPermission
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    public int getFeatures(@NonNull BluetoothDevice device) {
    public int getFeatures(@NonNull BluetoothDevice device) {
        final IBluetoothHapClient service = getService();
        requireNonNull(device);
        if (service == null) {
        int defaultValue = 0x00;
            Log.d(TAG, "getFeatures: Proxy not attached to service");
        if (!isValidDevice(device)) {
        } else if (mAdapter.isEnabled() && isValidDevice(device)) {
            return defaultValue;
            try {
                return service.getFeatures(device, mAttributionSource);
            } catch (RemoteException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            }
        }
        }
        return 0x00;
        return callServiceIfEnabled(
                mAdapter,
                this::getService,
                s -> s.getFeatures(device, mAttributionSource),
                defaultValue);
    }
    }


    /**
    /**
@@ -1119,16 +1061,14 @@ public final class BluetoothHapClient implements BluetoothProfile, AutoCloseable
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    public void setPresetName(
    public void setPresetName(
            @NonNull BluetoothDevice device, int presetIndex, @NonNull String name) {
            @NonNull BluetoothDevice device, int presetIndex, @NonNull String name) {
        final IBluetoothHapClient service = getService();
        requireNonNull(device);
        if (service == null) {
        if (!isValidDevice(device)) {
            Log.d(TAG, "setPresetName: Proxy not attached to service");
            return;
        } else if (mAdapter.isEnabled() && isValidDevice(device)) {
            try {
                service.setPresetName(device, presetIndex, name, mAttributionSource);
            } catch (RemoteException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            }
        }
        }
        callServiceIfEnabled(
                mAdapter,
                this::getService,
                s -> s.setPresetName(device, presetIndex, name, mAttributionSource));
    }
    }


    /**
    /**
@@ -1150,16 +1090,10 @@ public final class BluetoothHapClient implements BluetoothProfile, AutoCloseable
    @RequiresBluetoothConnectPermission
    @RequiresBluetoothConnectPermission
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    public void setPresetNameForGroup(int groupId, int presetIndex, @NonNull String name) {
    public void setPresetNameForGroup(int groupId, int presetIndex, @NonNull String name) {
        final IBluetoothHapClient service = getService();
        callServiceIfEnabled(
        if (service == null) {
                mAdapter,
            Log.d(TAG, "setPresetNameForGroup: Proxy not attached to service");
                this::getService,
        } else if (mAdapter.isEnabled()) {
                s -> s.setPresetNameForGroup(groupId, presetIndex, name, mAttributionSource));
            try {
                service.setPresetNameForGroup(groupId, presetIndex, name, mAttributionSource);
            } catch (RemoteException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            }
        }
    }
    }


    private boolean isValidDevice(BluetoothDevice device) {
    private boolean isValidDevice(BluetoothDevice device) {