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

Commit 43ca2aeb authored by Kyunglyul Hyun's avatar Kyunglyul Hyun
Browse files

Connect battery service only when it's available

As we tried to connect to the battery service
whenever a BLE device is connected, BatteryService
logs errors when a BLE device not supporting BAS
is connected.
This CL checks the availability first to prevent
logging errors

Tag: #feature
Bug: 235566393
Test: manually confirmed BAS works and no error for
devices not having BAS.

Change-Id: I87698e3838dce63e05fb69c6d2624f4219edb149
parent d2da3e10
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -221,6 +221,20 @@ public class BatteryService extends ProfileService {
        return true;
    }

    /**
     * Connects to the battery service of the given device if possible.
     * If it's impossible, it doesn't try without logging errors.
     */
    public boolean connectIfPossible(BluetoothDevice device) {
        if (device == null
                || getConnectionPolicy(device) == BluetoothProfile.CONNECTION_POLICY_FORBIDDEN
                || !Utils.arrayContains(
                        mAdapterService.getRemoteUuids(device), BluetoothUuid.BATTERY)) {
            return false;
        }
        return connect(device);
    }

    /**
     * Disconnects from the battery service of the given device.
     */
+4 −2
Original line number Diff line number Diff line
@@ -889,7 +889,7 @@ final class RemoteDevices {
            }
            BatteryService batteryService = BatteryService.getBatteryService();
            if (batteryService != null) {
                batteryService.connect(device);
                batteryService.connectIfPossible(device);
            }
            debugLog(
                    "aclStateChangeCallback: Adapter State: " + BluetoothAdapter.nameForState(state)
@@ -913,7 +913,9 @@ final class RemoteDevices {
            // Reset battery level on complete disconnection
            if (mAdapterService.getConnectionState(device) == 0) {
                BatteryService batteryService = BatteryService.getBatteryService();
                if (batteryService != null) {
                if (batteryService != null
                        && batteryService.getConnectionState(device)
                        != BluetoothProfile.STATE_DISCONNECTED) {
                    batteryService.disconnect(device);
                }
                resetBatteryLevel(device);