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