• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 "ohos.bluetooth.connection.proj.hpp"
17 #include "ohos.bluetooth.connection.impl.hpp"
18 #include "taihe/runtime.hpp"
19 #include "stdexcept"
20 #include "bluetooth_remote_device.h"
21 #include "bluetooth_host.h"
22 #include "bluetooth_log.h"
23 #include "bluetooth_errorcode.h"
24 
25 using namespace taihe;
26 using namespace ohos::bluetooth::connection;
27 
28 namespace {
29 
GetRemoteProductId(string_view deviceId)30 string GetRemoteProductId(string_view deviceId)
31 {
32     HILOGI("enter");
33     std::string remoteAddr = std::string(deviceId);
34     OHOS::Bluetooth::BluetoothRemoteDevice remoteDevice = OHOS::Bluetooth::BluetoothRemoteDevice(remoteAddr);
35     std::string productId = "";
36     int32_t err = remoteDevice.GetDeviceProductId(productId);
37     if (err != OHOS::Bluetooth::BT_NO_ERROR) {
38         set_business_error(err, "GetRemoteProductId return error");
39     }
40     return productId;
41 }
42 
GetRemoteDeviceName(string_view deviceId)43 string GetRemoteDeviceName(string_view deviceId)
44 {
45     HILOGI("enter");
46     std::string remoteAddr = std::string(deviceId);
47     std::string name = OHOS::Bluetooth::INVALID_NAME;
48     bool deviceAlias = false;
49 
50     OHOS::Bluetooth::BluetoothRemoteDevice remoteDevice = OHOS::Bluetooth::BluetoothRemoteDevice(remoteAddr);
51     int32_t err = remoteDevice.GetDeviceName(name, deviceAlias);
52     if (err != OHOS::Bluetooth::BT_NO_ERROR) {
53         set_business_error(err, "GetRemoteDeviceName return error");
54     }
55     return name;
56 }
57 
GetRemoteDeviceNameWithAlias(string_view deviceId,optional_view<bool> alias)58 string GetRemoteDeviceNameWithAlias(string_view deviceId, optional_view<bool> alias)
59 {
60     HILOGI("enter");
61     std::string remoteAddr = std::string(deviceId);
62     std::string name = OHOS::Bluetooth::INVALID_NAME;
63     bool deviceAlias = static_cast<bool>(alias);
64 
65     OHOS::Bluetooth::BluetoothRemoteDevice remoteDevice = OHOS::Bluetooth::BluetoothRemoteDevice(remoteAddr);
66     int32_t err = remoteDevice.GetDeviceName(name, deviceAlias);
67     if (err != OHOS::Bluetooth::BT_NO_ERROR) {
68         set_business_error(err, "GetRemoteDeviceNameWithAlias return error");
69     }
70     return name;
71 }
72 
GetPairedDevices()73 array<string> GetPairedDevices()
74 {
75     HILOGI("enter");
76     OHOS::Bluetooth::BluetoothHost *host = &OHOS::Bluetooth::BluetoothHost::GetDefaultHost();
77     std::vector<OHOS::Bluetooth::BluetoothRemoteDevice> remoteDeviceLists;
78     std::vector<std::string> dstDevicesvec;
79     int32_t err = host->GetPairedDevices(OHOS::Bluetooth::BT_TRANSPORT_BREDR, remoteDeviceLists);
80     if (err != OHOS::Bluetooth::BT_NO_ERROR) {
81         set_business_error(err, "GetPairedDevices return error");
82         return {};
83     }
84     for (auto vec : remoteDeviceLists) {
85         dstDevicesvec.push_back(vec.GetDeviceAddr().c_str());
86     }
87     array<string> result(taihe::copy_data_t{}, dstDevicesvec.data(), dstDevicesvec.size());
88     return result;
89 }
90 
SetBluetoothScanMode(ScanMode mode,double duration)91 void SetBluetoothScanMode(ScanMode mode, double duration)
92 {
93     HILOGI("enter");
94     OHOS::Bluetooth::BluetoothHost *host = &OHOS::Bluetooth::BluetoothHost::GetDefaultHost();
95     int32_t bondableMode = 1;
96     int32_t err = host->SetBtScanMode(mode, duration);
97     if (err != OHOS::Bluetooth::BT_NO_ERROR) {
98         set_business_error(err, "SetBluetoothScanMode return error");
99         return;
100     }
101     host->SetBondableMode(OHOS::Bluetooth::BT_TRANSPORT_BREDR, bondableMode);
102 }
103 
GetRemoteDeviceClass(string_view deviceId)104 DeviceClass GetRemoteDeviceClass(string_view deviceId)
105 {
106     HILOGI("enter");
107     std::string remoteAddr = std::string(deviceId);
108     OHOS::Bluetooth::BluetoothRemoteDevice remoteDevice = OHOS::Bluetooth::BluetoothRemoteDevice(remoteAddr);
109     int tmpCod = ohos::bluetooth::constant::MajorClass(
110         ohos::bluetooth::constant::MajorClass::key_t::MAJOR_UNCATEGORIZED).get_value();
111     int tmpMajorClass = ohos::bluetooth::constant::MajorClass(
112         ohos::bluetooth::constant::MajorClass::key_t::MAJOR_UNCATEGORIZED).get_value();
113     int tmpMajorMinorClass = ohos::bluetooth::constant::MajorMinorClass(
114         ohos::bluetooth::constant::MajorMinorClass::key_t::COMPUTER_UNCATEGORIZED).get_value();
115     int32_t err = remoteDevice.GetDeviceProductType(tmpCod, tmpMajorClass, tmpMajorMinorClass);
116     if (err != OHOS::Bluetooth::BT_NO_ERROR) {
117         set_business_error(err, "GetRemoteDeviceClass return error");
118     }
119     return {ohos::bluetooth::constant::MajorClass::from_value(tmpMajorClass),
120         ohos::bluetooth::constant::MajorMinorClass::from_value(tmpMajorMinorClass), tmpCod};
121 }
122 }  // namespace
123 
124 // Since these macros are auto-generate, lint will cause false positive.
125 // NOLINTBEGIN
126 TH_EXPORT_CPP_API_GetRemoteProductId(GetRemoteProductId);
127 TH_EXPORT_CPP_API_GetRemoteDeviceName(GetRemoteDeviceName);
128 TH_EXPORT_CPP_API_GetRemoteDeviceNameWithAlias(GetRemoteDeviceNameWithAlias);
129 TH_EXPORT_CPP_API_GetPairedDevices(GetPairedDevices);
130 TH_EXPORT_CPP_API_SetBluetoothScanMode(SetBluetoothScanMode);
131 TH_EXPORT_CPP_API_GetRemoteDeviceClass(GetRemoteDeviceClass);
132 // NOLINTEND