1 /*
2 * Copyright (C) 2023 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 "bt_napi_connection"
17 #endif
18
19 #include "napi_bluetooth_connection.h"
20
21 #include <set>
22
23 #include "napi_bluetooth_connection_observer.h"
24 #include "napi_bluetooth_remote_device_observer.h"
25 #include "bluetooth_log.h"
26 #include "bluetooth_errorcode.h"
27 #include "napi_bluetooth_error.h"
28 #include "napi_async_work.h"
29 #include "napi_bluetooth_utils.h"
30 #include "napi_ha_event_utils.h"
31 #include "parser/napi_parser_utils.h"
32 #include "hitrace_meter.h"
33 #include "bluetooth_utils.h"
34
35 namespace OHOS {
36 namespace Bluetooth {
37 std::shared_ptr<NapiBluetoothConnectionObserver> g_connectionObserver =
38 std::make_shared<NapiBluetoothConnectionObserver>();
39 std::shared_ptr<NapiBluetoothRemoteDeviceObserver> g_remoteDeviceObserver =
40 std::make_shared<NapiBluetoothRemoteDeviceObserver>();
41 std::mutex deviceMutex;
42
43 #ifdef BLUETOOTH_API_SINCE_10
DefineConnectionFunctions(napi_env env,napi_value exports)44 napi_value DefineConnectionFunctions(napi_env env, napi_value exports)
45 {
46 HILOGD("enter");
47 RegisterObserverToHost();
48 ConnectionPropertyValueInit(env, exports);
49 napi_property_descriptor desc[] = {
50 DECLARE_NAPI_FUNCTION("getBtConnectionState", GetBtConnectionState),
51 DECLARE_NAPI_FUNCTION("pairDevice", PairDeviceAsync),
52 DECLARE_NAPI_FUNCTION("cancelPairedDevice", CancelPairedDeviceAsync),
53 DECLARE_NAPI_FUNCTION("getProfileConnectionState", GetProfileConnectionStateEx),
54 DECLARE_NAPI_FUNCTION("setDevicePinCode", SetDevicePinCode),
55 DECLARE_NAPI_FUNCTION("cancelPairingDevice", CancelPairingDevice),
56 DECLARE_NAPI_FUNCTION("pairCredibleDevice", PairCredibleDevice),
57 DECLARE_NAPI_FUNCTION("getLocalProfileUuids", GetLocalProfileUuids),
58 DECLARE_NAPI_FUNCTION("getRemoteProfileUuids", GetRemoteProfileUuids),
59 DECLARE_NAPI_FUNCTION("on", RegisterConnectionObserver),
60 DECLARE_NAPI_FUNCTION("off", DeRegisterConnectionObserver),
61 DECLARE_NAPI_FUNCTION("isBluetoothDiscovering", IsBluetoothDiscovering),
62 DECLARE_NAPI_FUNCTION("getPairState", GetPairState),
63 DECLARE_NAPI_FUNCTION("connectAllowedProfiles", ConnectAllowedProfiles),
64 DECLARE_NAPI_FUNCTION("disconnectAllowedProfiles", DisconnectAllowedProfiles),
65 DECLARE_NAPI_FUNCTION("getRemoteProductId", GetRemoteProductId),
66 DECLARE_NAPI_FUNCTION("getRemoteDeviceName", GetRemoteDeviceName),
67 DECLARE_NAPI_FUNCTION("getRemoteDeviceClass", GetRemoteDeviceClass),
68 DECLARE_NAPI_FUNCTION("getLocalName", GetLocalName),
69 DECLARE_NAPI_FUNCTION("getPairedDevices", GetPairedDevices),
70 DECLARE_NAPI_FUNCTION("getProfileConnState", GetProfileConnectionState),
71 DECLARE_NAPI_FUNCTION("setDevicePairingConfirmation", SetDevicePairingConfirmation),
72 DECLARE_NAPI_FUNCTION("setLocalName", SetLocalName),
73 DECLARE_NAPI_FUNCTION("setBluetoothScanMode", SetBluetoothScanMode),
74 DECLARE_NAPI_FUNCTION("getBluetoothScanMode", GetBluetoothScanMode),
75 DECLARE_NAPI_FUNCTION("startBluetoothDiscovery", StartBluetoothDiscovery),
76 DECLARE_NAPI_FUNCTION("stopBluetoothDiscovery", StopBluetoothDiscovery),
77 DECLARE_NAPI_FUNCTION("setRemoteDeviceName", SetRemoteDeviceName),
78 DECLARE_NAPI_FUNCTION("setRemoteDeviceType", SetRemoteDeviceType),
79 DECLARE_NAPI_FUNCTION("getRemoteDeviceType", GetRemoteDeviceType),
80 DECLARE_NAPI_FUNCTION("getRemoteDeviceBatteryInfo", GetRemoteDeviceBatteryInfo),
81 DECLARE_NAPI_FUNCTION("controlDeviceAction", ControlDeviceAction),
82 DECLARE_NAPI_FUNCTION("getLastConnectionTime", GetRemoteDeviceConnectionTime),
83 DECLARE_NAPI_FUNCTION("updateCloudBluetoothDevice", UpdateCloudBluetoothDevice),
84 DECLARE_NAPI_FUNCTION("getCarKeyDfxData", GetCarKeyDfxData),
85 DECLARE_NAPI_FUNCTION("setCarKeyDfxData", SetCarKeyCardData),
86 DECLARE_NAPI_FUNCTION("getRemoteDeviceTransport", GetRemoteDeviceTransport),
87 };
88
89 HITRACE_METER_NAME(HITRACE_TAG_OHOS, "connection:napi_define_properties");
90 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
91 return exports;
92 }
93 #else
DefineConnectionFunctions(napi_env env,napi_value exports)94 napi_value DefineConnectionFunctions(napi_env env, napi_value exports)
95 {
96 HILOGD("enter");
97 RegisterObserverToHost();
98 ConnectionPropertyValueInit(env, exports);
99 napi_property_descriptor desc[] = {
100 DECLARE_NAPI_FUNCTION("getBtConnectionState", GetBtConnectionState),
101 DECLARE_NAPI_FUNCTION("pairDevice", PairDevice),
102 DECLARE_NAPI_FUNCTION("cancelPairedDevice", CancelPairedDevice),
103 DECLARE_NAPI_FUNCTION("getProfileConnectionState", GetProfileConnectionState),
104 DECLARE_NAPI_FUNCTION("getRemoteDeviceName", GetRemoteDeviceName),
105 DECLARE_NAPI_FUNCTION("getRemoteDeviceClass", GetRemoteDeviceClass),
106 DECLARE_NAPI_FUNCTION("getLocalName", GetLocalName),
107 DECLARE_NAPI_FUNCTION("getPairedDevices", GetPairedDevices),
108 DECLARE_NAPI_FUNCTION("getProfileConnState", GetProfileConnectionState),
109 DECLARE_NAPI_FUNCTION("setDevicePairingConfirmation", SetDevicePairingConfirmation),
110 DECLARE_NAPI_FUNCTION("setLocalName", SetLocalName),
111 DECLARE_NAPI_FUNCTION("setBluetoothScanMode", SetBluetoothScanMode),
112 DECLARE_NAPI_FUNCTION("getBluetoothScanMode", GetBluetoothScanMode),
113 DECLARE_NAPI_FUNCTION("startBluetoothDiscovery", StartBluetoothDiscovery),
114 DECLARE_NAPI_FUNCTION("stopBluetoothDiscovery", StopBluetoothDiscovery),
115 DECLARE_NAPI_FUNCTION("setRemoteDeviceName", SetRemoteDeviceName),
116 DECLARE_NAPI_FUNCTION("setRemoteDeviceType", SetRemoteDeviceType),
117 DECLARE_NAPI_FUNCTION("getRemoteDeviceType", GetRemoteDeviceType),
118 DECLARE_NAPI_FUNCTION("getRemoteDeviceBatteryInfo", GetRemoteDeviceBatteryInfo),
119 DECLARE_NAPI_FUNCTION("controlDeviceAction", ControlDeviceAction),
120 DECLARE_NAPI_FUNCTION("getLastConnectionTime", GetRemoteDeviceConnectionTime),
121 DECLARE_NAPI_FUNCTION("updateCloudBluetoothDevice", UpdateCloudBluetoothDevice),
122 };
123
124 HITRACE_METER_NAME(HITRACE_TAG_OHOS, "connection:napi_define_properties");
125 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
126 return exports;
127 }
128 #endif
129
130 using NapiBluetoothOnOffFunc = std::function<napi_status(napi_env env, napi_callback_info info)>;
131
NapiConnectionOnOffExecute(napi_env env,napi_callback_info info,NapiBluetoothOnOffFunc connectionObserverFunc,NapiBluetoothOnOffFunc remoteDeviceObserverFunc)132 static napi_status NapiConnectionOnOffExecute(napi_env env, napi_callback_info info,
133 NapiBluetoothOnOffFunc connectionObserverFunc, NapiBluetoothOnOffFunc remoteDeviceObserverFunc)
134 {
135 std::string type = "";
136 NAPI_BT_CALL_RETURN(NapiGetOnOffCallbackName(env, info, type));
137
138 napi_status status = napi_ok;
139 if (type == REGISTER_DEVICE_FIND_TYPE ||
140 type == REGISTER_DISCOVERY_RESULT_TYPE ||
141 type == REGISTER_PIN_REQUEST_TYPE) {
142 status = connectionObserverFunc(env, info);
143 } else if (type == REGISTER_BOND_STATE_TYPE || type == REGISTER_BATTERY_CHANGE_TYPE) {
144 status = remoteDeviceObserverFunc(env, info);
145 } else {
146 HILOGE("Unsupported callback: %{public}s", type.c_str());
147 status = napi_invalid_arg;
148 }
149 return status;
150 }
151
RegisterConnectionObserver(napi_env env,napi_callback_info info)152 napi_value RegisterConnectionObserver(napi_env env, napi_callback_info info)
153 {
154 auto connectionObserverFunc = [](napi_env env, napi_callback_info info) {
155 return g_connectionObserver->eventSubscribe_.Register(env, info);
156 };
157 auto remoteDeviceObserverFunc = [](napi_env env, napi_callback_info info) {
158 return g_remoteDeviceObserver->eventSubscribe_.Register(env, info);
159 };
160
161 auto status = NapiConnectionOnOffExecute(env, info, connectionObserverFunc, remoteDeviceObserverFunc);
162 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
163 return NapiGetUndefinedRet(env);
164 }
165
DeRegisterConnectionObserver(napi_env env,napi_callback_info info)166 napi_value DeRegisterConnectionObserver(napi_env env, napi_callback_info info)
167 {
168 auto connectionObserverFunc = [](napi_env env, napi_callback_info info) {
169 return g_connectionObserver->eventSubscribe_.Deregister(env, info);
170 };
171 auto remoteDeviceObserverFunc = [](napi_env env, napi_callback_info info) {
172 return g_remoteDeviceObserver->eventSubscribe_.Deregister(env, info);
173 };
174
175 auto status = NapiConnectionOnOffExecute(env, info, connectionObserverFunc, remoteDeviceObserverFunc);
176 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
177 return NapiGetUndefinedRet(env);
178 }
179
GetBtConnectionState(napi_env env,napi_callback_info info)180 napi_value GetBtConnectionState(napi_env env, napi_callback_info info)
181 {
182 HILOGD("enter");
183 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
184 int state = static_cast<int>(BTConnectState::DISCONNECTED);
185 int32_t err = host->GetBtConnectionState(state);
186 HILOGD("start state %{public}d", state);
187 napi_value result = nullptr;
188 napi_create_int32(env, GetProfileConnectionState(state), &result);
189 NAPI_BT_ASSERT_RETURN(env, err == BT_NO_ERROR, err, result);
190 return result;
191 }
192
PairDevice(napi_env env,napi_callback_info info)193 napi_value PairDevice(napi_env env, napi_callback_info info)
194 {
195 HILOGD("enter");
196 std::string remoteAddr = INVALID_MAC_ADDRESS;
197 bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
198 NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
199
200 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
201 int32_t ret = remoteDevice.StartPair();
202 NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_NO_ERROR, ret);
203 return NapiGetBooleanTrue(env);
204 }
205
CancelPairedDevice(napi_env env,napi_callback_info info)206 napi_value CancelPairedDevice(napi_env env, napi_callback_info info)
207 {
208 HILOGD("enter");
209 std::string remoteAddr{};
210 bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
211 NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
212
213 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
214 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
215 int32_t ret = host->RemovePair(remoteDevice);
216 NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_NO_ERROR, ret);
217
218 return NapiGetBooleanTrue(env);
219 }
220
CheckGetRemoteDeviceNameParam(napi_env env,napi_callback_info info,std::string & addr,bool & alias)221 bool CheckGetRemoteDeviceNameParam(napi_env env, napi_callback_info info, std::string &addr, bool &alias)
222 {
223 size_t argc = ARGS_SIZE_TWO;
224 napi_value argv[ARGS_SIZE_TWO] = {nullptr};
225 NAPI_BT_RETURN_IF(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr) != napi_ok, "call failed.", false);
226 NAPI_BT_RETURN_IF(NapiParseBdAddr(env, argv[PARAM0], addr) != napi_ok, "NapiParseBdAddr failed", false);
227 if (argc > ARGS_SIZE_ONE) {
228 NAPI_BT_RETURN_IF(!ParseBool(env, alias, argv[PARAM1]), "ParseBool failed", false);
229 }
230 return true;
231 }
232
GetRemoteDeviceName(napi_env env,napi_callback_info info)233 napi_value GetRemoteDeviceName(napi_env env, napi_callback_info info)
234 {
235 HILOGD("start");
236 std::string remoteAddr = INVALID_MAC_ADDRESS;
237 std::string name = INVALID_NAME;
238 napi_value result = nullptr;
239 bool alias = true;
240 bool checkRet = CheckGetRemoteDeviceNameParam(env, info, remoteAddr, alias);
241 napi_create_string_utf8(env, name.c_str(), name.size(), &result);
242 NAPI_BT_ASSERT_RETURN(env, checkRet == true, BT_ERR_INVALID_PARAM, result);
243
244 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
245 int32_t err = remoteDevice.GetDeviceName(name, alias);
246 napi_create_string_utf8(env, name.c_str(), name.size(), &result);
247 NAPI_BT_ASSERT_RETURN(env, err == BT_NO_ERROR, err, result);
248 return result;
249 }
250
GetCarKeyDfxData(napi_env env,napi_callback_info info)251 napi_value GetCarKeyDfxData(napi_env env, napi_callback_info info)
252 {
253 HILOGD("enter");
254 napi_value result = nullptr;
255 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
256 std::string dfxData;
257 int32_t err = host->GetCarKeyDfxData(dfxData);
258 napi_create_string_utf8(env, dfxData.c_str(), dfxData.size(), &result);
259 NAPI_BT_ASSERT_RETURN(env, err == BT_NO_ERROR, err, result);
260 return result;
261 }
262
ParseSetCarKeyCardDataParameters(napi_env env,napi_callback_info info,std::string & outRemoteAddr,int32_t & outAction)263 napi_status ParseSetCarKeyCardDataParameters(napi_env env, napi_callback_info info,
264 std::string &outRemoteAddr, int32_t &outAction)
265 {
266 HILOGD("enter");
267 std::string remoteAddr{};
268 size_t argc = ARGS_SIZE_TWO;
269 napi_value argv[ARGS_SIZE_TWO] = {nullptr};
270 NAPI_BT_CALL_RETURN(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
271 NAPI_BT_RETURN_IF(argc != ARGS_SIZE_TWO, "Requires 2 arguments.", napi_invalid_arg);
272 NAPI_BT_CALL_RETURN(NapiParseBdAddr(env, argv[PARAM0], remoteAddr));
273 outRemoteAddr = remoteAddr;
274 NAPI_BT_RETURN_IF(!ParseInt32(env, outAction, argv[PARAM1]), "action ParseInt32 failed", napi_invalid_arg);
275 return napi_ok;
276 }
277
SetCarKeyCardData(napi_env env,napi_callback_info info)278 napi_value SetCarKeyCardData(napi_env env, napi_callback_info info)
279 {
280 HILOGD("enter");
281 std::string remoteAddr;
282 int32_t action = 0;
283 auto status = ParseSetCarKeyCardDataParameters(env, info, remoteAddr, action);
284 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
285 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
286 int ret = host->SetCarKeyCardData(remoteAddr, action);
287 NAPI_BT_ASSERT_RETURN_UNDEF(env, ret == BT_NO_ERROR, ret);
288 return NapiGetBooleanTrue(env);
289 }
290
GetRemoteDeviceClass(napi_env env,napi_callback_info info)291 napi_value GetRemoteDeviceClass(napi_env env, napi_callback_info info)
292 {
293 HILOGD("start");
294 std::string remoteAddr = INVALID_MAC_ADDRESS;
295 bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
296 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet, BT_ERR_INVALID_PARAM);
297
298 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
299 int tmpCod = MajorClass::MAJOR_UNCATEGORIZED;
300 int tmpMajorClass = MajorClass::MAJOR_UNCATEGORIZED;
301 int tmpMajorMinorClass = MajorClass::MAJOR_UNCATEGORIZED;
302 int32_t err = remoteDevice.GetDeviceProductType(tmpCod, tmpMajorClass, tmpMajorMinorClass);
303 napi_value result = nullptr;
304 napi_create_object(env, &result);
305 napi_value majorClass = 0;
306 napi_create_int32(env, tmpMajorClass, &majorClass);
307 napi_set_named_property(env, result, "majorClass", majorClass);
308 napi_value majorMinorClass = 0;
309 napi_create_int32(env, tmpMajorMinorClass, &majorMinorClass);
310 napi_set_named_property(env, result, "majorMinorClass", majorMinorClass);
311 napi_value cod = 0;
312 napi_create_int32(env, tmpCod, &cod);
313 napi_set_named_property(env, result, "classOfDevice", cod);
314 NAPI_BT_ASSERT_RETURN(env, err == BT_NO_ERROR, err, result);
315 return result;
316 }
317
GetLocalName(napi_env env,napi_callback_info info)318 napi_value GetLocalName(napi_env env, napi_callback_info info)
319 {
320 napi_value result = nullptr;
321 HILOGD("enter");
322 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
323 std::string localName = INVALID_NAME;
324 int32_t err = host->GetLocalName(localName);
325 napi_create_string_utf8(env, localName.c_str(), localName.size(), &result);
326 NAPI_BT_ASSERT_RETURN(env, err == BT_NO_ERROR, err, result);
327 HILOGI("end");
328 return result;
329 }
330
GetPairedDevices(napi_env env,napi_callback_info info)331 napi_value GetPairedDevices(napi_env env, napi_callback_info info)
332 {
333 HILOGD("enter");
334 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
335 std::vector<BluetoothRemoteDevice> remoteDeviceLists;
336 int32_t ret = host->GetPairedDevices(BT_TRANSPORT_BREDR, remoteDeviceLists);
337 napi_value result = nullptr;
338 int count = 0;
339 napi_create_array(env, &result);
340 for (auto vec : remoteDeviceLists) {
341 napi_value remoteDeviceResult;
342 napi_create_string_utf8(env, vec.GetDeviceAddr().c_str(), vec.GetDeviceAddr().size(), &remoteDeviceResult);
343 napi_set_element(env, result, count, remoteDeviceResult);
344 count++;
345 }
346 NAPI_BT_ASSERT_RETURN(env, ret == BT_NO_ERROR, ret, result);
347 HILOGI("end");
348 return result;
349 }
350
GetProfileConnectionState(napi_env env,napi_callback_info info)351 napi_value GetProfileConnectionState(napi_env env, napi_callback_info info)
352 {
353 HILOGD("enter");
354 int profileId = 0;
355 bool checkRet = CheckProfileIdParam(env, info, profileId);
356 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet, BT_ERR_INVALID_PARAM);
357
358 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
359 int state = static_cast<int>(BTConnectState::DISCONNECTED);
360 int32_t err = host->GetBtProfileConnState(GetProfileId(profileId), state);
361 int status = GetProfileConnectionState(state);
362 napi_value ret = nullptr;
363 napi_create_int32(env, status, &ret);
364 NAPI_BT_ASSERT_RETURN(env, err == BT_NO_ERROR, err, ret);
365 HILOGD("status: %{public}d", status);
366 return ret;
367 }
368
GetProfileConnectionStateEx(napi_env env,napi_callback_info info)369 napi_value GetProfileConnectionStateEx(napi_env env, napi_callback_info info)
370 {
371 HILOGD("enter");
372 int profileId = 0;
373 size_t argSize = ARGS_SIZE_ONE;
374 bool checkRet = CheckProfileIdParamEx(env, info, profileId, argSize);
375 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet, BT_ERR_INVALID_PARAM);
376
377 napi_value ret = nullptr;
378 if (argSize == 0) {
379 ret = GetBtConnectionState(env, info);
380 } else {
381 ret = GetProfileConnectionState(env, info);
382 }
383 return ret;
384 }
385
SetDevicePairingConfirmation(napi_env env,napi_callback_info info)386 napi_value SetDevicePairingConfirmation(napi_env env, napi_callback_info info)
387 {
388 HILOGD("enter");
389 std::string remoteAddr{};
390 bool accept = false;
391 bool checkRet = CheckSetDevicePairingConfirmationParam(env, info, remoteAddr, accept);
392 NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
393
394 HILOGI("SetDevicePairingConfirmation::accept = %{public}d", accept);
395 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
396 int32_t ret = BT_NO_ERROR;
397 if (accept) {
398 ret = remoteDevice.SetDevicePairingConfirmation(accept);
399 } else {
400 ret = remoteDevice.CancelPairing();
401 }
402 NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_NO_ERROR, ret);
403 return NapiGetBooleanTrue(env);
404 }
405
SetLocalName(napi_env env,napi_callback_info info)406 napi_value SetLocalName(napi_env env, napi_callback_info info)
407 {
408 HILOGD("enter");
409 std::string localName = INVALID_NAME;
410 bool checkRet = CheckLocalNameParam(env, info, localName);
411 NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
412
413 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
414 int32_t ret = host->SetLocalName(localName);
415 NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_NO_ERROR, ret);
416 return NapiGetBooleanTrue(env);
417 }
418
SetBluetoothScanMode(napi_env env,napi_callback_info info)419 napi_value SetBluetoothScanMode(napi_env env, napi_callback_info info)
420 {
421 HILOGD("enter");
422 int32_t mode = 0;
423 int32_t duration = 0;
424 bool checkRet = CheckSetBluetoothScanModeParam(env, info, mode, duration);
425 NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
426 HILOGI("mode = %{public}d,duration = %{public}d", mode, duration);
427
428 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
429 int32_t ret = host->SetBtScanMode(mode, duration);
430 NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_NO_ERROR, ret);
431 host->SetBondableMode(BT_TRANSPORT_BREDR, 1);
432 return NapiGetBooleanTrue(env);
433 }
434
GetBluetoothScanMode(napi_env env,napi_callback_info info)435 napi_value GetBluetoothScanMode(napi_env env, napi_callback_info info)
436 {
437 HILOGD("enter");
438 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
439 int32_t scanMode = 0;
440 int32_t err = host->GetBtScanMode(scanMode);
441 napi_value result = nullptr;
442 napi_create_uint32(env, scanMode, &result);
443 NAPI_BT_ASSERT_RETURN(env, err == BT_NO_ERROR, err, result);
444 HILOGI("end");
445 return result;
446 }
447
StartBluetoothDiscovery(napi_env env,napi_callback_info info)448 napi_value StartBluetoothDiscovery(napi_env env, napi_callback_info info)
449 {
450 HILOGD("enter");
451 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
452 int ret = host->StartBtDiscovery();
453 NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_NO_ERROR, ret);
454 return NapiGetBooleanTrue(env);
455 }
456
StopBluetoothDiscovery(napi_env env,napi_callback_info info)457 napi_value StopBluetoothDiscovery(napi_env env, napi_callback_info info)
458 {
459 HILOGD("enter");
460 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
461 int ret = host->CancelBtDiscovery();
462 NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_NO_ERROR, ret);
463 return NapiGetBooleanTrue(env);
464 }
465
466 #ifdef BLUETOOTH_API_SINCE_10
ParseSetDevicePinCodeParameters(napi_env env,napi_callback_info info,std::string & outRemoteAddr,std::string & outPinCode)467 napi_status ParseSetDevicePinCodeParameters(napi_env env, napi_callback_info info,
468 std::string &outRemoteAddr, std::string &outPinCode)
469 {
470 HILOGD("enter");
471 std::string remoteAddr{};
472 std::string pinCode{};
473 size_t argc = ARGS_SIZE_THREE;
474 napi_value argv[ARGS_SIZE_THREE] = {nullptr};
475 NAPI_BT_CALL_RETURN(napi_get_cb_info(env, info, &argc, argv, nullptr, NULL));
476 NAPI_BT_RETURN_IF(argc != ARGS_SIZE_TWO && argc != ARGS_SIZE_THREE,
477 "Requires 2 or 3 arguments.", napi_invalid_arg);
478 NAPI_BT_CALL_RETURN(NapiParseBdAddr(env, argv[PARAM0], remoteAddr));
479 NAPI_BT_RETURN_IF(!ParseString(env, pinCode, argv[PARAM1]), "pinCode ParseString failed", napi_invalid_arg);
480 outRemoteAddr = remoteAddr;
481 outPinCode = pinCode;
482 return napi_ok;
483 }
484
SetDevicePinCode(napi_env env,napi_callback_info info)485 napi_value SetDevicePinCode(napi_env env, napi_callback_info info)
486 {
487 HILOGD("enter");
488 std::string remoteAddr = "";
489 std::string pinCode = "";
490 auto status = ParseSetDevicePinCodeParameters(env, info, remoteAddr, pinCode);
491 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
492
493 auto func = [remoteAddr, pinCode]() {
494 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
495 int32_t err = remoteDevice.SetDevicePin(pinCode);
496 HILOGI("SetDevicePinCode err: %{public}d", err);
497 return NapiAsyncWorkRet(err);
498 };
499 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
500 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
501 asyncWork->Run();
502 return asyncWork->GetRet();
503 }
504
CheckDeviceAsyncParam(napi_env env,napi_callback_info info,std::string & addr)505 napi_status CheckDeviceAsyncParam(napi_env env, napi_callback_info info, std::string &addr)
506 {
507 size_t argc = ARGS_SIZE_TWO;
508 napi_value argv[ARGS_SIZE_TWO] = {nullptr};
509 NAPI_BT_CALL_RETURN(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
510 NAPI_BT_RETURN_IF(argc != ARGS_SIZE_ONE && argc != ARGS_SIZE_TWO, "Requires 1 or 2 arguments", napi_invalid_arg);
511 NAPI_BT_CALL_RETURN(NapiParseBdAddr(env, argv[PARAM0], addr));
512 return napi_ok;
513 }
514
PairDeviceAsync(napi_env env,napi_callback_info info)515 napi_value PairDeviceAsync(napi_env env, napi_callback_info info)
516 {
517 HILOGD("enter");
518 std::shared_ptr<NapiHaEventUtils> haUtils = std::make_shared<NapiHaEventUtils>(env, "connection.PairDeviceAsync");
519 std::string remoteAddr = INVALID_MAC_ADDRESS;
520 auto checkRet = CheckDeviceAsyncParam(env, info, remoteAddr);
521 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet == napi_ok, BT_ERR_INVALID_PARAM);
522
523 auto func = [remoteAddr]() {
524 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
525 int32_t err = remoteDevice.StartPair();
526 HILOGI("err: %{public}d", err);
527 return NapiAsyncWorkRet(err);
528 };
529 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK, haUtils);
530 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
531 asyncWork->Run();
532 return asyncWork->GetRet();
533 }
534
CancelPairedDeviceAsync(napi_env env,napi_callback_info info)535 napi_value CancelPairedDeviceAsync(napi_env env, napi_callback_info info)
536 {
537 HILOGD("enter");
538 std::string remoteAddr {};
539 bool checkRet = CheckDeviceAsyncParam(env, info, remoteAddr);
540 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet == napi_ok, BT_ERR_INVALID_PARAM);
541
542 auto func = [remoteAddr]() {
543 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
544 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
545 int32_t err = host->RemovePair(remoteDevice);
546 HILOGI("err: %{public}d", err);
547 return NapiAsyncWorkRet(err);
548 };
549 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
550 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
551 asyncWork->Run();
552 return asyncWork->GetRet();
553 }
554
CancelPairingDevice(napi_env env,napi_callback_info info)555 napi_value CancelPairingDevice(napi_env env, napi_callback_info info)
556 {
557 HILOGD("enter");
558 std::string remoteAddr{};
559 bool checkRet = CheckDeviceAsyncParam(env, info, remoteAddr);
560 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet == napi_ok, BT_ERR_INVALID_PARAM);
561
562 auto func = [remoteAddr]() {
563 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
564 int32_t err = remoteDevice.CancelPairing();
565 HILOGI("err: %{public}d", err);
566 return NapiAsyncWorkRet(err);
567 };
568 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
569 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
570 asyncWork->Run();
571 return asyncWork->GetRet();
572 }
573
CheckPairCredibleDeviceParam(napi_env env,napi_callback_info info,std::string & addr,int & transport)574 napi_status CheckPairCredibleDeviceParam(napi_env env, napi_callback_info info, std::string &addr, int &transport)
575 {
576 size_t argc = ARGS_SIZE_THREE;
577 napi_value argv[ARGS_SIZE_THREE] = {nullptr};
578 NAPI_BT_CALL_RETURN(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
579 NAPI_BT_RETURN_IF(argc != ARGS_SIZE_TWO && argc != ARGS_SIZE_THREE, "Requires 2 or 3 arguments.", napi_invalid_arg);
580 NAPI_BT_CALL_RETURN(NapiParseBdAddr(env, argv[PARAM0], addr));
581 NAPI_BT_RETURN_IF(!ParseInt32(env, transport, argv[PARAM1]), "ParseInt32 failed", napi_invalid_arg);
582 NAPI_BT_RETURN_IF(!IsValidTransport(transport), "Invalid transport", napi_invalid_arg);
583 return napi_ok;
584 }
585
PairCredibleDevice(napi_env env,napi_callback_info info)586 napi_value PairCredibleDevice(napi_env env, napi_callback_info info)
587 {
588 HILOGD("enter");
589 std::shared_ptr<NapiHaEventUtils> haUtils =
590 std::make_shared<NapiHaEventUtils>(env, "connection.PairCredibleDevice");
591 std::string remoteAddr = INVALID_MAC_ADDRESS;
592 int transport = BT_TRANSPORT_NONE;
593 auto status = CheckPairCredibleDeviceParam(env, info, remoteAddr, transport);
594 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
595
596 auto func = [remoteAddr, transport]() {
597 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr, transport);
598 int32_t err = remoteDevice.StartCrediblePair();
599 HILOGI("err: %{public}d", err);
600 return NapiAsyncWorkRet(err);
601 };
602 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK, haUtils);
603 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
604 asyncWork->Run();
605 return asyncWork->GetRet();
606 }
607
CheckGetProfileUuids(napi_env env,napi_callback_info info,std::string & address)608 napi_status CheckGetProfileUuids(napi_env env, napi_callback_info info, std::string &address)
609 {
610 size_t argc = ARGS_SIZE_TWO;
611 napi_value argv[ARGS_SIZE_TWO] = {0};
612 NAPI_BT_CALL_RETURN(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
613 NAPI_BT_RETURN_IF(argc != ARGS_SIZE_ONE && argc != ARGS_SIZE_TWO, "Requires 1 or 2 arguments.", napi_invalid_arg);
614 NAPI_BT_CALL_RETURN(NapiParseBdAddr(env, argv[PARAM0], address));
615 return napi_ok;
616 }
617
GetLocalProfileUuids(napi_env env,napi_callback_info info)618 napi_value GetLocalProfileUuids(napi_env env, napi_callback_info info)
619 {
620 HILOGD("enter");
621 auto func = []() {
622 std::vector<std::string> uuids{};
623 int32_t err = BluetoothHost::GetDefaultHost().GetLocalProfileUuids(uuids);
624 HILOGI("err: %{public}d", err);
625 auto object = std::make_shared<NapiNativeUuidsArray>(uuids);
626 return NapiAsyncWorkRet(err, object);
627 };
628 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
629 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
630 asyncWork->Run();
631 return asyncWork->GetRet();
632 }
633
GetRemoteProfileUuids(napi_env env,napi_callback_info info)634 napi_value GetRemoteProfileUuids(napi_env env, napi_callback_info info)
635 {
636 HILOGD("enter");
637 std::string address;
638 auto status = CheckGetProfileUuids(env, info, address);
639 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
640 auto func = [address]() {
641 std::vector<std::string> uuids{};
642 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(address);
643 int32_t err = remoteDevice.GetDeviceUuids(uuids);
644 HILOGI("err: %{public}d", err);
645 auto object = std::make_shared<NapiNativeUuidsArray>(uuids);
646 return NapiAsyncWorkRet(err, object);
647 };
648 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
649 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
650 asyncWork->Run();
651 return asyncWork->GetRet();
652 }
653
IsBluetoothDiscovering(napi_env env,napi_callback_info info)654 napi_value IsBluetoothDiscovering(napi_env env, napi_callback_info info)
655 {
656 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
657 bool isDiscovering = false;
658 int32_t err = host->IsBtDiscovering(isDiscovering);
659 napi_value result = nullptr;
660 NAPI_BT_ASSERT_RETURN(env, napi_get_boolean(env, isDiscovering, &result) == napi_ok, err, result);
661 NAPI_BT_ASSERT_RETURN(env, err == BT_NO_ERROR, err, result);
662 HILOGE("isBluetoothDiscovering :%{public}d", isDiscovering);
663 return result;
664 }
665
GetPairState(napi_env env,napi_callback_info info)666 napi_value GetPairState(napi_env env, napi_callback_info info)
667 {
668 std::string remoteAddr = INVALID_MAC_ADDRESS;
669 bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
670 NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
671 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
672 int state = PAIR_NONE;
673 int32_t err = remoteDevice.GetPairState(state);
674 int pairState = static_cast<int>(BondState::BOND_STATE_INVALID);
675 DealPairStatus(state, pairState);
676 napi_value result = nullptr;
677 NAPI_BT_ASSERT_RETURN(env, napi_create_int32(env, pairState, &result) == napi_ok, err, result);
678 NAPI_BT_ASSERT_RETURN(env, (err == BT_NO_ERROR || err == BT_ERR_INTERNAL_ERROR), err, result);
679 HILOGI("getPairState :%{public}d", pairState);
680 return result;
681 }
682
ConnectAllowedProfiles(napi_env env,napi_callback_info info)683 napi_value ConnectAllowedProfiles(napi_env env, napi_callback_info info)
684 {
685 HILOGI("enter");
686 std::shared_ptr<NapiHaEventUtils> haUtils =
687 std::make_shared<NapiHaEventUtils>(env, "connection.ConnectAllowedProfiles");
688 std::string remoteAddr = INVALID_MAC_ADDRESS;
689 auto checkRet = CheckDeviceAsyncParam(env, info, remoteAddr);
690 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet == napi_ok, BT_ERR_INVALID_PARAM);
691
692 auto func = [remoteAddr]() {
693 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
694 int32_t ret = host->ConnectAllowedProfiles(remoteAddr);
695 HILOGI("ret: %{public}d", ret);
696 return NapiAsyncWorkRet(ret);
697 };
698 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK, haUtils);
699 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
700 asyncWork->Run();
701 return asyncWork->GetRet();
702 }
703
DisconnectAllowedProfiles(napi_env env,napi_callback_info info)704 napi_value DisconnectAllowedProfiles(napi_env env, napi_callback_info info)
705 {
706 HILOGI("enter");
707 std::shared_ptr<NapiHaEventUtils> haUtils =
708 std::make_shared<NapiHaEventUtils>(env, "connection.DisconnectAllowedProfiles");
709 std::string remoteAddr = INVALID_MAC_ADDRESS;
710 auto checkRet = CheckDeviceAsyncParam(env, info, remoteAddr);
711 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet == napi_ok, BT_ERR_INVALID_PARAM);
712
713 auto func = [remoteAddr]() {
714 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
715 int32_t ret = host->DisconnectAllowedProfiles(remoteAddr);
716 HILOGI("ret: %{public}d", ret);
717 return NapiAsyncWorkRet(ret);
718 };
719 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK, haUtils);
720 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
721 asyncWork->Run();
722 return asyncWork->GetRet();
723 }
724
GetRemoteProductId(napi_env env,napi_callback_info info)725 napi_value GetRemoteProductId(napi_env env, napi_callback_info info)
726 {
727 HILOGD("start");
728 std::string remoteAddr = INVALID_MAC_ADDRESS;
729 bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
730 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet, BT_ERR_INVALID_PARAM);
731
732 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
733 std::string productId;
734 int32_t err = remoteDevice.GetDeviceProductId(productId);
735
736 napi_value result = nullptr;
737 napi_create_string_utf8(env, productId.c_str(), productId.size(), &result);
738 NAPI_BT_ASSERT_RETURN(env, err == BT_NO_ERROR, err, result);
739 HILOGI("GetRemoteProductId :%{public}s", productId.c_str());
740 return result;
741 }
742
GetRemoteDeviceTransport(napi_env env,napi_callback_info info)743 napi_value GetRemoteDeviceTransport(napi_env env, napi_callback_info info)
744 {
745 std::string remoteAddr = INVALID_MAC_ADDRESS;
746 bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
747 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet, BT_ERR_INVALID_PARAM);
748 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
749 int32_t transport = static_cast<int32_t>(BluetoothTransport::TRANSPORT_UNKNOWN);
750 int32_t err = remoteDevice.GetDeviceTransport(transport);
751 napi_value result = nullptr;
752 NAPI_BT_ASSERT_RETURN(env, napi_create_int32(env, transport, &result) == napi_ok, err, result);
753 NAPI_BT_ASSERT_RETURN(env, err == BT_NO_ERROR, err, result);
754 return result;
755 }
756
757 #endif
758
ParseSetRemoteDeviceNameParameters(napi_env env,napi_callback_info info,std::string & outRemoteAddr,std::string & outDeviceName)759 napi_status ParseSetRemoteDeviceNameParameters(napi_env env, napi_callback_info info,
760 std::string &outRemoteAddr, std::string &outDeviceName)
761 {
762 HILOGD("enter");
763 std::string remoteAddr{};
764 std::string deviceName{};
765 size_t argc = ARGS_SIZE_TWO;
766 napi_value argv[ARGS_SIZE_TWO] = {nullptr};
767 NAPI_BT_CALL_RETURN(napi_get_cb_info(env, info, &argc, argv, nullptr, NULL));
768 NAPI_BT_RETURN_IF(argc != ARGS_SIZE_TWO, "Requires 2 arguments.", napi_invalid_arg);
769 NAPI_BT_CALL_RETURN(NapiParseBdAddr(env, argv[PARAM0], remoteAddr));
770 NAPI_BT_RETURN_IF(!ParseString(env, deviceName, argv[PARAM1]), "deviceName ParseString failed", napi_invalid_arg);
771 outRemoteAddr = remoteAddr;
772 outDeviceName = deviceName;
773 return napi_ok;
774 }
775
SetRemoteDeviceName(napi_env env,napi_callback_info info)776 napi_value SetRemoteDeviceName(napi_env env, napi_callback_info info)
777 {
778 HILOGD("enter");
779 std::string remoteAddr = "";
780 std::string deviceName = "";
781 auto status = ParseSetRemoteDeviceNameParameters(env, info, remoteAddr, deviceName);
782 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
783
784 auto func = [remoteAddr, deviceName]() {
785 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
786 int32_t err = remoteDevice.SetDeviceAlias(deviceName);
787 HILOGI("SetDeviceName err: %{public}d", err);
788 return NapiAsyncWorkRet(err);
789 };
790 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
791 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
792 asyncWork->Run();
793 return asyncWork->GetRet();
794 }
795
ParseSetRemoteDeviceTypeParameters(napi_env env,napi_callback_info info,std::string & outRemoteAddr,int32_t & outDeviceType)796 napi_status ParseSetRemoteDeviceTypeParameters(napi_env env, napi_callback_info info,
797 std::string &outRemoteAddr, int32_t &outDeviceType)
798 {
799 HILOGD("enter");
800 std::string remoteAddr{};
801 int32_t deviceType = DeviceType::DEVICE_TYPE_DEFAULT;
802 size_t argc = ARGS_SIZE_TWO;
803 napi_value argv[ARGS_SIZE_TWO] = {nullptr};
804 NAPI_BT_CALL_RETURN(napi_get_cb_info(env, info, &argc, argv, nullptr, NULL));
805 NAPI_BT_RETURN_IF(argc != ARGS_SIZE_TWO, "Requires 2 arguments.", napi_invalid_arg);
806 NAPI_BT_CALL_RETURN(NapiParseBdAddr(env, argv[PARAM0], remoteAddr));
807 NAPI_BT_RETURN_IF(!ParseInt32(env, deviceType, argv[PARAM1]), "deviceType ParseInt32 failed", napi_invalid_arg);
808 outRemoteAddr = remoteAddr;
809 outDeviceType = deviceType;
810 return napi_ok;
811 }
812
SetRemoteDeviceType(napi_env env,napi_callback_info info)813 napi_value SetRemoteDeviceType(napi_env env, napi_callback_info info)
814 {
815 HILOGD("enter");
816 std::string remoteAddr = INVALID_MAC_ADDRESS;
817 int32_t deviceType = DeviceType::DEVICE_TYPE_DEFAULT;
818 auto status = ParseSetRemoteDeviceTypeParameters(env, info, remoteAddr, deviceType);
819 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
820
821 auto func = [remoteAddr, deviceType]() {
822 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
823 int32_t err = remoteDevice.SetDeviceCustomType(deviceType);
824 HILOGI("SetRemoteDeviceType err: %{public}d", err);
825 return NapiAsyncWorkRet(err);
826 };
827 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
828 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
829 asyncWork->Run();
830 return asyncWork->GetRet();
831 }
832
GetRemoteDeviceType(napi_env env,napi_callback_info info)833 napi_value GetRemoteDeviceType(napi_env env, napi_callback_info info)
834 {
835 HILOGD("enter");
836 std::string remoteAddr;
837 bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
838 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet, BT_ERR_INVALID_PARAM);
839 auto func = [remoteAddr]() {
840 int32_t deviceType = DeviceType::DEVICE_TYPE_DEFAULT;
841 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
842 int32_t err = remoteDevice.GetDeviceCustomType(deviceType);
843 HILOGI("GetRemoteDeviceType err: %{public}d", err);
844 auto object = std::make_shared<NapiNativeInt>(deviceType);
845 return NapiAsyncWorkRet(err, object);
846 };
847 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
848 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
849 asyncWork->Run();
850 return asyncWork->GetRet();
851 }
852
GetRemoteDeviceBatteryInfo(napi_env env,napi_callback_info info)853 napi_value GetRemoteDeviceBatteryInfo(napi_env env, napi_callback_info info)
854 {
855 HILOGD("enter");
856 std::string remoteAddr = INVALID_MAC_ADDRESS;
857 auto checkRet = CheckDeivceIdParam(env, info, remoteAddr);
858 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet, BT_ERR_INVALID_PARAM);
859 auto func = [remoteAddr]() {
860 DeviceBatteryInfo batteryInfo;
861 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
862 int32_t err = remoteDevice.GetRemoteDeviceBatteryInfo(batteryInfo);
863 HILOGI("err: %{public}d", err);
864 auto object = std::make_shared<NapiNativeBatteryInfo>(batteryInfo);
865 return NapiAsyncWorkRet(err, object);
866 };
867 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
868 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
869 asyncWork->Run();
870 return asyncWork->GetRet();
871 }
872
ConnectionPropertyValueInit(napi_env env,napi_value exports)873 napi_value ConnectionPropertyValueInit(napi_env env, napi_value exports)
874 {
875 HILOGD("enter");
876 napi_value scanModeObj = ScanModeInit(env);
877 napi_value bondStateObj = BondStateInit(env);
878 napi_value unbondCauseObj = UnbondCauseInit(env);
879 #ifdef BLUETOOTH_API_SINCE_10
880 napi_value bluetoothTransportObject = BluetoothTransportInit(env);
881 napi_value pinTypeObject = PinTypeInit(env);
882 #endif
883 napi_value deviceTypeObject = DeviceTypeInit(env);
884 napi_value deviceChargeStateObject = DeviceChargeStateInit(env);
885 napi_property_descriptor exportProperties[] = {
886 DECLARE_NAPI_PROPERTY("ScanMode", scanModeObj),
887 DECLARE_NAPI_PROPERTY("BondState", bondStateObj),
888 DECLARE_NAPI_PROPERTY("UnbondCause", unbondCauseObj),
889 #ifdef BLUETOOTH_API_SINCE_10
890 DECLARE_NAPI_PROPERTY("BluetoothTransport", bluetoothTransportObject),
891 DECLARE_NAPI_PROPERTY("PinType", pinTypeObject),
892 #endif
893 DECLARE_NAPI_PROPERTY("DeviceType", deviceTypeObject),
894 DECLARE_NAPI_PROPERTY("DeviceChargeState", deviceChargeStateObject),
895 };
896 HITRACE_METER_NAME(HITRACE_TAG_OHOS, "connection:napi_define_properties");
897 napi_define_properties(env, exports, sizeof(exportProperties) / sizeof(*exportProperties), exportProperties);
898 return exports;
899 }
900
ScanModeInit(napi_env env)901 napi_value ScanModeInit(napi_env env)
902 {
903 HILOGD("enter");
904 napi_value scanMode = nullptr;
905 napi_create_object(env, &scanMode);
906 SetNamedPropertyByInteger(env, scanMode, static_cast<int>(ScanMode::SCAN_MODE_NONE), "SCAN_MODE_NONE");
907 SetNamedPropertyByInteger(
908 env, scanMode, static_cast<int>(ScanMode::SCAN_MODE_CONNECTABLE), "SCAN_MODE_CONNECTABLE");
909 SetNamedPropertyByInteger(
910 env, scanMode, static_cast<int>(ScanMode::SCAN_MODE_GENERAL_DISCOVERABLE), "SCAN_MODE_GENERAL_DISCOVERABLE");
911 SetNamedPropertyByInteger(
912 env, scanMode, static_cast<int>(ScanMode::SCAN_MODE_LIMITED_DISCOVERABLE), "SCAN_MODE_LIMITED_DISCOVERABLE");
913 SetNamedPropertyByInteger(env,
914 scanMode,
915 static_cast<int>(ScanMode::SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE),
916 "SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE");
917 SetNamedPropertyByInteger(env,
918 scanMode,
919 static_cast<int>(ScanMode::SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE),
920 "SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE");
921 return scanMode;
922 }
923
BondStateInit(napi_env env)924 napi_value BondStateInit(napi_env env)
925 {
926 HILOGD("enter");
927 napi_value bondState = nullptr;
928 napi_create_object(env, &bondState);
929 SetNamedPropertyByInteger(env, bondState, static_cast<int>(BondState::BOND_STATE_INVALID), "BOND_STATE_INVALID");
930 SetNamedPropertyByInteger(env, bondState, static_cast<int>(BondState::BOND_STATE_BONDING), "BOND_STATE_BONDING");
931 SetNamedPropertyByInteger(env, bondState, static_cast<int>(BondState::BOND_STATE_BONDED), "BOND_STATE_BONDED");
932 return bondState;
933 }
934
UnbondCauseInit(napi_env env)935 napi_value UnbondCauseInit(napi_env env)
936 {
937 HILOGD("enter");
938 napi_value unbondCause = nullptr;
939 napi_create_object(env, &unbondCause);
940 SetNamedPropertyByInteger(env, unbondCause, UNBOND_CAUSE_USER_REMOVED, "USER_REMOVED");
941 SetNamedPropertyByInteger(env, unbondCause, UNBOND_CAUSE_REMOTE_DEVICE_DOWN, "REMOTE_DEVICE_DOWN");
942 SetNamedPropertyByInteger(env, unbondCause, UNBOND_CAUSE_AUTH_FAILURE, "AUTH_FAILURE");
943 SetNamedPropertyByInteger(env, unbondCause, UNBOND_CAUSE_AUTH_REJECTED, "AUTH_REJECTED");
944 SetNamedPropertyByInteger(env, unbondCause, UNBOND_CAUSE_INTERNAL_ERROR, "INTERNAL_ERROR");
945 return unbondCause;
946 }
947
948 #ifdef BLUETOOTH_API_SINCE_10
BluetoothTransportInit(napi_env env)949 napi_value BluetoothTransportInit(napi_env env)
950 {
951 HILOGD("enter");
952 napi_value bluetoothTransport = nullptr;
953 napi_create_object(env, &bluetoothTransport);
954 SetNamedPropertyByInteger(
955 env, bluetoothTransport, static_cast<int>(BluetoothTransport::TRANSPORT_BR_EDR), "TRANSPORT_BR_EDR");
956 SetNamedPropertyByInteger(
957 env, bluetoothTransport, static_cast<int>(BluetoothTransport::TRANSPORT_LE), "TRANSPORT_LE");
958 SetNamedPropertyByInteger(
959 env, bluetoothTransport, static_cast<int>(BluetoothTransport::TRANSPORT_DUAL), "TRANSPORT_DUAL");
960 SetNamedPropertyByInteger(
961 env, bluetoothTransport, static_cast<int>(BluetoothTransport::TRANSPORT_UNKNOWN), "TRANSPORT_UNKNOWN");
962 return bluetoothTransport;
963 }
964
PinTypeInit(napi_env env)965 napi_value PinTypeInit(napi_env env)
966 {
967 HILOGD("enter");
968 napi_value pinType = nullptr;
969 napi_create_object(env, &pinType);
970 SetNamedPropertyByInteger(
971 env, pinType, static_cast<int>(PinType::PIN_TYPE_ENTER_PIN_CODE), "PIN_TYPE_ENTER_PIN_CODE");
972 SetNamedPropertyByInteger(
973 env, pinType, static_cast<int>(PinType::PIN_TYPE_ENTER_PASSKEY), "PIN_TYPE_ENTER_PASSKEY");
974 SetNamedPropertyByInteger(
975 env, pinType, static_cast<int>(PinType::PIN_TYPE_CONFIRM_PASSKEY), "PIN_TYPE_CONFIRM_PASSKEY");
976 SetNamedPropertyByInteger(
977 env, pinType, static_cast<int>(PinType::PIN_TYPE_NO_PASSKEY_CONSENT), "PIN_TYPE_NO_PASSKEY_CONSENT");
978 SetNamedPropertyByInteger(
979 env, pinType, static_cast<int>(PinType::PIN_TYPE_NOTIFY_PASSKEY), "PIN_TYPE_NOTIFY_PASSKEY");
980 SetNamedPropertyByInteger(
981 env, pinType, static_cast<int>(PinType::PIN_TYPE_DISPLAY_PIN_CODE), "PIN_TYPE_DISPLAY_PIN_CODE");
982 SetNamedPropertyByInteger(env, pinType, static_cast<int>(PinType::PIN_TYPE_OOB_CONSENT), "PIN_TYPE_OOB_CONSENT");
983 SetNamedPropertyByInteger(
984 env, pinType, static_cast<int>(PinType::PIN_TYPE_PIN_16_DIGITS), "PIN_TYPE_PIN_16_DIGITS");
985 return pinType;
986 }
987 #endif
988
DeviceTypeInit(napi_env env)989 napi_value DeviceTypeInit(napi_env env)
990 {
991 HILOGD("enter");
992 napi_value deviceType = nullptr;
993 napi_create_object(env, &deviceType);
994 SetNamedPropertyByInteger(
995 env, deviceType, static_cast<int>(DeviceType::DEVICE_TYPE_DEFAULT), "DEVICE_TYPE_DEFAULT");
996 SetNamedPropertyByInteger(
997 env, deviceType, static_cast<int>(DeviceType::DEVICE_TYPE_CAR), "DEVICE_TYPE_CAR");
998 SetNamedPropertyByInteger(
999 env, deviceType, static_cast<int>(DeviceType::DEVICE_TYPE_HEADSET), "DEVICE_TYPE_HEADSET");
1000 SetNamedPropertyByInteger(
1001 env, deviceType, static_cast<int>(DeviceType::DEVICE_TYPE_HEARING), "DEVICE_TYPE_HEARING");
1002 SetNamedPropertyByInteger(
1003 env, deviceType, static_cast<int>(DeviceType::DEVICE_TYPE_GLASSES), "DEVICE_TYPE_GLASSES");
1004 SetNamedPropertyByInteger(
1005 env, deviceType, static_cast<int>(DeviceType::DEVICE_TYPE_WATCH), "DEVICE_TYPE_WATCH");
1006 SetNamedPropertyByInteger(
1007 env, deviceType, static_cast<int>(DeviceType::DEVICE_TYPE_SPEAKER), "DEVICE_TYPE_SPEAKER");
1008 SetNamedPropertyByInteger(
1009 env, deviceType, static_cast<int>(DeviceType::DEVICE_TYPE_OTHERS), "DEVICE_TYPE_OTHERS");
1010 return deviceType;
1011 }
1012
DeviceChargeStateInit(napi_env env)1013 napi_value DeviceChargeStateInit(napi_env env)
1014 {
1015 HILOGD("enter");
1016 napi_value deviceChargeState = nullptr;
1017 napi_create_object(env, &deviceChargeState);
1018 SetNamedPropertyByInteger(
1019 env, deviceChargeState, static_cast<int32_t>(DeviceChargeState::DEVICE_NORMAL_CHARGE_NOT_CHARGED),
1020 "DEVICE_NORMAL_CHARGE_NOT_CHARGED");
1021 SetNamedPropertyByInteger(
1022 env, deviceChargeState, static_cast<int32_t>(DeviceChargeState::DEVICE_NORMAL_CHARGE_IN_CHARGING),
1023 "DEVICE_NORMAL_CHARGE_IN_CHARGING");
1024 SetNamedPropertyByInteger(
1025 env, deviceChargeState, static_cast<int32_t>(DeviceChargeState::DEVICE_SUPER_CHARGE_NOT_CHARGED),
1026 "DEVICE_SUPER_CHARGE_NOT_CHARGED");
1027 SetNamedPropertyByInteger(
1028 env, deviceChargeState, static_cast<int32_t>(DeviceChargeState::DEVICE_SUPER_CHARGE_IN_CHARGING),
1029 "DEVICE_SUPER_CHARGE_IN_CHARGING");
1030 return deviceChargeState;
1031 }
1032
RegisterObserverToHost()1033 void RegisterObserverToHost()
1034 {
1035 HILOGD("enter");
1036 BluetoothHost &host = BluetoothHost::GetDefaultHost();
1037 host.RegisterObserver(g_connectionObserver);
1038 host.RegisterRemoteDeviceObserver(g_remoteDeviceObserver);
1039 }
1040
DealPairStatus(const int & status,int & bondStatus)1041 void DealPairStatus(const int &status, int &bondStatus)
1042 {
1043 HILOGD("status is %{public}d", status);
1044 switch (status) {
1045 case PAIR_NONE:
1046 bondStatus = static_cast<int>(BondState::BOND_STATE_INVALID);
1047 break;
1048 case PAIR_PAIRING:
1049 bondStatus = static_cast<int>(BondState::BOND_STATE_BONDING);
1050 break;
1051 case PAIR_PAIRED:
1052 bondStatus = static_cast<int>(BondState::BOND_STATE_BONDED);
1053 break;
1054 default:
1055 break;
1056 }
1057 }
1058
1059 struct ControlDeviceActionParams {
1060 std::string deviceId;
1061 uint32_t controlType;
1062 uint32_t controlTypeVal;
1063 uint32_t controlObject;
1064 };
1065
ParseControlDeviceActionParams(napi_env env,napi_value object,ControlDeviceActionParams & params)1066 napi_status ParseControlDeviceActionParams(napi_env env, napi_value object, ControlDeviceActionParams ¶ms)
1067 {
1068 HILOGD("ParseControlDeviceActionParams enter");
1069 NAPI_BT_CALL_RETURN(NapiCheckObjectPropertiesName(env, object,
1070 {"deviceId", "type", "typeValue", "controlObject"}));
1071 std::string tmpDeviceId = INVALID_MAC_ADDRESS;
1072 NAPI_BT_CALL_RETURN(NapiParseObjectBdAddr(env, object, "deviceId", tmpDeviceId));
1073 if (tmpDeviceId.empty() || tmpDeviceId.length() != ADDRESS_LENGTH) {
1074 HILOGE("Invalid deviceId");
1075 return napi_invalid_arg;
1076 }
1077 uint32_t tmpControlType = INVALID_CONTROL_TYPE;
1078 uint32_t tmpControlTypeVal = INVALID_CONTROL_TYPE_VAL;
1079 uint32_t tmpControlObject = INVALID_CONTROL_OBJECT;
1080
1081 NapiObject napiObject = { env, object };
1082 NAPI_BT_CALL_RETURN(NapiParseObjectUint32Check(napiObject, "type", tmpControlType,
1083 ControlType::PLAY, ControlType::ERASE));
1084 NAPI_BT_CALL_RETURN(NapiParseObjectUint32Check(napiObject, "typeValue", tmpControlTypeVal,
1085 ControlTypeVal::DISABLE, ControlTypeVal::QUERY));
1086 NAPI_BT_CALL_RETURN(NapiParseObjectUint32Check(napiObject, "controlObject", tmpControlObject,
1087 ControlObject::LEFT_EAR, ControlObject::LEFT_RIGHT_EAR));
1088
1089 HILOGI("deviceId: %{public}s, controlType: %{public}u, controlTypeVal: %{public}u, controlObject: %{public}u",
1090 GetEncryptAddr(tmpDeviceId).c_str(), tmpControlType, tmpControlTypeVal, tmpControlObject);
1091
1092 params.deviceId = tmpDeviceId;
1093 params.controlType = tmpControlType;
1094 params.controlTypeVal = tmpControlTypeVal;
1095 params.controlObject = tmpControlObject;
1096 return napi_ok;
1097 }
1098
ControlDeviceAction(napi_env env,napi_callback_info info)1099 napi_value ControlDeviceAction(napi_env env, napi_callback_info info)
1100 {
1101 HILOGD("ControlDeviceAction enter");
1102 size_t argc = ARGS_SIZE_ONE;
1103 napi_value argv[ARGS_SIZE_ONE] = {nullptr};
1104 napi_value thisVar = nullptr;
1105 auto checkRes = napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL);
1106 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRes == napi_ok, BT_ERR_INVALID_PARAM);
1107
1108 NAPI_BT_ASSERT_RETURN_UNDEF(env, argc == ARGS_SIZE_ONE, BT_ERR_INVALID_PARAM);
1109
1110 ControlDeviceActionParams params = { INVALID_MAC_ADDRESS, INVALID_CONTROL_TYPE,
1111 INVALID_CONTROL_TYPE_VAL, INVALID_CONTROL_OBJECT };
1112 auto status = ParseControlDeviceActionParams(env, argv[PARAM0], params);
1113 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
1114 std::string deviceId = params.deviceId;
1115 uint32_t controlType = params.controlType;
1116 uint32_t controlTypeVal = params.controlTypeVal;
1117 uint32_t controlObject = params.controlObject;
1118 auto func = [deviceId, controlType, controlTypeVal, controlObject]() {
1119 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(deviceId);
1120 int32_t err = remoteDevice.ControlDeviceAction(controlType, controlTypeVal, controlObject);
1121 HILOGI("ControlDeviceAction err: %{public}d", err);
1122 return NapiAsyncWorkRet(err);
1123 };
1124 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
1125 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
1126 asyncWork->Run();
1127 return asyncWork->GetRet();
1128 }
1129
GetRemoteDeviceConnectionTime(napi_env env,napi_callback_info info)1130 napi_value GetRemoteDeviceConnectionTime(napi_env env, napi_callback_info info)
1131 {
1132 HILOGD("enter");
1133 std::string remoteAddr = INVALID_MAC_ADDRESS;
1134 bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
1135 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet, BT_ERR_INVALID_PARAM);
1136 auto func = [remoteAddr]() {
1137 int64_t connectionTime = 0;
1138 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
1139 int32_t err = remoteDevice.GetLastConnectionTime(connectionTime);
1140 HILOGI("GetRemoteDeviceConnectionTime GetLastConnectionTime err: %{public}d", err);
1141 auto object = std::make_shared<NapiNativeInt64>(connectionTime);
1142 return NapiAsyncWorkRet(err, object);
1143 };
1144 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
1145 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
1146 asyncWork->Run();
1147 return asyncWork->GetRet();
1148 }
1149
UpdateCloudBluetoothDevice(napi_env env,napi_callback_info info)1150 napi_value UpdateCloudBluetoothDevice(napi_env env, napi_callback_info info)
1151 {
1152 HILOGI("[CLOUD_DEV] UpdateCloudBluetoothDevice enter");
1153 size_t argc = ARGS_SIZE_ONE;
1154 napi_value argv[ARGS_SIZE_ONE] = {nullptr};
1155 napi_value thisVar = nullptr;
1156 auto checkRes = napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL);
1157 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRes == napi_ok, BT_ERR_INVALID_PARAM);
1158 std::vector<TrustPairDeviceParam> trustPairs {};
1159 auto status = NapiParseTrustPairDevice(env, argv[PARAM0], trustPairs);
1160 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
1161 auto func = [trustPairs]() {
1162 int32_t err = BluetoothHost::GetDefaultHost().UpdateCloudBluetoothDevice(trustPairs);
1163 HILOGI("[CLOUD_DEV] UpdateCloudBluetoothDevice err: %{public}d", err);
1164 return NapiAsyncWorkRet(err);
1165 };
1166 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
1167 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
1168 asyncWork->Run();
1169 return asyncWork->GetRet();
1170 }
1171 } // namespace Bluetooth
1172 } // namespace OHOS