1 /*
2 * Copyright (C) 2021-2022 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 #include <uv.h>
16 #include "napi_bluetooth_pbap_pse_observer.h"
17
18 namespace OHOS {
19 namespace Bluetooth {
OnServiceConnectionStateChanged(const BluetoothRemoteDevice & device,int state)20 void NapiPbapPseObserver::OnServiceConnectionStateChanged(const BluetoothRemoteDevice &device, int state)
21 {
22 HILOGI("NapiPbapPseObserver::OnServiceConnectionStateChanged called");
23 if (!callbackInfos_[STR_BT_PBAP_PSE_CONNECTION_STATE_CHANGE]) {
24 HILOGW("NapiPbapPseObserver::OnServiceConnectionStateChanged: This callback is not registered by ability.");
25 return;
26 }
27 HILOGI("NapiPbapPseObserver::OnServiceConnectionStateChanged: %{public}s is registered by ability",
28 STR_BT_PBAP_PSE_CONNECTION_STATE_CHANGE.c_str());
29 std::shared_ptr<BluetoothCallbackInfo> callbackInfo =
30 callbackInfos_[STR_BT_PBAP_PSE_CONNECTION_STATE_CHANGE];
31
32 callbackInfo->state_ = state;
33 callbackInfo->deviceId_ = device.GetDeviceAddr();
34 uv_loop_s *loop = nullptr;
35 napi_get_uv_event_loop(callbackInfo->env_, &loop);
36 uv_work_t *work = new uv_work_t;
37 work->data = (void*)callbackInfo.get();
38
39 uv_queue_work(
40 loop,
41 work,
42 [](uv_work_t *work) {},
43 [](uv_work_t *work, int status) {
44 BluetoothCallbackInfo *callbackInfo = (BluetoothCallbackInfo *)work->data;
45 napi_value result = nullptr;
46 napi_create_object(callbackInfo->env_, &result);
47 ConvertStateChangeParamToJS(callbackInfo->env_, result, callbackInfo->deviceId_, callbackInfo->state_);
48 napi_value callback = nullptr;
49 napi_value undefined = nullptr;
50 napi_value callResult = nullptr;
51 napi_get_undefined(callbackInfo->env_, &undefined);
52 napi_get_reference_value(callbackInfo->env_, callbackInfo->callback_, &callback);
53 napi_call_function(callbackInfo->env_, undefined, callback, ARGS_SIZE_ONE, &result, &callResult);
54 delete work;
55 work = nullptr;
56 }
57 );
58 }
59
60 } // namespace Bluetooth
61 } // namespace OHOS