1 /*
2  * Copyright (C) 2024 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 "napi/native_api.h"
16 #include <ConnectivityKit/bluetooth/oh_bluetooth.h>
17 #include "hilog/log.h"
18 #include <cstdint>
19 
20 #undef LOG_DOMAIN
21 #undef LOG_TAG
22 #define LOG_DOMAIN 0x2300 // 全局domain宏,标识业务领域
23 #define LOG_TAG "MY_TAG"   // 全局tag宏,标识模块日志tag
24 
GetBluetoothSwitchState(napi_env env, napi_callback_info info)25 static napi_value GetBluetoothSwitchState(napi_env env, napi_callback_info info)
26 {
27     Bluetooth_SwitchState state;
28     auto code = OH_Bluetooth_GetBluetoothSwitchState(&state);
29     OH_LOG_INFO(LOG_APP, "testTag ret: %{public}d, state: %{public}d",
30         static_cast<int>(code),
31         static_cast<int>(state));
32 
33     int32_t stateValue = static_cast<int32_t>(state);
34     napi_value res;
35     napi_create_uint32(env, stateValue, &res);
36     return res;
37 }
38 
39 EXTERN_C_START
Init(napi_env env, napi_value exports)40 static napi_value Init(napi_env env, napi_value exports)
41 {
42     napi_property_descriptor desc[] = {
43         { "GetBluetoothSwitchState", nullptr, GetBluetoothSwitchState,
44           nullptr, nullptr, nullptr, napi_default, nullptr },
45     };
46     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
47     return exports;
48 }
49 EXTERN_C_END
50 
51 static napi_module demoModule = {
52     .nm_version = 1,
53     .nm_flags = 0,
54     .nm_filename = nullptr,
55     .nm_register_func = Init,
56     .nm_modname = "entry",
57     .nm_priv = ((void*)0),
58     .reserved = { 0 },
59 };
60 
RegisterEntryModule(void)61 extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
62 {
63     napi_module_register(&demoModule);
64 }
65