From 82f6d0af4117f1c959a404ef920ebec5f5e723a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=81=92=E7=94=B3?= Date: Thu, 11 Sep 2025 13:08:45 +0000 Subject: [PATCH] fix: switching nearlink needs disconnect sco MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 王恒申 Change-Id: Iadc7e78b0a0fe20ce7198bb900c1874fd0eec9db --- .../native/bluetoothclient/audio_bluetooth_manager.cpp | 2 +- .../test/bluetooth_hfp_manager_test.cpp | 1 + .../domain/device/src/sle/sle_audio_device_manager.cpp | 9 +++++---- .../service/service_main/include/audio_core_service.h | 2 +- .../service_main/src/audio_core_service_private.cpp | 10 +++++++--- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp b/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp index 318ba256c4..f9dbd7899a 100644 --- a/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp +++ b/frameworks/native/bluetoothclient/audio_bluetooth_manager.cpp @@ -465,7 +465,7 @@ int32_t AudioHfpManager::SetActiveHfpDevice(const std::string &macAddress) AudioXCollie audioXCollie("AudioHfpManager::SetActiveHfpDevice", BT_SET_ACTIVE_DEVICE_TIMEOUT, nullptr, nullptr, AUDIO_XCOLLIE_FLAG_LOG | AUDIO_XCOLLIE_FLAG_RECOVERY); BluetoothRemoteDevice device; - if (HfpBluetoothDeviceManager::GetConnectedHfpBluetoothDevice(macAddress, device) != SUCCESS) { + if (macAddress != "" && HfpBluetoothDeviceManager::GetConnectedHfpBluetoothDevice(macAddress, device) != SUCCESS) { AUDIO_ERR_LOG("SetActiveHfpDevice failed for the HFP device, %{public}s does not exist.", GetEncryptAddr(macAddress).c_str()); return ERROR; diff --git a/frameworks/native/bluetoothclient/test/bluetooth_hfp_manager_test.cpp b/frameworks/native/bluetoothclient/test/bluetooth_hfp_manager_test.cpp index dfae106e0c..ab75aac695 100644 --- a/frameworks/native/bluetoothclient/test/bluetooth_hfp_manager_test.cpp +++ b/frameworks/native/bluetoothclient/test/bluetooth_hfp_manager_test.cpp @@ -83,6 +83,7 @@ HWTEST_F(BluetoothHfpManagerTest, BluetoothHfpManagerTest_001, TestSize.Level1) EXPECT_NE(AudioHfpManager::SetActiveHfpDevice("33:33:33"), SUCCESS); EXPECT_EQ(AudioHfpManager::SetActiveHfpDevice(HFP_DEVICE_MAC1), SUCCESS); EXPECT_EQ(AudioHfpManager::SetActiveHfpDevice(HFP_DEVICE_MAC2), SUCCESS); + EXPECT_EQ(AudioHfpManager::SetActiveHfpDevice(""), SUCCESS); } /** diff --git a/services/audio_policy/server/domain/device/src/sle/sle_audio_device_manager.cpp b/services/audio_policy/server/domain/device/src/sle/sle_audio_device_manager.cpp index 0693521cdf..8f923bd5fe 100644 --- a/services/audio_policy/server/domain/device/src/sle/sle_audio_device_manager.cpp +++ b/services/audio_policy/server/domain/device/src/sle/sle_audio_device_manager.cpp @@ -100,7 +100,8 @@ int32_t SleAudioDeviceManager::StartPlaying(const std::string &device, uint32_t { CHECK_AND_RETURN_RET_LOG(callback_ != nullptr, ERR_INVALID_PARAM, "callback is nullptr"); - AUDIO_INFO_LOG("sle streamType %{public}u", streamType); + AUDIO_INFO_LOG("device [%{public}s] sle streamType [%{public}u]", + AudioPolicyUtils::GetInstance().GetEncryptAddr(device).c_str(), streamType); std::lock_guard lock(startedSleStreamTypeMutex_); int32_t ret = ERROR; if (!startedSleStreamType_[device][streamType].empty()) { @@ -116,7 +117,8 @@ int32_t SleAudioDeviceManager::StopPlaying(const std::string &device, uint32_t s CHECK_AND_RETURN_RET_LOG(callback_ != nullptr, ERR_INVALID_PARAM, "callback is nullptr"); int32_t ret = ERROR; - AUDIO_INFO_LOG("sle streamType %{public}u", streamType); + AUDIO_INFO_LOG("device [%{public}s] sle streamType [%{public}u]", + AudioPolicyUtils::GetInstance().GetEncryptAddr(device).c_str(), streamType); callback_->StopPlaying(device, streamType, ret); return ret; } @@ -385,8 +387,7 @@ void SleAudioDeviceManager::ResetSleStreamTypeCount(const std::shared_ptrmacAddress_, streamType); } - auto ret = SetActiveSinkDevice(deviceDesc->macAddress_, SLE_AUDIO_STREAM_NONE); - CHECK_AND_RETURN_LOG(ret == SUCCESS, "set active device failed, ret: %{public}d", ret); + SetActiveSinkDevice(deviceDesc->macAddress_, SLE_AUDIO_STREAM_NONE); startedSleStreamType_.erase(it); } diff --git a/services/audio_policy/server/service/service_main/include/audio_core_service.h b/services/audio_policy/server/service/service_main/include/audio_core_service.h index 031b12a4d3..22fc43399b 100644 --- a/services/audio_policy/server/service/service_main/include/audio_core_service.h +++ b/services/audio_policy/server/service/service_main/include/audio_core_service.h @@ -509,7 +509,7 @@ private: void UpdateRemoteOffloadModuleName(std::shared_ptr pipeInfo, std::string &moduleName); void UpdateOffloadState(std::shared_ptr pipeInfo); void NotifyRouteUpdate(const std::vector> &streamDescs); - void ResetNearlinkDeviceState(const std::shared_ptr &deviceDesc); + void ResetNearlinkDeviceState(const std::shared_ptr &deviceDesc, bool isRunning = true); // For offload void CheckAndUpdateOffloadEnableForStream( diff --git a/services/audio_policy/server/service/service_main/src/audio_core_service_private.cpp b/services/audio_policy/server/service/service_main/src/audio_core_service_private.cpp index 3adf00b374..5811f51c2c 100644 --- a/services/audio_policy/server/service/service_main/src/audio_core_service_private.cpp +++ b/services/audio_policy/server/service/service_main/src/audio_core_service_private.cpp @@ -555,7 +555,7 @@ int32_t AudioCoreService::BluetoothDeviceFetchOutputHandle(shared_ptr desc = streamDesc->newDeviceDescs_.front(); CHECK_AND_RETURN_RET_LOG(desc != nullptr, BLUETOOTH_FETCH_RESULT_CONTINUE, "Device desc is nullptr"); - ResetNearlinkDeviceState(desc); + ResetNearlinkDeviceState(desc, streamDesc->streamStatus_ == STREAM_STATUS_STARTED); if (desc->deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP) { std::string sinkPort = AudioPolicyUtils::GetInstance().GetSinkPortName(DEVICE_TYPE_BLUETOOTH_A2DP); @@ -2894,8 +2894,11 @@ bool AudioCoreService::IsFastAllowed(std::string &bundleName) return true; } -void AudioCoreService::ResetNearlinkDeviceState(const std::shared_ptr &deviceDesc) +void AudioCoreService::ResetNearlinkDeviceState(const std::shared_ptr &deviceDesc, + bool isRunning) { + CHECK_AND_RETURN(isRunning); + CHECK_AND_RETURN_LOG(deviceDesc != nullptr, "deviceDesc is nullptr"); auto currentOutputDevice = audioActiveDevice_.GetCurrentOutputDevice(); @@ -2945,10 +2948,11 @@ int32_t AudioCoreService::ActivateNearlinkDevice(const std::shared_ptr