audio: Do not use A2DP/LE status when populating device port
The events about the status of A2DP and LE are sent by
the framework asynchronously with attempts to connect
an external device. As a result, the connection attempt
may be rejected by the bluetooth HAL module.
Bug: 327711955
Test: atest VtsHalAudioCoreTargetTest
Change-Id: Iecef0ee6a66cd290237fdcccd6a7016e9622c4b0
diff --git a/audio/aidl/default/bluetooth/ModuleBluetooth.cpp b/audio/aidl/default/bluetooth/ModuleBluetooth.cpp
index ac375a0..8c381cd 100644
--- a/audio/aidl/default/bluetooth/ModuleBluetooth.cpp
+++ b/audio/aidl/default/bluetooth/ModuleBluetooth.cpp
@@ -224,31 +224,19 @@
const auto& devicePort = audioPort->ext.get<AudioPortExt::device>();
const auto& description = devicePort.device.type;
// This method must return an error when the device can not be connected.
- if (description.connection == AudioDeviceDescription::CONNECTION_BT_A2DP) {
- bool isA2dpEnabled = false;
- if (!!mBluetoothA2dp) {
- RETURN_STATUS_IF_ERROR((*mBluetoothA2dp).isEnabled(&isA2dpEnabled));
- }
- LOG(DEBUG) << __func__ << ": isA2dpEnabled: " << isA2dpEnabled;
- if (!isA2dpEnabled) return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
- } else if (description.connection == AudioDeviceDescription::CONNECTION_BT_LE) {
- bool isLeEnabled = false;
- if (!!mBluetoothLe) {
- RETURN_STATUS_IF_ERROR((*mBluetoothLe).isEnabled(&isLeEnabled));
- }
- LOG(DEBUG) << __func__ << ": isLeEnabled: " << isLeEnabled;
- if (!isLeEnabled) return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
- } else if (description.connection == AudioDeviceDescription::CONNECTION_WIRELESS &&
- description.type == AudioDeviceType::OUT_HEARING_AID) {
- // Hearing aids can use a number of profiles, no single switch exists.
- } else {
+ // Since A2DP/LE status events are sent asynchronously, it is more reliable
+ // to attempt connecting to the BT stack rather than judge by the A2DP/LE status.
+ if (description.connection != AudioDeviceDescription::CONNECTION_BT_A2DP &&
+ description.connection != AudioDeviceDescription::CONNECTION_BT_LE &&
+ !(description.connection == AudioDeviceDescription::CONNECTION_WIRELESS &&
+ description.type == AudioDeviceType::OUT_HEARING_AID)) {
LOG(ERROR) << __func__ << ": unsupported device type: " << audioPort->toString();
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
}
CachedProxy proxy;
RETURN_STATUS_IF_ERROR(createProxy(*audioPort, nextPortId, proxy));
- // Since the device is already connected and configured by the BT stack, provide
- // the current configuration instead of all possible profiles.
+ // If the device is actually connected, it is configured by the BT stack.
+ // Provide the current configuration instead of all possible profiles.
const auto& pcmConfig = proxy.pcmConfig;
audioPort->profiles.clear();
audioPort->profiles.push_back(