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
This commit is contained in:
Michał Narajowski
2023-03-02 15:09:23 +00:00
parent 14e5641321
commit f9da437a5f

View File

@@ -226,7 +226,20 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
case BluetoothProfile.STATE_DISCONNECTED: case BluetoothProfile.STATE_DISCONNECTED:
if (mHandler.hasMessages(profile.getProfileId())) { if (mHandler.hasMessages(profile.getProfileId())) {
mHandler.removeMessages(profile.getProfileId()); mHandler.removeMessages(profile.getProfileId());
setProfileConnectedStatus(profile.getProfileId(), true); 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; break;
default: default:
@@ -1188,6 +1201,13 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
} }
private boolean isProfileConnectedFail() { private boolean isProfileConnectedFail() {
Log.d(TAG, "anonymizedAddress=" + mDevice.getAnonymizedAddress()
+ " mIsA2dpProfileConnectedFail=" + mIsA2dpProfileConnectedFail
+ " mIsHearingAidProfileConnectedFail=" + mIsHearingAidProfileConnectedFail
+ " mIsLeAudioProfileConnectedFail=" + mIsLeAudioProfileConnectedFail
+ " mIsHeadsetProfileConnectedFail=" + mIsHeadsetProfileConnectedFail
+ " isConnectedSapDevice()=" + isConnectedSapDevice());
return mIsA2dpProfileConnectedFail || mIsHearingAidProfileConnectedFail return mIsA2dpProfileConnectedFail || mIsHearingAidProfileConnectedFail
|| (!isConnectedSapDevice() && mIsHeadsetProfileConnectedFail) || (!isConnectedSapDevice() && mIsHeadsetProfileConnectedFail)
|| mIsLeAudioProfileConnectedFail; || mIsLeAudioProfileConnectedFail;