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

Commit f9da437a authored by Michał Narajowski's avatar Michał Narajowski
Browse files

CachedBluetoothDevice: Fix detecting connect fail

Transition from CONNECTING to DISCONNECTED doesn't have to mean that the
connection has failed. Add check to verify that connection policy for
profile is ALLOWED and only then trigger connection failed.

Setting connection failed results in "Problem connecting." in Bluetooth
connected devices UI.

Bug: 268587046
Test: BluetoothInstrumentationTest
Change-Id: I1eb116e4ae1735fafe3d7f35fc0bf51d21cb87d3
parent 14e56413
Loading
Loading
Loading
Loading
+21 −1
Original line number Diff line number Diff line
@@ -226,8 +226,21 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
                    case BluetoothProfile.STATE_DISCONNECTED:
                        if (mHandler.hasMessages(profile.getProfileId())) {
                            mHandler.removeMessages(profile.getProfileId());
                            if (profile.getConnectionPolicy(mDevice) >
                                BluetoothProfile.CONNECTION_POLICY_FORBIDDEN) {
                                /*
                                 * If we received state DISCONNECTED and previous state was
                                 * CONNECTING and connection policy is FORBIDDEN or UNKNOWN
                                 * then it's not really a failure to connect.
                                 *
                                 * Connection profile is considered as failed when connection
                                 * policy indicates that profile should be connected
                                 * but it got disconnected.
                                 */
                                Log.w(TAG, "onProfileStateChanged(): Failed to connect profile");
                                setProfileConnectedStatus(profile.getProfileId(), true);
                            }
                        }
                        break;
                    default:
                        Log.w(TAG, "onProfileStateChanged(): unknown profile state : "
@@ -1188,6 +1201,13 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
    }

    private boolean isProfileConnectedFail() {
        Log.d(TAG, "anonymizedAddress=" + mDevice.getAnonymizedAddress()
                + " mIsA2dpProfileConnectedFail=" + mIsA2dpProfileConnectedFail
                + " mIsHearingAidProfileConnectedFail=" + mIsHearingAidProfileConnectedFail
                + " mIsLeAudioProfileConnectedFail=" + mIsLeAudioProfileConnectedFail
                + " mIsHeadsetProfileConnectedFail=" + mIsHeadsetProfileConnectedFail
                + " isConnectedSapDevice()=" + isConnectedSapDevice());

        return mIsA2dpProfileConnectedFail || mIsHearingAidProfileConnectedFail
                || (!isConnectedSapDevice() && mIsHeadsetProfileConnectedFail)
                || mIsLeAudioProfileConnectedFail;