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 
16 #include "bluetooth_access_impl.h"
17 #include "bluetooth_log.h"
18 #include "napi_bluetooth_utils.h"
19 
20 namespace OHOS {
21 namespace CJSystemapi {
22 namespace CJBluetoothAccess {
23 using Bluetooth::BluetoothHost;
24 using Bluetooth::BTStateID;
25 using Bluetooth::BluetoothState;
26 
EnableBluetooth(int32_t* errCode)27 void AccessImpl::EnableBluetooth(int32_t* errCode)
28 {
29     BluetoothHost *host = &BluetoothHost::GetDefaultHost();
30     *errCode = host->EnableBle();
31     return;
32 }
33 
DisableBluetooth(int32_t* errCode)34 void AccessImpl::DisableBluetooth(int32_t* errCode)
35 {
36     BluetoothHost *host = &BluetoothHost::GetDefaultHost();
37     *errCode = host->DisableBt();
38     return;
39 }
40 
GetState(int32_t* errCode)41 int32_t AccessImpl::GetState(int32_t* errCode)
42 {
43     BluetoothHost *host = &BluetoothHost::GetDefaultHost();
44     int32_t state = BTStateID::STATE_TURN_OFF;
45     *errCode = host->GetBtState(state);
46     int32_t status = static_cast<int32_t>(BluetoothState::STATE_OFF);
47     switch (state) {
48         case BTStateID::STATE_TURNING_ON:
49             HILOGD("STATE_TURNING_ON(1)");
50             status = static_cast<int32_t>(BluetoothState::STATE_TURNING_ON);
51             break;
52         case BTStateID::STATE_TURN_ON:
53             HILOGD("STATE_ON(2)");
54             status = static_cast<int32_t>(BluetoothState::STATE_ON);
55             break;
56         case BTStateID::STATE_TURNING_OFF:
57             HILOGD("STATE_TURNING_OFF(3)");
58             status = static_cast<int32_t>(BluetoothState::STATE_TURNING_OFF);
59             break;
60         case BTStateID::STATE_TURN_OFF:
61             HILOGD("STATE_OFF(0)");
62             status = static_cast<int32_t>(BluetoothState::STATE_OFF);
63             break;
64         default:
65             HILOGE("get state failed");
66             break;
67     }
68 
69     bool enableBle = host->IsBleEnabled();
70     if (enableBle && (state == BTStateID::STATE_TURN_OFF)) {
71         HILOGD("BR off and BLE on, STATE_BLE_ON(5)");
72         status = static_cast<int32_t>(BluetoothState::STATE_BLE_ON);
73     } else if (!enableBle && (state == BTStateID::STATE_TURN_OFF)) {
74         status = static_cast<int32_t>(BluetoothState::STATE_OFF);
75     }
76     return status;
77 }
78 } // namespace CJBluetoothAccess
79 } // namespace CJSystemapi
80 } // namespace OHOS