1 /*
2 * Copyright (c) 2021-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #ifndef LOG_TAG
16 #define LOG_TAG "AudioBluetoothManager"
17 #endif
18
19 #include "audio_bluetooth_manager.h"
20 #include "bluetooth_def.h"
21 #include "audio_errors.h"
22 #include "audio_common_log.h"
23 #include "audio_utils.h"
24 #include "bluetooth_audio_manager.h"
25 #include "bluetooth_device_manager.h"
26 #include "bluetooth_device_utils.h"
27
28 namespace OHOS {
29 namespace Bluetooth {
30 using namespace AudioStandard;
31
32 const int32_t BT_VIRTUAL_DEVICE_ADD = 0;
33 const int32_t BT_VIRTUAL_DEVICE_REMOVE = 1;
34 constexpr const uint8_t CONN_REASON_MANUAL_VIRTUAL_CONNECT_PREEMPT_FLAG = 0x03;
35 A2dpSource *AudioA2dpManager::a2dpInstance_ = nullptr;
36 std::shared_ptr<AudioA2dpListener> AudioA2dpManager::a2dpListener_ = std::make_shared<AudioA2dpListener>();
37 int AudioA2dpManager::connectionState_ = static_cast<int>(BTConnectState::DISCONNECTED);
38 int32_t AudioA2dpManager::captureConnectionState_ = static_cast<int32_t>(BTHdapConnectState::DISCONNECTED);
39 BluetoothRemoteDevice AudioA2dpManager::activeA2dpDevice_;
40 std::mutex g_a2dpInstanceLock;
41 HandsFreeAudioGateway *AudioHfpManager::hfpInstance_ = nullptr;
42 std::shared_ptr<AudioHfpListener> AudioHfpManager::hfpListener_ = std::make_shared<AudioHfpListener>();
43 AudioScene AudioHfpManager::scene_ = AUDIO_SCENE_DEFAULT;
44 AudioScene AudioHfpManager::sceneFromPolicy_ = AUDIO_SCENE_DEFAULT;
45 OHOS::Bluetooth::ScoCategory AudioHfpManager::scoCategory = OHOS::Bluetooth::ScoCategory::SCO_DEFAULT;
46 OHOS::Bluetooth::RecognitionStatus AudioHfpManager::recognitionStatus =
47 OHOS::Bluetooth::RecognitionStatus::RECOGNITION_DISCONNECTED;
48 bool AudioHfpManager::isVirtualCall = true;
49 BluetoothRemoteDevice AudioHfpManager::activeHfpDevice_;
50 std::vector<std::shared_ptr<AudioA2dpPlayingStateChangedListener>> AudioA2dpManager::a2dpPlayingStateChangedListeners_;
51 std::mutex g_activehfpDeviceLock;
52 std::mutex g_audioSceneLock;
53 std::mutex g_hfpInstanceLock;
54 std::mutex g_a2dpPlayingStateChangedLock;
55 static const int32_t BT_SET_ACTIVE_DEVICE_TIMEOUT = 8; //BtService SetActiveDevice 8s timeout
56
GetAudioStreamInfo(A2dpCodecInfo codecInfo,AudioStreamInfo & audioStreamInfo)57 static bool GetAudioStreamInfo(A2dpCodecInfo codecInfo, AudioStreamInfo &audioStreamInfo)
58 {
59 AUDIO_DEBUG_LOG("codec info rate[%{public}d] format[%{public}d] channel[%{public}d]",
60 codecInfo.sampleRate, codecInfo.bitsPerSample, codecInfo.channelMode);
61 switch (codecInfo.sampleRate) {
62 case A2DP_SBC_SAMPLE_RATE_48000_USER:
63 case A2DP_L2HCV2_SAMPLE_RATE_48000_USER:
64 audioStreamInfo.samplingRate = SAMPLE_RATE_48000;
65 break;
66 case A2DP_SBC_SAMPLE_RATE_44100_USER:
67 audioStreamInfo.samplingRate = SAMPLE_RATE_44100;
68 break;
69 case A2DP_SBC_SAMPLE_RATE_32000_USER:
70 audioStreamInfo.samplingRate = SAMPLE_RATE_32000;
71 break;
72 case A2DP_SBC_SAMPLE_RATE_16000_USER:
73 audioStreamInfo.samplingRate = SAMPLE_RATE_16000;
74 break;
75 case A2DP_L2HCV2_SAMPLE_RATE_96000_USER:
76 audioStreamInfo.samplingRate = SAMPLE_RATE_96000;
77 break;
78 default:
79 return false;
80 }
81 switch (codecInfo.bitsPerSample) {
82 case A2DP_SAMPLE_BITS_16_USER:
83 audioStreamInfo.format = SAMPLE_S16LE;
84 break;
85 case A2DP_SAMPLE_BITS_24_USER:
86 audioStreamInfo.format = SAMPLE_S24LE;
87 break;
88 case A2DP_SAMPLE_BITS_32_USER:
89 audioStreamInfo.format = SAMPLE_S32LE;
90 break;
91 default:
92 return false;
93 }
94 switch (codecInfo.channelMode) {
95 case A2DP_SBC_CHANNEL_MODE_STEREO_USER:
96 audioStreamInfo.channels = STEREO;
97 break;
98 case A2DP_SBC_CHANNEL_MODE_MONO_USER:
99 audioStreamInfo.channels = MONO;
100 break;
101 default:
102 return false;
103 }
104 audioStreamInfo.encoding = ENCODING_PCM;
105 return true;
106 }
107
108 // LCOV_EXCL_START
RegisterBluetoothA2dpListener()109 void AudioA2dpManager::RegisterBluetoothA2dpListener()
110 {
111 AUDIO_INFO_LOG("AudioA2dpManager::RegisterBluetoothA2dpListener");
112 std::lock_guard<std::mutex> a2dpLock(g_a2dpInstanceLock);
113 a2dpInstance_ = A2dpSource::GetProfile();
114 CHECK_AND_RETURN_LOG(a2dpInstance_ != nullptr, "Failed to obtain A2DP profile instance");
115 a2dpInstance_->RegisterObserver(a2dpListener_);
116 }
117
UnregisterBluetoothA2dpListener()118 void AudioA2dpManager::UnregisterBluetoothA2dpListener()
119 {
120 AUDIO_INFO_LOG("AudioA2dpManager::UnregisterBluetoothA2dpListener");
121 std::lock_guard<std::mutex> a2dpLock(g_a2dpInstanceLock);
122 CHECK_AND_RETURN_LOG(a2dpInstance_ != nullptr, "A2DP profile instance unavailable");
123
124 a2dpInstance_->DeregisterObserver(a2dpListener_);
125 a2dpInstance_ = nullptr;
126 }
127
DisconnectBluetoothA2dpSink()128 void AudioA2dpManager::DisconnectBluetoothA2dpSink()
129 {
130 int connectionState = static_cast<int>(BTConnectState::DISCONNECTED);
131 auto a2dpList = MediaBluetoothDeviceManager::GetAllA2dpBluetoothDevice();
132 for (const auto &device : a2dpList) {
133 a2dpListener_->OnConnectionStateChanged(device, connectionState,
134 static_cast<uint32_t>(ConnChangeCause::CONNECT_CHANGE_COMMON_CAUSE));
135 }
136
137 auto virtualDevices = MediaBluetoothDeviceManager::GetA2dpVirtualDeviceList();
138 for (const auto &virtualDevice : virtualDevices) {
139 a2dpListener_->OnVirtualDeviceChanged(static_cast<int32_t>(Bluetooth::BT_VIRTUAL_DEVICE_REMOVE),
140 virtualDevice.GetDeviceAddr());
141 }
142
143 MediaBluetoothDeviceManager::ClearAllA2dpBluetoothDevice();
144 }
145
DisconnectBluetoothA2dpSource()146 void AudioA2dpManager::DisconnectBluetoothA2dpSource()
147 {
148 int captureConnectionState = static_cast<int>(BTHdapConnectState::DISCONNECTED);
149 auto a2dpInList = A2dpInBluetoothDeviceManager::GetAllA2dpInBluetoothDevice();
150 A2dpCodecInfo defaultCodecInfo = {};
151 for (const auto &device : a2dpInList) {
152 a2dpListener_->OnCaptureConnectionStateChanged(device, captureConnectionState, defaultCodecInfo);
153 }
154 A2dpInBluetoothDeviceManager::ClearAllA2dpInBluetoothDevice();
155 A2dpInBluetoothDeviceManager::ClearAllA2dpInStreamInfo();
156 }
157
SetActiveA2dpDevice(const std::string & macAddress)158 int32_t AudioA2dpManager::SetActiveA2dpDevice(const std::string& macAddress)
159 {
160 std::lock_guard<std::mutex> a2dpLock(g_a2dpInstanceLock);
161 AUDIO_WARNING_LOG("incoming device:%{public}s, current device:%{public}s",
162 GetEncryptAddr(macAddress).c_str(), GetEncryptAddr(activeA2dpDevice_.GetDeviceAddr()).c_str());
163 a2dpInstance_ = A2dpSource::GetProfile();
164 CHECK_AND_RETURN_RET_LOG(a2dpInstance_ != nullptr, ERROR, "Failed to obtain A2DP profile instance");
165 BluetoothRemoteDevice device;
166 if (macAddress != "") {
167 int32_t tmp = MediaBluetoothDeviceManager::GetConnectedA2dpBluetoothDevice(macAddress, device);
168 CHECK_AND_RETURN_RET_LOG(tmp == SUCCESS, ERROR, "the configuring A2DP device doesn't exist.");
169 } else {
170 AUDIO_INFO_LOG("Deactive A2DP device");
171 }
172 int32_t ret = a2dpInstance_->SetActiveSinkDevice(device);
173 CHECK_AND_RETURN_RET_LOG(ret == 0, ERROR, "SetActiveA2dpDevice failed. result: %{public}d", ret);
174 activeA2dpDevice_ = device;
175 return SUCCESS;
176 }
177
GetActiveA2dpDevice()178 std::string AudioA2dpManager::GetActiveA2dpDevice()
179 {
180 std::lock_guard<std::mutex> a2dpLock(g_a2dpInstanceLock);
181 a2dpInstance_ = A2dpSource::GetProfile();
182 CHECK_AND_RETURN_RET_LOG(a2dpInstance_ != nullptr, "", "Failed to obtain A2DP profile instance");
183 BluetoothRemoteDevice device = a2dpInstance_->GetActiveSinkDevice();
184 return device.GetDeviceAddr();
185 }
186
SetDeviceAbsVolume(const std::string & macAddress,int32_t volume)187 int32_t AudioA2dpManager::SetDeviceAbsVolume(const std::string& macAddress, int32_t volume)
188 {
189 BluetoothRemoteDevice device;
190 int32_t ret = MediaBluetoothDeviceManager::GetConnectedA2dpBluetoothDevice(macAddress, device);
191 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERROR, "SetDeviceAbsVolume: the configuring A2DP device doesn't exist.");
192 return AvrcpTarget::GetProfile()->SetDeviceAbsoluteVolume(device, volume);
193 }
194
GetA2dpDeviceStreamInfo(const std::string & macAddress,AudioStreamInfo & streamInfo)195 int32_t AudioA2dpManager::GetA2dpDeviceStreamInfo(const std::string& macAddress,
196 AudioStreamInfo &streamInfo)
197 {
198 std::lock_guard<std::mutex> a2dpLock(g_a2dpInstanceLock);
199 a2dpInstance_ = A2dpSource::GetProfile();
200 CHECK_AND_RETURN_RET_LOG(a2dpInstance_ != nullptr, ERROR, "Failed to obtain A2DP profile instance");
201 BluetoothRemoteDevice device;
202 int32_t ret = MediaBluetoothDeviceManager::GetConnectedA2dpBluetoothDevice(macAddress, device);
203 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERROR,
204 "GetA2dpDeviceStreamInfo: the configuring A2DP device doesn't exist.");
205 A2dpCodecStatus codecStatus = a2dpInstance_->GetCodecStatus(device);
206 bool result = GetAudioStreamInfo(codecStatus.codecInfo, streamInfo);
207 CHECK_AND_RETURN_RET_LOG(result, ERROR, "GetA2dpDeviceStreamInfo: Unsupported a2dp codec info");
208 return SUCCESS;
209 }
210
GetA2dpInDeviceStreamInfo(const std::string & macAddress,AudioStreamInfo & streamInfo)211 int32_t AudioA2dpManager::GetA2dpInDeviceStreamInfo(const std::string &macAddress,
212 AudioStreamInfo &streamInfo)
213 {
214 bool ret = A2dpInBluetoothDeviceManager::GetA2dpInDeviceStreamInfo(macAddress, streamInfo);
215 CHECK_AND_RETURN_RET_LOG(ret == true, ERROR, "the StreamInfo of the a2dp input device doesn't exist.");
216 return SUCCESS;
217 }
218
HasA2dpDeviceConnected()219 bool AudioA2dpManager::HasA2dpDeviceConnected()
220 {
221 a2dpInstance_ = A2dpSource::GetProfile();
222 CHECK_AND_RETURN_RET(a2dpInstance_, false);
223 std::vector<int32_t> states {static_cast<int32_t>(BTConnectState::CONNECTED)};
224 std::vector<BluetoothRemoteDevice> devices;
225 a2dpInstance_->GetDevicesByStates(states, devices);
226
227 return !devices.empty();
228 }
229
A2dpOffloadSessionRequest(const std::vector<A2dpStreamInfo> & info)230 int32_t AudioA2dpManager::A2dpOffloadSessionRequest(const std::vector<A2dpStreamInfo> &info)
231 {
232 CHECK_AND_RETURN_RET_LOG(activeA2dpDevice_.GetDeviceAddr() != "00:00:00:00:00:00", A2DP_NOT_OFFLOAD,
233 "Invalid mac address, not request, return A2DP_NOT_OFFLOAD.");
234 int32_t ret = a2dpInstance_->A2dpOffloadSessionRequest(activeA2dpDevice_, info);
235 AUDIO_DEBUG_LOG("Request %{public}zu stream and return a2dp offload state %{public}d", info.size(), ret);
236 return ret;
237 }
238
OffloadStartPlaying(const std::vector<int32_t> & sessionsID)239 int32_t AudioA2dpManager::OffloadStartPlaying(const std::vector<int32_t> &sessionsID)
240 {
241 CHECK_AND_RETURN_RET_LOG(activeA2dpDevice_.GetDeviceAddr() != "00:00:00:00:00:00", ERROR,
242 "Invalid mac address, not start, return error.");
243 AUDIO_DEBUG_LOG("Start playing %{public}zu stream", sessionsID.size());
244 return a2dpInstance_->OffloadStartPlaying(activeA2dpDevice_, sessionsID);
245 }
246
OffloadStopPlaying(const std::vector<int32_t> & sessionsID)247 int32_t AudioA2dpManager::OffloadStopPlaying(const std::vector<int32_t> &sessionsID)
248 {
249 if (activeA2dpDevice_.GetDeviceAddr() == "00:00:00:00:00:00") {
250 AUDIO_DEBUG_LOG("Invalid mac address, not stop, return error.");
251 return ERROR;
252 }
253 AUDIO_DEBUG_LOG("Stop playing %{public}zu stream", sessionsID.size());
254 return a2dpInstance_->OffloadStopPlaying(activeA2dpDevice_, sessionsID);
255 }
256
GetRenderPosition(uint32_t & delayValue,uint64_t & sendDataSize,uint32_t & timeStamp)257 int32_t AudioA2dpManager::GetRenderPosition(uint32_t &delayValue, uint64_t &sendDataSize, uint32_t &timeStamp)
258 {
259 if (activeA2dpDevice_.GetDeviceAddr() == "00:00:00:00:00:00") {
260 AUDIO_DEBUG_LOG("Invalid mac address, return error.");
261 return ERROR;
262 }
263 return a2dpInstance_->GetRenderPosition(activeA2dpDevice_, delayValue, sendDataSize, timeStamp);
264 }
265
RegisterA2dpPlayingStateChangedListener(std::shared_ptr<AudioA2dpPlayingStateChangedListener> listener)266 int32_t AudioA2dpManager::RegisterA2dpPlayingStateChangedListener(
267 std::shared_ptr<AudioA2dpPlayingStateChangedListener> listener)
268 {
269 std::lock_guard<std::mutex> lock(g_a2dpPlayingStateChangedLock);
270 a2dpPlayingStateChangedListeners_.push_back(listener);
271 return SUCCESS;
272 }
273
OnA2dpPlayingStateChanged(const std::string & deviceAddress,int32_t playingState)274 void AudioA2dpManager::OnA2dpPlayingStateChanged(const std::string &deviceAddress, int32_t playingState)
275 {
276 std::lock_guard<std::mutex> lock(g_a2dpPlayingStateChangedLock);
277 for (auto listener : a2dpPlayingStateChangedListeners_) {
278 listener->OnA2dpPlayingStateChanged(deviceAddress, playingState);
279 }
280 }
281
CheckA2dpDeviceReconnect()282 void AudioA2dpManager::CheckA2dpDeviceReconnect()
283 {
284 if (a2dpInstance_ == nullptr) {
285 a2dpInstance_ = A2dpSource::GetProfile();
286 }
287 CHECK_AND_RETURN_LOG(a2dpInstance_ != nullptr, "A2DP profile instance unavailable");
288 std::vector<int32_t> states {static_cast<int32_t>(BTConnectState::CONNECTED)};
289 std::vector<BluetoothRemoteDevice> devices;
290 a2dpInstance_->GetDevicesByStates(states, devices);
291
292 for (auto &device : devices) {
293 a2dpListener_->OnConnectionStateChanged(device, static_cast<int32_t>(BTConnectState::CONNECTED),
294 static_cast<uint32_t>(ConnChangeCause::CONNECT_CHANGE_COMMON_CAUSE));
295
296 int32_t wearState = 0; // 0 unwear state
297 if (IsBTWearDetectionEnable(device)) {
298 wearState = BluetoothAudioManager::GetInstance().IsDeviceWearing(device);
299 if (wearState == 1) MediaBluetoothDeviceManager::SetMediaStack(device, WEAR_ACTION); // 1 wear state
300 }
301 AUDIO_WARNING_LOG("reconnect a2dp device:%{public}s, wear state:%{public}d",
302 GetEncryptAddr(device.GetDeviceAddr()).c_str(), wearState);
303 }
304
305 std::vector<std::string> virtualDevices;
306 a2dpInstance_->GetVirtualDeviceList(virtualDevices);
307 for (auto &macAddress : virtualDevices) {
308 AUDIO_WARNING_LOG("reconnect virtual a2dp device:%{public}s", GetEncryptAddr(macAddress).c_str());
309 a2dpListener_->OnVirtualDeviceChanged(static_cast<int32_t>(Bluetooth::BT_VIRTUAL_DEVICE_ADD), macAddress);
310 }
311 }
312
Connect(const std::string & macAddress)313 int32_t AudioA2dpManager::Connect(const std::string &macAddress)
314 {
315 CHECK_AND_RETURN_RET_LOG(a2dpInstance_ != nullptr, ERROR, "A2DP profile instance unavailable");
316 BluetoothRemoteDevice virtualDevice = BluetoothRemoteDevice(macAddress);
317 if (MediaBluetoothDeviceManager::IsA2dpBluetoothDeviceConnecting(macAddress)) {
318 AUDIO_WARNING_LOG("A2dp device %{public}s is connecting, ignore connect request",
319 GetEncryptAddr(macAddress).c_str());
320 virtualDevice.SetVirtualAutoConnectType(CONN_REASON_MANUAL_VIRTUAL_CONNECT_PREEMPT_FLAG, 0);
321 return SUCCESS;
322 }
323 std::vector<std::string> virtualDevices;
324 a2dpInstance_->GetVirtualDeviceList(virtualDevices);
325 if (std::find(virtualDevices.begin(), virtualDevices.end(), macAddress) == virtualDevices.end()) {
326 AUDIO_WARNING_LOG("A2dp device %{public}s is not virtual device, ignore connect request",
327 GetEncryptAddr(macAddress).c_str());
328 return SUCCESS;
329 }
330 int32_t ret = a2dpInstance_->Connect(virtualDevice);
331 CHECK_AND_RETURN_RET_LOG(ret == 0, ERROR, "A2dp Connect Failed");
332 virtualDevice.SetVirtualAutoConnectType(CONN_REASON_MANUAL_VIRTUAL_CONNECT_PREEMPT_FLAG, 0);
333 return SUCCESS;
334 }
335
OnConnectionStateChanged(const BluetoothRemoteDevice & device,int state,int cause)336 void AudioA2dpListener::OnConnectionStateChanged(const BluetoothRemoteDevice &device, int state, int cause)
337 {
338 AUDIO_WARNING_LOG("state: %{public}d, macAddress: %{public}s", state,
339 GetEncryptAddr(device.GetDeviceAddr()).c_str());
340 // Record connection state and device for hdi start time to check
341 AudioA2dpManager::SetConnectionState(state);
342 if (state == static_cast<int>(BTConnectState::CONNECTING)) {
343 MediaBluetoothDeviceManager::SetMediaStack(device, BluetoothDeviceAction::CONNECTING_ACTION);
344 }
345 if (state == static_cast<int>(BTConnectState::CONNECTED)) {
346 MediaBluetoothDeviceManager::SetMediaStack(device, BluetoothDeviceAction::CONNECT_ACTION);
347 }
348 if (state == static_cast<int>(BTConnectState::DISCONNECTED)) {
349 MediaBluetoothDeviceManager::SetMediaStack(device, BluetoothDeviceAction::DISCONNECT_ACTION);
350 }
351 }
352
OnConfigurationChanged(const BluetoothRemoteDevice & device,const A2dpCodecInfo & codecInfo,int error)353 void AudioA2dpListener::OnConfigurationChanged(const BluetoothRemoteDevice &device, const A2dpCodecInfo &codecInfo,
354 int error)
355 {
356 AUDIO_INFO_LOG("OnConfigurationChanged: sampleRate: %{public}d, channels: %{public}d, format: %{public}d",
357 codecInfo.sampleRate, codecInfo.channelMode, codecInfo.bitsPerSample);
358 AudioStreamInfo streamInfo = {};
359 bool result = GetAudioStreamInfo(codecInfo, streamInfo);
360 CHECK_AND_RETURN_LOG(result, "OnConfigurationChanged: Unsupported a2dp codec info");
361 MediaBluetoothDeviceManager::UpdateA2dpDeviceConfiguration(device, streamInfo);
362 }
363
OnPlayingStatusChanged(const BluetoothRemoteDevice & device,int playingState,int error)364 void AudioA2dpListener::OnPlayingStatusChanged(const BluetoothRemoteDevice &device, int playingState, int error)
365 {
366 AUDIO_INFO_LOG("OnPlayingStatusChanged, state: %{public}d, error: %{public}d", playingState, error);
367 if (error == SUCCESS) {
368 AudioA2dpManager::OnA2dpPlayingStateChanged(device.GetDeviceAddr(), playingState);
369 }
370 }
371
OnMediaStackChanged(const BluetoothRemoteDevice & device,int action)372 void AudioA2dpListener::OnMediaStackChanged(const BluetoothRemoteDevice &device, int action)
373 {
374 AUDIO_WARNING_LOG("action: %{public}d, macAddress: %{public}s", action,
375 GetEncryptAddr(device.GetDeviceAddr()).c_str());
376 MediaBluetoothDeviceManager::SetMediaStack(device, action);
377 }
378
OnVirtualDeviceChanged(int32_t action,std::string macAddress)379 void AudioA2dpListener::OnVirtualDeviceChanged(int32_t action, std::string macAddress)
380 {
381 AUDIO_WARNING_LOG("action: %{public}d, macAddress: %{public}s", action, GetEncryptAddr(macAddress).c_str());
382 if (action == static_cast<int32_t>(Bluetooth::BT_VIRTUAL_DEVICE_ADD)) {
383 MediaBluetoothDeviceManager::SetMediaStack(BluetoothRemoteDevice(macAddress),
384 BluetoothDeviceAction::VIRTUAL_DEVICE_ADD_ACTION);
385 }
386 if (action == static_cast<int32_t>(Bluetooth::BT_VIRTUAL_DEVICE_REMOVE)) {
387 MediaBluetoothDeviceManager::SetMediaStack(BluetoothRemoteDevice(macAddress),
388 BluetoothDeviceAction::VIRTUAL_DEVICE_REMOVE_ACTION);
389 }
390 }
391
OnCaptureConnectionStateChanged(const BluetoothRemoteDevice & device,int state,const A2dpCodecInfo & codecInfo)392 void AudioA2dpListener::OnCaptureConnectionStateChanged(const BluetoothRemoteDevice &device, int state,
393 const A2dpCodecInfo &codecInfo)
394 {
395 AUDIO_INFO_LOG("capture connection state: %{public}d", state);
396 AudioA2dpManager::SetCaptureConnectionState(static_cast<int32_t>(state));
397 AudioStreamInfo streamInfo = {};
398 if (state == static_cast<int>(BTHdapConnectState::CONNECTED)) {
399 AUDIO_INFO_LOG("A2dpInCodecInfo: sampleRate: %{public}d, channels: %{public}d, format: %{public}d",
400 codecInfo.sampleRate, codecInfo.channelMode, codecInfo.bitsPerSample);
401 bool result = GetAudioStreamInfo(codecInfo, streamInfo);
402 CHECK_AND_RETURN_LOG(result == true, "Unsupported a2dpIn codec info");
403 A2dpInBluetoothDeviceManager::SetA2dpInStack(device, streamInfo, BluetoothDeviceAction::CONNECT_ACTION);
404 } else if (state == static_cast<int>(BTHdapConnectState::DISCONNECTED)) {
405 A2dpInBluetoothDeviceManager::SetA2dpInStack(device, streamInfo, BluetoothDeviceAction::DISCONNECT_ACTION);
406 }
407 }
408
RegisterBluetoothScoListener()409 void AudioHfpManager::RegisterBluetoothScoListener()
410 {
411 AUDIO_INFO_LOG("AudioHfpManager::RegisterBluetoothScoListener");
412 std::lock_guard<std::mutex> hfpLock(g_hfpInstanceLock);
413 hfpInstance_ = HandsFreeAudioGateway::GetProfile();
414 CHECK_AND_RETURN_LOG(hfpInstance_ != nullptr, "Failed to obtain HFP AG profile instance");
415 hfpInstance_->RegisterObserver(hfpListener_);
416 }
417
UnregisterBluetoothScoListener()418 void AudioHfpManager::UnregisterBluetoothScoListener()
419 {
420 AUDIO_INFO_LOG("AudioHfpManager::UnregisterBluetoothScoListene");
421 std::lock_guard<std::mutex> hfpLock(g_hfpInstanceLock);
422 CHECK_AND_RETURN_LOG(hfpInstance_ != nullptr, "HFP AG profile instance unavailable");
423
424 hfpInstance_->DeregisterObserver(hfpListener_);
425 hfpInstance_ = nullptr;
426 }
427
CheckHfpDeviceReconnect()428 void AudioHfpManager::CheckHfpDeviceReconnect()
429 {
430 if (hfpInstance_ == nullptr) {
431 hfpInstance_ = HandsFreeAudioGateway::GetProfile();
432 }
433 CHECK_AND_RETURN_LOG(hfpInstance_ != nullptr, "HFP profile instance unavailable");
434 std::vector<int32_t> states {static_cast<int32_t>(BTConnectState::CONNECTED)};
435 std::vector<BluetoothRemoteDevice> devices = hfpInstance_->GetDevicesByStates(states);
436 for (auto &device : devices) {
437 hfpListener_->OnConnectionStateChanged(device, static_cast<int32_t>(BTConnectState::CONNECTED),
438 static_cast<uint32_t>(ConnChangeCause::CONNECT_CHANGE_COMMON_CAUSE));
439
440 int32_t wearState = 0; // 0 unwear state
441 if (IsBTWearDetectionEnable(device)) {
442 wearState = BluetoothAudioManager::GetInstance().IsDeviceWearing(device);
443 if (wearState == 1) HfpBluetoothDeviceManager::SetHfpStack(device, WEAR_ACTION); // 1 wear state
444 }
445 AUDIO_INFO_LOG("reconnect hfp device:%{public}s, wear state:%{public}d",
446 GetEncryptAddr(device.GetDeviceAddr()).c_str(), wearState);
447 }
448
449 std::vector<std::string> virtualDevices;
450 hfpInstance_->GetVirtualDeviceList(virtualDevices);
451 for (auto &macAddress : virtualDevices) {
452 AUDIO_PRERELEASE_LOGI("reconnect virtual hfp device:%{public}s", GetEncryptAddr(macAddress).c_str());
453 hfpListener_->OnVirtualDeviceChanged(static_cast<int32_t>(Bluetooth::BT_VIRTUAL_DEVICE_ADD), macAddress);
454 }
455 }
456
HandleScoWithRecongnition(bool handleFlag,BluetoothRemoteDevice & device)457 int32_t AudioHfpManager::HandleScoWithRecongnition(bool handleFlag, BluetoothRemoteDevice &device)
458 {
459 CHECK_AND_RETURN_RET_LOG(hfpInstance_ != nullptr, ERROR, "HFP AG profile instance unavailable");
460 bool ret = true;
461 if (handleFlag) {
462 int8_t scoCategory = GetScoCategoryFromScene(scene_);
463 if (scoCategory == ScoCategory::SCO_DEFAULT &&
464 AudioHfpManager::scoCategory != ScoCategory::SCO_RECOGNITION) {
465 AUDIO_INFO_LOG("Recongnition sco connect");
466 AudioHfpManager::recognitionStatus = RecognitionStatus::RECOGNITION_CONNECTING;
467 ret = BluetoothScoManager::HandleScoConnect(ScoCategory::SCO_RECOGNITION, &device);
468 if (ret == SUCCESS) {
469 AudioHfpManager::scoCategory = ScoCategory::SCO_RECOGNITION;
470 AudioHfpManager::recognitionStatus = RecognitionStatus::RECOGNITION_CONNECTED;
471 }
472 } else {
473 AUDIO_WARNING_LOG("Sco Connected OR Connecting, No Need to Create");
474 }
475 } else {
476 if (AudioHfpManager::scoCategory == ScoCategory::SCO_RECOGNITION) {
477 AUDIO_INFO_LOG("Recongnition sco close");
478 AudioHfpManager::recognitionStatus = RecognitionStatus::RECOGNITION_DISCONNECTING;
479 ret = BluetoothScoManager::HandleScoDisconnect(ScoCategory::SCO_RECOGNITION, &device);
480 if (ret == SUCCESS) {
481 AudioHfpManager::scoCategory = ScoCategory::SCO_DEFAULT;
482 AudioHfpManager::recognitionStatus = RecognitionStatus::RECOGNITION_DISCONNECTED;
483 }
484 }
485 }
486 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERROR, "HandleScoWithRecongnition failed, result: %{public}d", ret);
487 return SUCCESS;
488 }
489
ClearRecongnitionStatus()490 void AudioHfpManager::ClearRecongnitionStatus()
491 {
492 if (AudioHfpManager::scoCategory == ScoCategory::SCO_RECOGNITION) {
493 AudioHfpManager::scoCategory = ScoCategory::SCO_DEFAULT;
494 AudioHfpManager::recognitionStatus = RecognitionStatus::RECOGNITION_DISCONNECTED;
495 AUDIO_WARNING_LOG("Recognition sco status has been cleared.");
496 }
497 }
498
GetScoCategory()499 ScoCategory AudioHfpManager::GetScoCategory()
500 {
501 return scoCategory;
502 }
503
GetRecognitionStatus()504 RecognitionStatus AudioHfpManager::GetRecognitionStatus()
505 {
506 return recognitionStatus;
507 }
508
SetActiveHfpDevice(const std::string & macAddress)509 int32_t AudioHfpManager::SetActiveHfpDevice(const std::string &macAddress)
510 {
511 AudioXCollie audioXCollie("AudioHfpManager::SetActiveHfpDevice", BT_SET_ACTIVE_DEVICE_TIMEOUT,
512 nullptr, nullptr, AUDIO_XCOLLIE_FLAG_LOG | AUDIO_XCOLLIE_FLAG_RECOVERY);
513 BluetoothRemoteDevice device;
514 if (HfpBluetoothDeviceManager::GetConnectedHfpBluetoothDevice(macAddress, device) != SUCCESS) {
515 AUDIO_ERR_LOG("SetActiveHfpDevice failed for the HFP device %{public}s does not exist.",
516 GetEncryptAddr(macAddress).c_str());
517 return ERROR;
518 }
519 std::lock_guard<std::mutex> hfpDeviceLock(g_activehfpDeviceLock);
520 AUDIO_INFO_LOG("incoming device:%{public}s, current device:%{public}s",
521 GetEncryptAddr(macAddress).c_str(), GetEncryptAddr(activeHfpDevice_.GetDeviceAddr()).c_str());
522 if (macAddress != activeHfpDevice_.GetDeviceAddr()) {
523 AUDIO_WARNING_LOG("Active hfp device is changed, need to DisconnectSco for current activeHfpDevice.");
524 int32_t ret = DisconnectSco();
525 CHECK_AND_RETURN_RET_LOG(ret == 0, ERROR, "DisconnectSco failed, result: %{public}d", ret);
526 }
527 std::lock_guard<std::mutex> hfpLock(g_hfpInstanceLock);
528 CHECK_AND_RETURN_RET_LOG(hfpInstance_ != nullptr, ERROR, "HFP AG profile instance unavailable");
529 bool res = hfpInstance_->SetActiveDevice(device);
530 CHECK_AND_RETURN_RET_LOG(res == true, ERROR, "SetActiveHfpDevice failed, result: %{public}d", res);
531 activeHfpDevice_ = device;
532 return SUCCESS;
533 }
534
GetActiveHfpDevice()535 std::string AudioHfpManager::GetActiveHfpDevice()
536 {
537 std::lock_guard<std::mutex> hfpLock(g_hfpInstanceLock);
538 CHECK_AND_RETURN_RET_LOG(hfpInstance_ != nullptr, "", "HFP AG profile instance unavailable");
539 BluetoothRemoteDevice device = hfpInstance_->GetActiveDevice();
540 return device.GetDeviceAddr();
541 }
542
ConnectScoUponDefaultScene(int8_t category)543 int32_t AudioHfpManager::ConnectScoUponDefaultScene(int8_t category)
544 {
545 if (category != ScoCategory::SCO_DEFAULT) {
546 return SUCCESS;
547 }
548 CHECK_AND_RETURN_RET_LOG(hfpInstance_ != nullptr, ERROR, "HFP AG profile instance unavailable");
549 int32_t ret = BluetoothScoManager::HandleScoConnect(ScoCategory::SCO_VIRTUAL);
550 CHECK_AND_RETURN_RET_LOG(ret == 0, ERROR, "ConnectSco failed, result: %{public}d", ret);
551 return ret;
552 }
553
ConnectScoWithAudioScene(AudioScene scene)554 int32_t AudioHfpManager::ConnectScoWithAudioScene(AudioScene scene)
555 {
556 if (scoCategory == ScoCategory::SCO_RECOGNITION) {
557 AUDIO_WARNING_LOG("Recognition Sco Connected");
558 return SUCCESS;
559 }
560
561 std::lock_guard<std::mutex> sceneLock(g_audioSceneLock);
562 int8_t lastScoCategory = GetScoCategoryFromScene(scene_);
563 int8_t newScoCategory = GetScoCategoryFromScene(scene);
564 AUDIO_INFO_LOG("new sco category is %{public}d, last sco category is %{public}d", newScoCategory, lastScoCategory);
565
566 int32_t ret = ConnectScoUponDefaultScene(newScoCategory); // default scene need support bluetooth sco
567 if (lastScoCategory == newScoCategory) {
568 AUDIO_INFO_LOG("sco category %{public}d not change", newScoCategory);
569 return SUCCESS;
570 }
571 std::lock_guard<std::mutex> hfpLock(g_hfpInstanceLock);
572 CHECK_AND_RETURN_RET_LOG(hfpInstance_ != nullptr, ERROR, "HFP AG profile instance unavailable");
573 bool isInbardingEnabled = false;
574 hfpInstance_->IsInbandRingingEnabled(isInbardingEnabled);
575 if ((scene == AUDIO_SCENE_RINGING || scene == AUDIO_SCENE_VOICE_RINGING) && !isInbardingEnabled) {
576 AUDIO_WARNING_LOG("The inbarding switch is off, ignore the ring scene.");
577 return SUCCESS;
578 }
579 if (lastScoCategory != ScoCategory::SCO_DEFAULT) {
580 AUDIO_INFO_LOG("Entered to disConnectSco for last audioScene category.");
581 if (!IsVirtualCall()) {
582 AUDIO_INFO_LOG("voip change to call disconnect sco.");
583 ret = BluetoothScoManager::HandleScoDisconnect(ScoCategory::SCO_CALLULAR);
584 SetVirtualCall(true);
585 } else {
586 ret = BluetoothScoManager::HandleScoDisconnect(static_cast<ScoCategory> (lastScoCategory));
587 }
588 CHECK_AND_RETURN_RET_LOG(ret == 0, ERROR,
589 "ConnectScoWithAudioScene failed as the last SCO failed to be disconnected, result: %{public}d", ret);
590 }
591 if (newScoCategory != ScoCategory::SCO_DEFAULT) {
592 AUDIO_INFO_LOG("Entered to connectSco for new audioScene category.");
593 if (newScoCategory == ScoCategory::SCO_VIRTUAL && !IsVirtualCall()) {
594 AUDIO_INFO_LOG("voip change to call connect sco.");
595 ret = BluetoothScoManager::HandleScoConnect(ScoCategory::SCO_CALLULAR);
596 } else {
597 ret = BluetoothScoManager::HandleScoConnect(static_cast<ScoCategory> (newScoCategory));
598 }
599 CHECK_AND_RETURN_RET_LOG(ret == 0, ERROR, "ConnectScoWithAudioScene failed, result: %{public}d", ret);
600 }
601 scene_ = scene;
602 return SUCCESS;
603 }
604
DisconnectSco()605 int32_t AudioHfpManager::DisconnectSco()
606 {
607 std::lock_guard<std::mutex> sceneLock(g_audioSceneLock);
608 int8_t currentScoCategory = GetScoCategoryFromScene(scene_);
609 if (currentScoCategory == ScoCategory::SCO_DEFAULT) {
610 CHECK_AND_RETURN_RET_LOG(hfpInstance_ != nullptr, ERROR, "nullptr hfpInstance_");
611 int32_t ret = BluetoothScoManager::HandleScoDisconnect(ScoCategory::SCO_VIRTUAL);
612 CHECK_AND_RETURN_RET_LOG(ret == 0, ERROR, "DisconnectSco failed, result: %{public}d", ret);
613 return SUCCESS;
614 }
615 AUDIO_INFO_LOG("current sco category %{public}d", currentScoCategory);
616
617 std::lock_guard<std::mutex> hfpLock(g_hfpInstanceLock);
618 CHECK_AND_RETURN_RET_LOG(hfpInstance_ != nullptr, ERROR, "HFP AG profile instance unavailable");
619 int32_t ret;
620 if (currentScoCategory == ScoCategory::SCO_VIRTUAL && !IsVirtualCall()) {
621 ret = BluetoothScoManager::HandleScoDisconnect(ScoCategory::SCO_CALLULAR);
622 } else {
623 ret = BluetoothScoManager::HandleScoDisconnect(static_cast<ScoCategory> (currentScoCategory));
624 }
625 CHECK_AND_RETURN_RET_LOG(ret == 0, ERROR, "DisconnectSco failed, result: %{public}d", ret);
626 scene_ = AUDIO_SCENE_DEFAULT;
627 return SUCCESS;
628 }
629
GetScoCategoryFromScene(AudioScene scene)630 int8_t AudioHfpManager::GetScoCategoryFromScene(AudioScene scene)
631 {
632 switch (scene) {
633 case AUDIO_SCENE_VOICE_RINGING:
634 case AUDIO_SCENE_PHONE_CALL:
635 return ScoCategory::SCO_CALLULAR;
636 case AUDIO_SCENE_RINGING:
637 case AUDIO_SCENE_PHONE_CHAT:
638 return ScoCategory::SCO_VIRTUAL;
639 default:
640 return ScoCategory::SCO_DEFAULT;
641 }
642 }
643
DisconnectBluetoothHfpSink()644 void AudioHfpManager::DisconnectBluetoothHfpSink()
645 {
646 int connectionState = static_cast<int>(BTConnectState::DISCONNECTED);
647 auto hfpList = HfpBluetoothDeviceManager::GetAllHfpBluetoothDevice();
648 for (const auto &device : hfpList) {
649 hfpListener_->OnConnectionStateChanged(device, connectionState,
650 static_cast<uint32_t>(ConnChangeCause::CONNECT_CHANGE_COMMON_CAUSE));
651 }
652
653 auto virtualDevices = HfpBluetoothDeviceManager::GetHfpVirtualDeviceList();
654 for (const auto &virtualDevice : virtualDevices) {
655 hfpListener_->OnVirtualDeviceChanged(static_cast<int32_t>(Bluetooth::BT_VIRTUAL_DEVICE_REMOVE),
656 virtualDevice.GetDeviceAddr());
657 }
658 HfpBluetoothDeviceManager::ClearAllHfpBluetoothDevice();
659 }
660
UpdateCurrentActiveHfpDevice(const BluetoothRemoteDevice & device)661 void AudioHfpManager::UpdateCurrentActiveHfpDevice(const BluetoothRemoteDevice &device)
662 {
663 std::lock_guard<std::mutex> hfpDeviceLock(g_activehfpDeviceLock);
664 activeHfpDevice_ = device;
665 }
666
GetCurrentActiveHfpDevice()667 std::string AudioHfpManager::GetCurrentActiveHfpDevice()
668 {
669 std::lock_guard<std::mutex> hfpDeviceLock(g_activehfpDeviceLock);
670 return activeHfpDevice_.GetDeviceAddr();
671 }
672
UpdateAudioScene(AudioScene scene)673 void AudioHfpManager::UpdateAudioScene(AudioScene scene)
674 {
675 std::lock_guard<std::mutex> sceneLock(g_audioSceneLock);
676 scene_ = scene;
677 }
678
GetCurrentAudioScene()679 AudioStandard::AudioScene AudioHfpManager::GetCurrentAudioScene()
680 {
681 std::lock_guard<std::mutex> sceneLock(g_audioSceneLock);
682 return scene_;
683 }
684
SetAudioSceneFromPolicy(AudioScene scene)685 void AudioHfpManager::SetAudioSceneFromPolicy(AudioScene scene)
686 {
687 sceneFromPolicy_ = scene;
688 }
689
GetPolicyAudioScene()690 AudioStandard::AudioScene AudioHfpManager::GetPolicyAudioScene()
691 {
692 return sceneFromPolicy_;
693 }
694
Connect(const std::string & macAddress)695 int32_t AudioHfpManager::Connect(const std::string &macAddress)
696 {
697 CHECK_AND_RETURN_RET_LOG(hfpInstance_ != nullptr, ERROR, "HFP AG profile instance unavailable");
698 BluetoothRemoteDevice virtualDevice = BluetoothRemoteDevice(macAddress);
699 if (HfpBluetoothDeviceManager::IsHfpBluetoothDeviceConnecting(macAddress)) {
700 AUDIO_WARNING_LOG("Hfp device %{public}s is connecting, ignore connect request",
701 GetEncryptAddr(macAddress).c_str());
702 virtualDevice.SetVirtualAutoConnectType(CONN_REASON_MANUAL_VIRTUAL_CONNECT_PREEMPT_FLAG, 0);
703 return SUCCESS;
704 }
705 std::vector<std::string> virtualDevices;
706 hfpInstance_->GetVirtualDeviceList(virtualDevices);
707 if (std::find(virtualDevices.begin(), virtualDevices.end(), macAddress) == virtualDevices.end()) {
708 AUDIO_WARNING_LOG("Hfp device %{public}s is not virtual device, ignore connect request",
709 GetEncryptAddr(macAddress).c_str());
710 return SUCCESS;
711 }
712 int32_t ret = hfpInstance_->Connect(virtualDevice);
713 CHECK_AND_RETURN_RET_LOG(ret == 0, ERROR, "Hfp Connect Failed");
714 virtualDevice.SetVirtualAutoConnectType(CONN_REASON_MANUAL_VIRTUAL_CONNECT_PREEMPT_FLAG, 0);
715 return SUCCESS;
716 }
717
SetVirtualCall(const bool isVirtual)718 int32_t AudioHfpManager::SetVirtualCall(const bool isVirtual)
719 {
720 AUDIO_INFO_LOG("SetVirtualCall %{public}d", isVirtual);
721 isVirtualCall = isVirtual;
722 return SUCCESS;
723 }
724
IsVirtualCall()725 bool AudioHfpManager::IsVirtualCall()
726 {
727 return isVirtualCall;
728 }
729
IsAudioScoStateConnect()730 bool AudioHfpManager::IsAudioScoStateConnect()
731 {
732 AudioScoState scoState = BluetoothScoManager::GetAudioScoState();
733 bool isConnect = (scoState == AudioScoState::CONNECTED || scoState == AudioScoState::CONNECTING);
734 return isConnect;
735 }
736
OnScoStateChanged(const BluetoothRemoteDevice & device,int state,int reason)737 void AudioHfpListener::OnScoStateChanged(const BluetoothRemoteDevice &device, int state, int reason)
738 {
739 AUDIO_WARNING_LOG("state:[%{public}d] reason:[%{public}d] device:[%{public}s]",
740 state, reason, GetEncryptAddr(device.GetDeviceAddr()).c_str());
741 // SCO_DISCONNECTED = 3, SCO_CONNECTING = 4, SCO_DISCONNECTING = 5, SCO_CONNECTED = 6
742 HfpScoConnectState scoState = static_cast<HfpScoConnectState>(state);
743 if (scoState == HfpScoConnectState::SCO_CONNECTED || scoState == HfpScoConnectState::SCO_DISCONNECTED) {
744 if (device.GetDeviceAddr() == AudioHfpManager::GetCurrentActiveHfpDevice() &&
745 scoState == HfpScoConnectState::SCO_DISCONNECTED) {
746 BluetoothRemoteDevice defaultDevice;
747 AudioHfpManager::UpdateCurrentActiveHfpDevice(defaultDevice);
748 AUDIO_WARNING_LOG("Sco disconnect, need set audio scene as default.");
749 AudioHfpManager::UpdateAudioScene(AUDIO_SCENE_DEFAULT);
750 } else if (scoState == HfpScoConnectState::SCO_CONNECTED && reason == HFP_AG_SCO_REMOTE_USER_SET_UP) {
751 AudioScene audioScene = AudioHfpManager::GetPolicyAudioScene();
752 if (audioScene != AudioHfpManager::GetCurrentAudioScene()) {
753 AUDIO_WARNING_LOG("Sco connect by peripheral device, update scene_ %{public}d", audioScene);
754 AudioHfpManager::UpdateAudioScene(audioScene);
755 }
756 AudioHfpManager::UpdateCurrentActiveHfpDevice(device);
757 }
758 bool isConnected = (scoState == HfpScoConnectState::SCO_CONNECTED) ? true : false;
759 BluetoothScoManager::UpdateScoState(scoState, &device);
760 HfpBluetoothDeviceManager::OnScoStateChanged(device, isConnected, reason);
761 }
762 }
763
OnConnectionStateChanged(const BluetoothRemoteDevice & device,int state,int cause)764 void AudioHfpListener::OnConnectionStateChanged(const BluetoothRemoteDevice &device, int state, int cause)
765 {
766 AUDIO_WARNING_LOG("state: %{public}d device: %{public}s", state, GetEncryptAddr(device.GetDeviceAddr()).c_str());
767 if (state == static_cast<int>(BTConnectState::CONNECTING)) {
768 HfpBluetoothDeviceManager::SetHfpStack(device, BluetoothDeviceAction::CONNECTING_ACTION);
769 }
770 if (state == static_cast<int>(BTConnectState::CONNECTED)) {
771 HfpBluetoothDeviceManager::SetHfpStack(device, BluetoothDeviceAction::CONNECT_ACTION);
772 }
773 if (state == static_cast<int>(BTConnectState::DISCONNECTED)) {
774 if (device.GetDeviceAddr() == AudioHfpManager::GetCurrentActiveHfpDevice()) {
775 BluetoothRemoteDevice defaultDevice;
776 AudioHfpManager::UpdateCurrentActiveHfpDevice(defaultDevice);
777 AUDIO_WARNING_LOG("Current active hfp device diconnect, need set audio scene as default.");
778 AudioHfpManager::UpdateAudioScene(AUDIO_SCENE_DEFAULT);
779 BluetoothScoManager::UpdateScoState(HfpScoConnectState::SCO_DISCONNECTED, &device);
780 }
781 HfpBluetoothDeviceManager::SetHfpStack(device, BluetoothDeviceAction::DISCONNECT_ACTION);
782 }
783 }
784
OnHfpStackChanged(const BluetoothRemoteDevice & device,int action)785 void AudioHfpListener::OnHfpStackChanged(const BluetoothRemoteDevice &device, int action)
786 {
787 AUDIO_WARNING_LOG("action: %{public}d device: %{public}s", action, GetEncryptAddr(device.GetDeviceAddr()).c_str());
788 HfpBluetoothDeviceManager::SetHfpStack(device, action);
789 }
790
OnVirtualDeviceChanged(int32_t action,std::string macAddress)791 void AudioHfpListener::OnVirtualDeviceChanged(int32_t action, std::string macAddress)
792 {
793 AUDIO_WARNING_LOG("action: %{public}d device: %{public}s", action, GetEncryptAddr(macAddress).c_str());
794 if (action == static_cast<int32_t>(Bluetooth::BT_VIRTUAL_DEVICE_ADD)) {
795 HfpBluetoothDeviceManager::SetHfpStack(BluetoothRemoteDevice(macAddress),
796 BluetoothDeviceAction::VIRTUAL_DEVICE_ADD_ACTION);
797 }
798 if (action == static_cast<int32_t>(Bluetooth::BT_VIRTUAL_DEVICE_REMOVE)) {
799 HfpBluetoothDeviceManager::SetHfpStack(BluetoothRemoteDevice(macAddress),
800 BluetoothDeviceAction::VIRTUAL_DEVICE_REMOVE_ACTION);
801 }
802 }
803 // LCOV_EXCL_STOP
804 } // namespace Bluetooth
805 } // namespace OHOS
806