diff --git a/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp b/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp index a2bb064dd0c250d08a829bb2881194d7af997c06..10b240ef139cd2b2bd32bdd774618d170f9ffde3 100644 --- a/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp +++ b/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp @@ -116,8 +116,12 @@ int32_t AudioA2dpManager::SetActiveA2dpDevice(const std::string& macAddress) a2dpInstance_ = A2dpSource::GetProfile(); CHECK_AND_RETURN_RET_LOG(a2dpInstance_ != nullptr, ERROR, "Failed to obtain A2DP profile instance"); BluetoothRemoteDevice device; - int32_t tmp = MediaBluetoothDeviceManager::GetConnectedA2dpBluetoothDevice(macAddress, device); - CHECK_AND_RETURN_RET_LOG(tmp == SUCCESS, ERROR, "SetActiveA2dpDevice: the configuring A2DP device doesn't exist."); + if (macAddress != "") { + int32_t tmp = MediaBluetoothDeviceManager::GetConnectedA2dpBluetoothDevice(macAddress, device); + CHECK_AND_RETURN_RET_LOG(tmp == SUCCESS, ERROR, "the configuring A2DP device doesn't exist."); + } else { + AUDIO_INFO_LOG("Deactive A2DP device"); + } int32_t ret = a2dpInstance_->SetActiveSinkDevice(device); CHECK_AND_RETURN_RET_LOG(ret == 0, ERROR, "SetActiveA2dpDevice failed. result: %{public}d", ret); activeA2dpDevice_ = device; diff --git a/services/audio_policy/server/src/service/audio_policy_service.cpp b/services/audio_policy/server/src/service/audio_policy_service.cpp index ec806ee992064093ee49b5a9a14c31a089744c7f..f61731d0c07abc3d92ecfd4b3cab7ab8a5819b8a 100644 --- a/services/audio_policy/server/src/service/audio_policy_service.cpp +++ b/services/audio_policy/server/src/service/audio_policy_service.cpp @@ -1607,10 +1607,10 @@ void AudioPolicyService::FetchOutputDeviceWhenNoRunningStream() return; } SetVolumeForSwitchDevice(desc->deviceType_); - currentActiveDevice_ = AudioDeviceDescriptor(*desc); - if (desc->deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP || desc->deviceType_ == DEVICE_TYPE_BLUETOOTH_SCO) { - activeBTDevice_ = desc->macAddress_; + if (desc->deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP) { + SwitchActiveA2dpDevice(new AudioDeviceDescriptor(*desc)); } + currentActiveDevice_ = AudioDeviceDescriptor(*desc); AUDIO_DEBUG_LOG("currentActiveDevice update %{public}d", currentActiveDevice_.deviceType_); OnPreferredOutputDeviceUpdated(currentActiveDevice_); } @@ -2795,6 +2795,9 @@ void AudioPolicyService::UpdateActiveA2dpDeviceWhenDisconnecting(const std::stri IOHandles_.erase(BLUETOOTH_SPEAKER); } audioPolicyManager_.SetAbsVolumeScene(false); +#ifdef BLUETOOTH_ENABLE + Bluetooth::AudioA2dpManager::SetActiveA2dpDevice(""); +#endif return; } } @@ -5400,6 +5403,12 @@ void AudioPolicyService::OnPreferredStateUpdated(AudioDeviceDescriptor &desc, userSelectRecordCaptureDevice->macAddress_ == desc.macAddress_) { audioStateManager_.SetPerferredRecordCaptureDevice(new(std::nothrow) AudioDeviceDescriptor()); } +#ifdef BLUETOOTH_ENABLE + if (desc.deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP && + desc.macAddress_ == currentActiveDevice_.macAddress_) { + Bluetooth::AudioA2dpManager::SetActiveA2dpDevice(""); + } +#endif } else { reason = AudioStreamDeviceChangeReason::NEW_DEVICE_AVAILABLE; if (desc.deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP) {