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_remote_device_observer"
17 #endif
18
19 #include <map>
20 #include <memory>
21 #include <string>
22
23 #include "bluetooth_log.h"
24 #include "bluetooth_utils.h"
25 #include "napi_bluetooth_remote_device_observer.h"
26 #include "napi_bluetooth_utils.h"
27 #include "napi_bluetooth_connection.h"
28
29 namespace OHOS {
30 namespace Bluetooth {
NapiBluetoothRemoteDeviceObserver()31 NapiBluetoothRemoteDeviceObserver::NapiBluetoothRemoteDeviceObserver()
32 : eventSubscribe_({REGISTER_BOND_STATE_TYPE,
33 REGISTER_BATTERY_CHANGE_TYPE},
34 BT_MODULE_NAME)
35 {}
36
OnAclStateChanged(const BluetoothRemoteDevice & device,int state,unsigned int reason)37 void NapiBluetoothRemoteDeviceObserver::OnAclStateChanged(
38 const BluetoothRemoteDevice &device, int state, unsigned int reason)
39 {}
40
OnPairStatusChanged(const BluetoothRemoteDevice & device,int status,int cause)41 void NapiBluetoothRemoteDeviceObserver::OnPairStatusChanged(const BluetoothRemoteDevice &device, int status, int cause)
42 {
43 int bondStatus = 0;
44 DealPairStatus(status, bondStatus);
45 HILOGI("addr:%{public}s, bondStatus:%{public}d, cause:%{public}d", GET_ENCRYPT_ADDR(device), bondStatus, cause);
46
47 auto nativeObject = std::make_shared<NapiNativeBondStateParam>(device.GetDeviceAddr(), bondStatus, cause);
48 eventSubscribe_.PublishEvent(REGISTER_BOND_STATE_TYPE, nativeObject);
49 }
50
OnRemoteUuidChanged(const BluetoothRemoteDevice & device,const std::vector<ParcelUuid> & uuids)51 void NapiBluetoothRemoteDeviceObserver ::OnRemoteUuidChanged(
52 const BluetoothRemoteDevice &device, const std::vector<ParcelUuid> &uuids)
53 {
54 HILOGD("called");
55 }
56
OnRemoteNameChanged(const BluetoothRemoteDevice & device,const std::string & deviceName)57 void NapiBluetoothRemoteDeviceObserver ::OnRemoteNameChanged(
58 const BluetoothRemoteDevice &device, const std::string &deviceName)
59 {
60 HILOGD("addr:%{public}s, deviceName:%{public}s", GET_ENCRYPT_ADDR(device), deviceName.c_str());
61 }
62
OnRemoteAliasChanged(const BluetoothRemoteDevice & device,const std::string & alias)63 void NapiBluetoothRemoteDeviceObserver ::OnRemoteAliasChanged(
64 const BluetoothRemoteDevice &device, const std::string &alias)
65 {
66 HILOGD("addr:%{public}s, alias:%{public}s", GET_ENCRYPT_ADDR(device), alias.c_str());
67 }
68
OnRemoteCodChanged(const BluetoothRemoteDevice & device,const BluetoothDeviceClass & cod)69 void NapiBluetoothRemoteDeviceObserver ::OnRemoteCodChanged(
70 const BluetoothRemoteDevice &device, const BluetoothDeviceClass &cod)
71 {
72 HILOGD("addr:%{public}s, cod:%{public}d", GET_ENCRYPT_ADDR(device), cod.GetClassOfDevice());
73 }
74
OnRemoteBatteryLevelChanged(const BluetoothRemoteDevice & device,int batteryLevel)75 void NapiBluetoothRemoteDeviceObserver ::OnRemoteBatteryLevelChanged(
76 const BluetoothRemoteDevice &device, int batteryLevel)
77 {
78 HILOGD("addr:%{public}s, batteryLevel:%{public}d", GET_ENCRYPT_ADDR(device), batteryLevel);
79 }
80
OnRemoteBatteryChanged(const BluetoothRemoteDevice & device,const DeviceBatteryInfo & batteryInfo)81 void NapiBluetoothRemoteDeviceObserver ::OnRemoteBatteryChanged(
82 const BluetoothRemoteDevice &device, const DeviceBatteryInfo &batteryInfo)
83 {
84 auto nativeObject = std::make_shared<NapiNativeBatteryInfo>(batteryInfo);
85 eventSubscribe_.PublishEvent(REGISTER_BATTERY_CHANGE_TYPE, nativeObject);
86 }
87
OnReadRemoteRssiEvent(const BluetoothRemoteDevice & device,int rssi,int status)88 void NapiBluetoothRemoteDeviceObserver ::OnReadRemoteRssiEvent(
89 const BluetoothRemoteDevice &device, int rssi, int status)
90 {
91 HILOGD("addr:%{public}s, rssi:%{public}d, status is %{public}d", GET_ENCRYPT_ADDR(device), rssi, status);
92 }
93 } // namespace Bluetooth
94 } // namespace OHOS