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

Commit e94b33b1 authored by pramod kotreshappa's avatar pramod kotreshappa Committed by Steve Kondik
Browse files

Bluetooth: Fix for device gets stuck in pairing

Fix for device gets stuck in pairing when trying to pair to two
devices one after the other or simultaneous. Now second pairing
is blocked when first pairing is ongoing.

Change-Id: Ib7e6788243a38b3a0dd864bee2b0797305468e27
CRs-fixed: 610056
parent 00ef5545
Loading
Loading
Loading
Loading
+27 −0
Original line number Diff line number Diff line
@@ -306,15 +306,26 @@ final class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> {
    }

    boolean startPairing() {
        if(mLocalAdapter.checkPairingState() == true)
        {
            Log.v(TAG, "Pairing is onging");
            return true;
        }

        mLocalAdapter.setPairingState(true);
        Log.v(TAG, "startPairing : isPairing : " + mLocalAdapter.checkPairingState());

        // Pairing is unreliable while scanning, so cancel discovery
        if (mLocalAdapter.isDiscovering()) {
            mLocalAdapter.cancelDiscovery();
        }

        if (!mDevice.createBond()) {
            mLocalAdapter.setPairingState(false);
            return false;
        }

        Log.v(TAG, "startPairing CreateBond : isPairing : " + mLocalAdapter.checkPairingState());
        mConnectAfterPairing = true;  // auto-connect after pairing
        return true;
    }
@@ -340,6 +351,7 @@ final class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> {
                final boolean successful = dev.removeBond();
                if (successful) {
                    if (Utils.D) {
                        mDevice.setAlias(null);
                        Log.d(TAG, "Command sent successfully:REMOVE_BOND " + describe(null));
                    }
                    setRemovable(true);
@@ -564,11 +576,24 @@ final class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> {
    }

    void onBondingStateChanged(int bondState) {
        if (bondState == BluetoothDevice.BOND_NONE) {
            mLocalAdapter.setPairingState(false);
            mProfiles.clear();
            mConnectAfterPairing = false;  // cancel auto-connect
            setPhonebookPermissionChoice(ACCESS_UNKNOWN);
            setMessagePermissionChoice(ACCESS_UNKNOWN);
            mPhonebookRejectedTimes = 0;
            savePhonebookRejectTimes();
            mMessageRejectedTimes = 0;
            saveMessageRejectTimes();
            Log.v(TAG,"onBondingstate none: isPairing : " + mLocalAdapter.checkPairingState());
        }

        if(DEBUG) Log.d(TAG, "onBondingStateChanged" + bondState);

        switch (bondState) {
            case BluetoothDevice.BOND_NONE:
                mLocalAdapter.setPairingState(false);
                mProfiles.clear();
                mConnectAfterPairing = false;  // cancel auto-connect
                // fall through
@@ -587,12 +612,14 @@ final class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> {
                break;

            case BluetoothDevice.BOND_BONDED:
                mLocalAdapter.setPairingState(false);
                if (mDevice.isBluetoothDock()) {
                    onBondingDockConnect();
                } else if (mConnectAfterPairing) {
                    connect(false);
                }
                mConnectAfterPairing = false;
                Log.v(TAG,"BondState bonded: isPairing : " + mLocalAdapter.checkPairingState());
                break;

            default:
+12 −2
Original line number Diff line number Diff line
@@ -49,7 +49,7 @@ public final class LocalBluetoothAdapter {
    private static final int SCAN_EXPIRATION_MS = 5 * 60 * 1000; // 5 mins

    private long mLastScan;

    public static boolean isPairing = false;
    private LocalBluetoothAdapter(BluetoothAdapter adapter) {
        mAdapter = adapter;
    }
@@ -82,10 +82,12 @@ public final class LocalBluetoothAdapter {
    }

    boolean enable() {
        isPairing = false;
        return mAdapter.enable();
    }

    boolean disable() {
        isPairing = false;
        return mAdapter.disable();
    }

@@ -199,7 +201,7 @@ public final class LocalBluetoothAdapter {
        boolean success = enabled
                ? mAdapter.enable()
                : mAdapter.disable();

        isPairing = false;
        if (success) {
            setBluetoothStateInt(enabled
                ? BluetoothAdapter.STATE_TURNING_ON
@@ -213,4 +215,12 @@ public final class LocalBluetoothAdapter {
            syncBluetoothState();
        }
    }

    public boolean checkPairingState(){
       return isPairing;
    }

    public void setPairingState(boolean state){
       isPairing = state;
    }
}