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

Commit 66734225 authored by Jakub Pawlowski's avatar Jakub Pawlowski Committed by Andre Eisenbach
Browse files

Fix connecting to profiles when bonding from local device (2/3)

When bond is created from Bluetooth Settings, profiles gets properly
auto connected when services are discovered. If pairing is done from any
other app, i.e. NFC app, the bond is not being recognized as initiated
from local device, and profiles are not connected.

This patch makes sure that if bonding is initiated, no matter from which
app, it will cause proper profiles to connect.

Bug: 30211618
Change-Id: Icda038447554f429b9cbfc74d68c679e63572bdb
parent be4507f6
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
@@ -1123,6 +1123,13 @@ public class AdapterService extends Service {
            return service.getBondState(device);
        }

        public boolean isBondingInitiatedLocally(BluetoothDevice device) {
            // don't check caller, may be called from system UI
            AdapterService service = getService();
            if (service == null) return false;
            return service.isBondingInitiatedLocally(device);
        }

        public int getConnectionState(BluetoothDevice device) {
            AdapterService service = getService();
            if (service == null) return 0;
@@ -1599,6 +1606,8 @@ public class AdapterService extends Service {
            return false;
        }

        deviceProp.setBondingInitiatedLocally(true);

        // Pairing is unreliable while scanning, so cancel discovery
        // Note, remove this when native stack improves
        cancelDiscoveryNative();
@@ -1878,6 +1887,12 @@ public class AdapterService extends Service {
     boolean cancelBondProcess(BluetoothDevice device) {
        enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, "Need BLUETOOTH ADMIN permission");
        byte[] addr = Utils.getBytesFromAddress(device.getAddress());

        DeviceProperties deviceProp = mRemoteDevices.getDeviceProperties(device);
        if (deviceProp != null) {
            deviceProp.setBondingInitiatedLocally(false);
        }

        return cancelBondNative(addr);
    }

@@ -1887,6 +1902,8 @@ public class AdapterService extends Service {
        if (deviceProp == null || deviceProp.getBondState() != BluetoothDevice.BOND_BONDED) {
            return false;
        }
        deviceProp.setBondingInitiatedLocally(false);

        Message msg = mBondStateMachine.obtainMessage(BondStateMachine.REMOVE_BOND);
        msg.obj = device;
        mBondStateMachine.sendMessage(msg);
@@ -1902,6 +1919,15 @@ public class AdapterService extends Service {
        return deviceProp.getBondState();
    }

    boolean isBondingInitiatedLocally(BluetoothDevice device) {
        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        DeviceProperties deviceProp = mRemoteDevices.getDeviceProperties(device);
        if (deviceProp == null) {
            return false;
        }
        return deviceProp.isBondingInitiatedLocally();
    }

    int getConnectionState(BluetoothDevice device) {
        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        byte[] addr = Utils.getBytesFromAddress(device.getAddress());
+19 −0
Original line number Diff line number Diff line
@@ -100,6 +100,7 @@ final class RemoteDevices {
        private String mAlias;
        private int mBondState;
        private BluetoothDevice mDevice;
        private boolean isBondingInitiatedLocally;

        DeviceProperties() {
            mBondState = BluetoothDevice.BOND_NONE;
@@ -217,6 +218,24 @@ final class RemoteDevices {
                return mBondState;
            }
        }

        /**
         * @param isBondingInitiatedLocally wether bonding is initiated locally
         */
        void setBondingInitiatedLocally(boolean isBondingInitiatedLocally) {
            synchronized (mObject) {
                this.isBondingInitiatedLocally = isBondingInitiatedLocally;
            }
        }

        /**
         * @return the isBondingInitiatedLocally
         */
        boolean isBondingInitiatedLocally() {
            synchronized (mObject) {
                return isBondingInitiatedLocally;
            }
        }
    }

    private void sendUuidIntent(BluetoothDevice device) {