1 /*
2  * Copyright (C) 2021 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 <string>
17 
18 #include "bluetooth_raw_address.h"
19 #include "bluetooth_def.h"
20 #include "bluetooth_host.h"
21 #include "bluetooth_device.h"
22 #include "bluetooth_host_proxy.h"
23 #include "bluetooth_log.h"
24 #include "bluetooth_utils.h"
25 #include "bluetooth_profile_manager.h"
26 #include "bluetooth_remote_device.h"
27 #include "iservice_registry.h"
28 #include "system_ability_definition.h"
29 #include "bluetooth_audio_manager.h"
30 
31 using namespace OHOS::bluetooth;
32 
33 namespace OHOS {
34 namespace Bluetooth {
BluetoothRemoteDevice(const std::string &addr, const int transport)35 BluetoothRemoteDevice::BluetoothRemoteDevice(const std::string &addr, const int transport)
36 {
37     address_ = addr;
38     transport_ = transport;
39 }
40 
GetDeviceType() const41 int BluetoothRemoteDevice::GetDeviceType() const
42 {
43     HILOGI("enter");
44     int type = 0;
45     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), type, "Invalid remote device.");
46 
47     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
48     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, type, "proxy is nullptr.");
49 
50     return hostProxy->GetDeviceType(transport_, address_);
51 }
52 
IsValidBluetoothRemoteDevice() const53 bool BluetoothRemoteDevice::IsValidBluetoothRemoteDevice() const
54 {
55     CHECK_AND_RETURN_LOG_RET(BluetoothHost::IsValidBluetoothAddr(address_), false,
56         "invalid bluetooth addr, address_: %{public}s", GetEncryptAddr(address_).c_str());
57 
58     CHECK_AND_RETURN_LOG_RET(transport_ == BT_TRANSPORT_BREDR ||
59         transport_ == BT_TRANSPORT_BLE || transport_ == BT_TRANSPORT_NONE,
60         false, "invalid transport type.");
61     return true;
62 }
63 
GetTransportType() const64 int BluetoothRemoteDevice::GetTransportType() const
65 {
66     return transport_;
67 }
68 
GetPhonebookPermission() const69 int BluetoothRemoteDevice::GetPhonebookPermission() const
70 {
71     HILOGI("enter");
72     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), INVALID_VALUE, "Invalid remote device.");
73     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
74     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, INVALID_VALUE, "proxy is nullptr.");
75 
76     return hostProxy->GetPhonebookPermission(address_);
77 }
78 
SetPhonebookPermission(int permission)79 bool BluetoothRemoteDevice::SetPhonebookPermission(int permission)
80 {
81     HILOGI("enter, permission: %{public}d", permission);
82     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), false, "Invalid remote device.");
83     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
84     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, false, "proxy is nullptr.");
85     return hostProxy->SetPhonebookPermission(address_, permission);
86 }
87 
GetMessagePermission() const88 int BluetoothRemoteDevice::GetMessagePermission() const
89 {
90     HILOGI("enter");
91     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), INVALID_VALUE, "Invalid remote device.");
92     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
93     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, INVALID_VALUE, "proxy is nullptr.");
94     return hostProxy->GetMessagePermission(address_);
95 }
96 
SetMessagePermission(int permission)97 bool BluetoothRemoteDevice::SetMessagePermission(int permission)
98 {
99     HILOGI("enter, permission: %{public}d", permission);
100     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), false, "Invalid remote device.");
101     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
102     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, false, "proxy is nullptr.");
103     return hostProxy->SetMessagePermission(address_, permission);
104 }
105 
GetPowerMode(void) const106 int BluetoothRemoteDevice::GetPowerMode(void) const
107 {
108     HILOGI("enter");
109     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), INVALID_VALUE, "Invalid remote device.");
110     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
111     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, INVALID_VALUE, "proxy is nullptr.");
112     return hostProxy->GetPowerMode(address_);
113 }
114 
GetDeviceName() const115 std::string BluetoothRemoteDevice::GetDeviceName() const
116 {
117     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), INVALID_NAME, "Invalid remote device");
118     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), INVALID_NAME, "bluetooth is off.");
119     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
120     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, INVALID_NAME, "proxy is nullptr.");
121     std::string name = INVALID_NAME;
122     hostProxy->GetDeviceName(transport_, address_, name);
123     return name;
124 }
125 
GetDeviceName(std::string &name) const126 int BluetoothRemoteDevice::GetDeviceName(std::string &name) const
127 {
128     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device");
129     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
130     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
131     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr.");
132     return hostProxy->GetDeviceName(transport_, address_, name);
133 }
134 
GetDeviceAlias() const135 std::string BluetoothRemoteDevice::GetDeviceAlias() const
136 {
137     HILOGI("enter");
138     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), INVALID_NAME, "Invalid remote device");
139     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
140     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, INVALID_NAME, "proxy is nullptr.");
141     return hostProxy->GetDeviceAlias(address_);
142 }
143 
SetDeviceAlias(const std::string &aliasName)144 int32_t BluetoothRemoteDevice::SetDeviceAlias(const std::string &aliasName)
145 {
146     HILOGI("enter");
147     CHECK_AND_RETURN_LOG_RET(
148         IsValidBluetoothRemoteDevice() && aliasName != INVALID_NAME, BT_ERR_INVALID_PARAM, "Invalid remote device");
149     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
150 
151     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
152     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INVALID_STATE, "proxy is nullptr.");
153     return hostProxy->SetDeviceAlias(address_, aliasName);
154 }
155 
GetRemoteDeviceBatteryInfo(DeviceBatteryInfo &batteryInfo) const156 int BluetoothRemoteDevice::GetRemoteDeviceBatteryInfo(DeviceBatteryInfo &batteryInfo) const
157 {
158     HILOGI("enter");
159     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device.");
160     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
161     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr.");
162     BluetoothBatteryInfo bluetoothBatteryInfo;
163     int32_t ret = hostProxy->GetRemoteDeviceBatteryInfo(address_, bluetoothBatteryInfo);
164     CHECK_AND_RETURN_LOG_RET(ret == BT_NO_ERROR, ret, "GetRemoteDeviceBatteryInfo fail");
165     batteryInfo.deviceId_ = address_;
166     batteryInfo.batteryLevel_ = bluetoothBatteryInfo.batteryLevel_;
167     batteryInfo.leftEarBatteryLevel_ = bluetoothBatteryInfo.leftEarBatteryLevel_;
168     batteryInfo.leftEarChargeState_ = static_cast<DeviceChargeState>(bluetoothBatteryInfo.leftEarChargeState_);
169     batteryInfo.rightEarBatteryLevel_ = bluetoothBatteryInfo.rightEarBatteryLevel_;
170     batteryInfo.rightEarChargeState_ = static_cast<DeviceChargeState>(bluetoothBatteryInfo.rightEarChargeState_);
171     batteryInfo.boxBatteryLevel_ = bluetoothBatteryInfo.boxBatteryLevel_;
172     batteryInfo.boxChargeState_ = static_cast<DeviceChargeState>(bluetoothBatteryInfo.boxChargeState_);
173     return ret;
174 }
175 
GetPairState(int &pairState) const176 int BluetoothRemoteDevice::GetPairState(int &pairState) const
177 {
178     HILOGI("enter");
179     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), INVALID_VALUE, "Invalid remote device.");
180     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
181     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, INVALID_VALUE, "proxy is nullptr.");
182     return hostProxy->GetPairState(transport_, address_, pairState);
183 }
184 
StartPair()185 int BluetoothRemoteDevice::StartPair()
186 {
187     HILOGI("enter");
188     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device");
189     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
190     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
191     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr.");
192     return hostProxy->StartPair(transport_, address_);
193 }
194 
StartCrediblePair()195 int BluetoothRemoteDevice::StartCrediblePair()
196 {
197     HILOGI("enter");
198     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device");
199     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
200     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
201     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr.");
202     return hostProxy->StartCrediblePair(transport_, address_);
203 }
204 
CancelPairing()205 int BluetoothRemoteDevice::CancelPairing()
206 {
207     HILOGI("enter");
208     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device");
209     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
210     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
211     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr.");
212     if (hostProxy->CancelPairing(transport_, address_)) {
213         return BT_NO_ERROR;
214     }
215     return  BT_ERR_INTERNAL_ERROR;
216 }
217 
IsBondedFromLocal() const218 bool BluetoothRemoteDevice::IsBondedFromLocal() const
219 {
220     HILOGI("enter");
221     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), false, "Invalid remote device");
222     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
223     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, false, "proxy is nullptr.");
224     return hostProxy->IsBondedFromLocal(transport_, address_);
225 }
226 
IsAclConnected() const227 bool BluetoothRemoteDevice::IsAclConnected() const
228 {
229     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), false, "Invalid remote device");
230     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
231     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, false, "proxy is nullptr.");
232     return hostProxy->IsAclConnected(transport_, address_);
233 }
234 
IsAclEncrypted() const235 bool BluetoothRemoteDevice::IsAclEncrypted() const
236 {
237     HILOGI("enter");
238     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), false, "Invalid remote device");
239     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
240     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, false, "proxy is nullptr.");
241     return hostProxy->IsAclEncrypted(transport_, address_);
242 }
243 
GetDeviceClass(int &cod) const244 int BluetoothRemoteDevice::GetDeviceClass(int &cod) const
245 {
246     HILOGD("enter");
247     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device");
248     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
249     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
250     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr.");
251     int ret = hostProxy->GetDeviceClass(address_, cod);
252     return ret;
253 }
254 
GetDeviceProductId(std::string &prodcutId) const255 int BluetoothRemoteDevice::GetDeviceProductId(std::string &prodcutId) const
256 {
257     HILOGD("enter");
258     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device");
259     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
260 
261     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
262     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr.");
263     std::shared_ptr<BluetoothRemoteDeviceInfo> info;
264     int32_t exception = hostProxy->GetRemoteDeviceInfo(address_, info, DeviceInfoType::DEVICE_MODEL_ID);
265     if (exception == BT_NO_ERROR && info != nullptr) {
266         prodcutId = info->modelId_;
267     }
268     return exception;
269 }
270 
GetDeviceUuids(std::vector<std::string> &uuids) const271 int BluetoothRemoteDevice::GetDeviceUuids(std::vector<std::string> &uuids) const
272 {
273     HILOGI("enter");
274     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device");
275     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
276     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr.");
277     return hostProxy->GetDeviceUuids(address_, uuids);
278 }
279 
SetDevicePin(const std::string &pin)280 int BluetoothRemoteDevice::SetDevicePin(const std::string &pin)
281 {
282     HILOGI("enter");
283     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device");
284     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
285     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr.");
286     return hostProxy->SetDevicePin(address_, pin);
287 }
288 
SetDevicePairingConfirmation(bool accept)289 int BluetoothRemoteDevice::SetDevicePairingConfirmation(bool accept)
290 {
291     HILOGI("enter, accept: %{public}d", accept);
292     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device");
293     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
294     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
295     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr.");
296     return hostProxy->SetDevicePairingConfirmation(transport_, address_, accept);
297 }
298 
SetDevicePasskey(int passkey, bool accept)299 bool BluetoothRemoteDevice::SetDevicePasskey(int passkey, bool accept)
300 {
301     HILOGI("enter, accept: %{public}d", accept);
302     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), false, "Invalid remote device");
303     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
304     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, false, "proxy is nullptr.");
305     return hostProxy->SetDevicePasskey(transport_, address_, passkey, accept);
306 }
307 
PairRequestReply(bool accept)308 bool BluetoothRemoteDevice::PairRequestReply(bool accept)
309 {
310     HILOGI("enter, accept: %{public}d", accept);
311     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), false, "Invalid remote device");
312     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
313     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, false, "proxy is nullptr.");
314     return hostProxy->PairRequestReply(transport_, address_, accept);
315 }
316 
ReadRemoteRssiValue()317 bool BluetoothRemoteDevice::ReadRemoteRssiValue()
318 {
319     HILOGI("enter");
320     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), false, "Invalid remote device");
321     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
322     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, false, "proxy is nullptr.");
323     return hostProxy->ReadRemoteRssiValue(address_);
324 }
325 
GetDeviceProductType(int &cod, int &majorClass, int &majorMinorClass) const326 int BluetoothRemoteDevice::GetDeviceProductType(int &cod, int &majorClass, int &majorMinorClass) const
327 {
328     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "Invalid remote device");
329 
330     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
331 
332     int deviceCod = 0;
333     int ret = GetDeviceClass(deviceCod);
334     BluetoothDeviceClass deviceClass = BluetoothDeviceClass(deviceCod);
335     cod = deviceClass.GetClassOfDevice();
336     majorClass = deviceClass.GetMajorClass();
337     majorMinorClass = deviceClass.GetMajorMinorClass();
338     if (cod == 0) {
339         HILOGW("cod = %{public}d", cod);
340         cod = BluetoothDevice::MAJOR_UNCATEGORIZED;
341         majorClass = BluetoothDevice::MAJOR_UNCATEGORIZED;
342         majorMinorClass = BluetoothDevice::MAJOR_UNCATEGORIZED;
343     }
344     HILOGD("device %{public}s cod = %{public}#X, majorClass = %{public}#X, majorMinorClass = %{public}#X",
345         GetEncryptAddr(address_).c_str(), cod, majorClass, majorMinorClass);
346 
347     return ret;
348 }
349 
SetDeviceCustomType(int32_t deviceType) const350 int32_t BluetoothRemoteDevice::SetDeviceCustomType(int32_t deviceType) const
351 {
352     HILOGI("enter");
353     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice() && deviceType >= DeviceType::DEVICE_TYPE_DEFAULT &&
354         deviceType <= DeviceType::DEVICE_TYPE_SPEAKER, BT_ERR_INVALID_PARAM,
355         "Invalid remote device or Invalid device Type");
356     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
357 
358     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
359     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INVALID_STATE, "proxy is nullptr.");
360     return hostProxy->SetDeviceCustomType(address_, deviceType);
361 }
362 
GetDeviceCustomType(int32_t &deviceType) const363 int32_t BluetoothRemoteDevice::GetDeviceCustomType(int32_t &deviceType) const
364 {
365     HILOGI("enter");
366     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "Invalid remote device");
367     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
368 
369     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
370     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INVALID_STATE, "proxy is nullptr.");
371     std::shared_ptr<BluetoothRemoteDeviceInfo> info;
372     int32_t exception = hostProxy->GetRemoteDeviceInfo(address_, info, DeviceInfoType::DEVICE_CUSTOM_TYPE);
373     if (exception == BT_NO_ERROR && info != nullptr) {
374         deviceType = info->customType_;
375     }
376     return exception;
377 }
378 
GetDeviceVendorId(uint16_t &vendorId) const379 int32_t BluetoothRemoteDevice::GetDeviceVendorId(uint16_t &vendorId) const
380 {
381     HILOGD("enter");
382     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote Device");
383     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
384     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
385     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
386     std::shared_ptr<BluetoothRemoteDeviceInfo> info;
387     int32_t exception = hostProxy->GetRemoteDeviceInfo(address_, info, DeviceInfoType::DEVICE_VENDOR_ID);
388     if (exception == BT_NO_ERROR && info != nullptr) {
389         vendorId = info->vendorId_;
390     }
391     return exception;
392 }
393 
GetDeviceProductId(uint16_t &productId) const394 int32_t BluetoothRemoteDevice::GetDeviceProductId(uint16_t &productId) const
395 {
396     HILOGD("enter");
397     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote Device");
398     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
399     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
400     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
401     std::shared_ptr<BluetoothRemoteDeviceInfo> info;
402     int32_t exception = hostProxy->GetRemoteDeviceInfo(address_, info, DeviceInfoType::DEVICE_PRODUCT_ID);
403     if (exception == BT_NO_ERROR && info != nullptr) {
404         productId = info->productId_;
405     }
406     return exception;
407 }
408 
IsSupportVirtualAutoConnect(bool &outSupport) const409 int32_t BluetoothRemoteDevice::IsSupportVirtualAutoConnect(bool &outSupport) const
410 {
411     HILOGD("enter");
412     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote Device");
413     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
414     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
415     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
416     int32_t exception = hostProxy->IsSupportVirtualAutoConnect(address_, outSupport);
417     return exception;
418 }
419 
SetVirtualAutoConnectType(int connType, int businessType) const420 int32_t BluetoothRemoteDevice::SetVirtualAutoConnectType(int connType, int businessType) const
421 {
422     HILOGD("enter");
423     CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote Device");
424     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
425     sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
426     CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
427     int32_t exception = hostProxy->SetVirtualAutoConnectType(address_, connType, businessType);
428     return exception;
429 }
430 }  // namespace Bluetooth
431 }  // namespace OHOS
432