1 /*
2 * Copyright (C) 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
16 #include "bluetooth_a2dp_observer_impl.h"
17
18 #include "bluetooth_log.h"
19 #include "bluetooth_utils.h"
20
21 namespace OHOS {
22 namespace Bluetooth {
OnConnectionStateChanged(const BluetoothRemoteDevice & device,int state,int cause)23 void A2dpSourceObserverImpl::OnConnectionStateChanged(const BluetoothRemoteDevice& device, int state, int cause)
24 {
25 HILOGD("enter, remote device address: %{public}s, state: %{public}d, cause: %{public}d", GET_ENCRYPT_ADDR(device),
26 state, cause);
27 if (!stateChangeFunc) {
28 HILOGD("failed to register state change callback");
29 return;
30 }
31 char* deviceId = MallocCString(device.GetDeviceAddr());
32 if (deviceId == nullptr) {
33 HILOGD("failed to malloc memory");
34 return;
35 }
36 StateChangeParam param = StateChangeParam {
37 .deviceId = deviceId, .state = static_cast<int32_t>(state), .cause = static_cast<int32_t>(cause)
38 };
39 stateChangeFunc(param);
40 free(deviceId);
41 }
42
RegisterStateChangeFunc(std::function<void (StateChangeParam)> cjCallback)43 void A2dpSourceObserverImpl::RegisterStateChangeFunc(std::function<void(StateChangeParam)> cjCallback)
44 {
45 stateChangeFunc = cjCallback;
46 }
47
48 } // namespace Bluetooth
49 } // namespace OHOS