• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "napi_bluetooth_connection_observer.h"
16 
17 #include "bluetooth_host.h"
18 #include "bluetooth_log.h"
19 #include "bluetooth_utils.h"
20 
21 #include "napi/native_api.h"
22 #include "napi/native_node_api.h"
23 #include "napi_bluetooth_connection.h"
24 #include "napi_event_subscribe_module.h"
25 
26 #include <uv.h>
27 
28 namespace OHOS {
29 namespace Bluetooth {
NapiBluetoothConnectionObserver()30 NapiBluetoothConnectionObserver::NapiBluetoothConnectionObserver()
31     : eventSubscribe_({REGISTER_DEVICE_FIND_TYPE,
32         REGISTER_DISCOVERY_RESULT_TYPE,
33         REGISTER_PIN_REQUEST_TYPE},
34         BT_MODULE_NAME)
35 {}
36 
OnDiscoveryStateChanged(int status)37 void NapiBluetoothConnectionObserver::OnDiscoveryStateChanged(int status)
38 {
39     switch (status) {
40         case DISCOVERY_STARTED:
41             HILOGD("DISCOVERY_STARTED(1)");
42             break;
43         case DISCOVERYING:
44             HILOGD("DISCOVERYING(2)");
45             break;
46         case DISCOVERY_STOPED:
47             HILOGD("DISCOVERY_STOPED(3)");
48             break;
49         default:
50             HILOGE("invaild status is %{public}d", status);
51             break;
52     }
53 }
54 
OnDiscoveryResult(const BluetoothRemoteDevice & device,int rssi,const std::string deviceName,int deviceClass)55 void NapiBluetoothConnectionObserver::OnDiscoveryResult(
56     const BluetoothRemoteDevice &device, int rssi, const std::string deviceName, int deviceClass)
57 {
58     OnDiscoveryResultCallBack(device);
59     OnDiscoveryResultCallBack(device, rssi, deviceName, deviceClass);
60 }
61 
OnPairRequested(const BluetoothRemoteDevice & device)62 void NapiBluetoothConnectionObserver::OnPairRequested(const BluetoothRemoteDevice &device)
63 {
64     HILOGD("start");
65     BluetoothRemoteDevice remoteDevice;
66     if (device.GetTransportType() == BT_TRANSPORT_BREDR) {
67         remoteDevice = BluetoothHost::GetDefaultHost().GetRemoteDevice(device.GetDeviceAddr(), BT_TRANSPORT_BREDR);
68     } else if (device.GetTransportType() == BT_TRANSPORT_BLE) {
69         remoteDevice = BluetoothHost::GetDefaultHost().GetRemoteDevice(device.GetDeviceAddr(), BT_TRANSPORT_BLE);
70     }
71     remoteDevice.PairRequestReply(true);
72 }
73 
OnPairConfirmed(const BluetoothRemoteDevice & device,int reqType,int number)74 void NapiBluetoothConnectionObserver::OnPairConfirmed(const BluetoothRemoteDevice &device, int reqType, int number)
75 {
76     HILOGD("OnPairConfirmed");
77     std::shared_ptr<PairConfirmedCallBackInfo> pairConfirmInfo =
78         std::make_shared<PairConfirmedCallBackInfo>(number, reqType, device.GetDeviceAddr());
79     OnPairConfirmedCallBack(pairConfirmInfo);
80 }
81 
OnScanModeChanged(int mode)82 void NapiBluetoothConnectionObserver::OnScanModeChanged(int mode)
83 {
84     HILOGI("mode is %{public}d", mode);
85 }
86 
OnDeviceNameChanged(const std::string & deviceName)87 void NapiBluetoothConnectionObserver::OnDeviceNameChanged(const std::string &deviceName)
88 {
89     HILOGI("name is %{public}s", deviceName.c_str());
90 }
91 
OnDeviceAddrChanged(const std::string & address)92 void NapiBluetoothConnectionObserver::OnDeviceAddrChanged(const std::string &address)
93 {
94     HILOGI("address is %{public}s", GetEncryptAddr(address).c_str());
95 }
96 
OnPairConfirmedCallBack(const std::shared_ptr<PairConfirmedCallBackInfo> & pairConfirmInfo)97 void NapiBluetoothConnectionObserver::OnPairConfirmedCallBack(
98     const std::shared_ptr<PairConfirmedCallBackInfo> &pairConfirmInfo)
99 {
100     CHECK_AND_RETURN_LOG(pairConfirmInfo, "pairConfirmInfo is nullptr");
101     HILOGI("Addr: %{public}s", GetEncryptAddr(pairConfirmInfo->deviceAddr).c_str());
102 
103     auto nativeObject = std::make_shared<NapiNativePinRequiredParam>(pairConfirmInfo);
104     eventSubscribe_.PublishEvent(REGISTER_PIN_REQUEST_TYPE, nativeObject);
105 }
106 
OnDiscoveryResultCallBack(const BluetoothRemoteDevice & device)107 void NapiBluetoothConnectionObserver::OnDiscoveryResultCallBack(const BluetoothRemoteDevice &device)
108 {
109     std::shared_ptr<BluetoothRemoteDevice> remoteDevice = std::make_shared<BluetoothRemoteDevice>(device);
110     auto nativeObject = std::make_shared<NapiNativeDiscoveryResultArray>(remoteDevice);
111     eventSubscribe_.PublishEvent(REGISTER_DEVICE_FIND_TYPE, nativeObject);
112 }
113 
OnDiscoveryResultCallBack(const BluetoothRemoteDevice & device,int rssi,const std::string & deviceName,int deviceClass)114 void NapiBluetoothConnectionObserver::OnDiscoveryResultCallBack(
115     const BluetoothRemoteDevice &device, int rssi, const std::string &deviceName, int deviceClass)
116 {
117     std::shared_ptr<BluetoothRemoteDevice> remoteDevice = std::make_shared<BluetoothRemoteDevice>(device);
118     auto nativeObject =
119         std::make_shared<NapiNativeDiscoveryInfoResultArray>(remoteDevice, rssi, deviceName, deviceClass);
120     eventSubscribe_.PublishEvent(REGISTER_DISCOVERY_RESULT_TYPE, nativeObject);
121 }
122 }  // namespace Bluetooth
123 }  // namespace OHOS
124