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