1 /*
2 * Copyright (C) 2021-2022 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 "bluetooth_ble_central_manager.h"
16 #include "bluetooth_ble_central_manager_callback_stub.h"
17 #include "bluetooth_def.h"
18 #include "bluetooth_host.h"
19 #include "bluetooth_log.h"
20 #include "bluetooth_observer_list.h"
21 #include "bluetooth_utils.h"
22 #include "i_bluetooth_ble_central_manager.h"
23 #include "iservice_registry.h"
24 #include "system_ability_definition.h"
25 #include "bluetooth_profile_manager.h"
26
27 namespace OHOS {
28 namespace Bluetooth {
29 struct BleCentralManager::impl {
30 impl();
31 ~impl();
32
33 void ConvertBleScanSetting(const BleScanSettings &inSettings, BluetoothBleScanSettings &outSetting);
34 void ConvertBleScanFilter(const std::vector<BleScanFilter> &filters,
35 std::vector<BluetoothBleScanFilter> &bluetoothBleScanFilters);
36 void ConvertAdvertiserSetting(const BleAdvertiserSettings &inSettings, BluetoothBleAdvertiserSettings &outSettings);
37 void ConvertActiveDeviceInfo(const std::vector<BleActiveDeviceInfo> &inDeviceInfos,
38 std::vector<BluetoothActiveDeviceInfo> &outDeviceInfos);
39 bool InitScannerId(void);
40 int32_t CheckScanParams(const BleScanSettings &settings, const std::vector<BleScanFilter> &filters);
41
42 class BluetoothBleCentralManagerCallbackImp : public BluetoothBleCentralManagerCallBackStub {
43 public:
BluetoothBleCentralManagerCallbackImp()44 explicit BluetoothBleCentralManagerCallbackImp() {};
45 ~BluetoothBleCentralManagerCallbackImp() override = default;
46 void OnScanCallback(const BluetoothBleScanResult &result, uint8_t callbackType) override
47 {
48 callbacks_.ForEach(
49 [callbackType, &result](std::shared_ptr<BleCentralManagerCallback> observer) {
50 BluetoothBleScanResult tempResult(result);
51 BleScanResult scanResult;
52 for (auto &manufacturerData : tempResult.GetManufacturerData()) {
53 scanResult.AddManufacturerData(manufacturerData.first, manufacturerData.second);
54 }
55
56 for (auto &serviceUuidData : tempResult.GetServiceUuids()) {
57 UUID uuid = UUID::ConvertFrom128Bits(serviceUuidData.ConvertTo128Bits());
58 scanResult.AddServiceUuid(uuid);
59 }
60
61 for (auto &serviceData : tempResult.GetServiceData()) {
62 UUID uuid = UUID::ConvertFrom128Bits(serviceData.first.ConvertTo128Bits());
63 scanResult.AddServiceData(uuid, serviceData.second);
64 }
65
66 scanResult.SetAdvertiseFlag(tempResult.GetAdvertiseFlag());
67 scanResult.SetRssi(tempResult.GetRssi());
68 scanResult.SetConnectable(tempResult.IsConnectable());
69 BluetoothRemoteDevice device(tempResult.GetPeripheralDevice().GetAddress(), BT_TRANSPORT_BLE);
70 scanResult.SetPeripheralDevice(device);
71 scanResult.SetPayload(tempResult.GetPayload());
72 scanResult.SetName(tempResult.GetName());
73 scanResult.SetEventType(tempResult.GetEventType());
74 std::string address = result.GetPeripheralDevice().GetAddress();
75 HILOGI("device: %{public}s, len: %{public}d",
76 GetEncryptAddr(address).c_str(), static_cast<int>(scanResult.GetPayload().size()));
77 if (callbackType == BLE_SCAN_CALLBACK_TYPE_ALL_MATCH) {
78 observer->OnScanCallback(scanResult);
79 } else {
80 observer->OnFoundOrLostCallback(scanResult, callbackType);
81 }
82 });
83 }
84
85 void OnBleBatchScanResultsEvent(std::vector<BluetoothBleScanResult> &results) override
86 {
87 HILOGI("enter");
88 callbacks_.ForEach([&results](std::shared_ptr<BleCentralManagerCallback> observer) {
89 std::vector<BleScanResult> scanResults;
90 for (auto &result : results) {
91 BleScanResult scanResult;
92 for (auto &manufacturerData : result.GetManufacturerData()) {
93 scanResult.AddManufacturerData(manufacturerData.first, manufacturerData.second);
94 }
95
96 for (auto &serviceData : result.GetServiceData()) {
97 UUID uuid = UUID::ConvertFrom128Bits(serviceData.first.ConvertTo128Bits());
98 scanResult.AddServiceData(uuid, serviceData.second);
99 }
100
101 for (auto &serviceUuidData : result.GetServiceUuids()) {
102 UUID uuid = UUID::ConvertFrom128Bits(serviceUuidData.ConvertTo128Bits());
103 scanResult.AddServiceUuid(uuid);
104 }
105
106 scanResult.SetAdvertiseFlag(result.GetAdvertiseFlag());
107 scanResult.SetConnectable(result.IsConnectable());
108 scanResult.SetRssi(result.GetRssi());
109 BluetoothRemoteDevice device(result.GetPeripheralDevice().GetAddress(), BT_TRANSPORT_BLE);
110 scanResult.SetPeripheralDevice(device);
111 scanResult.SetPayload(result.GetPayload());
112
113 scanResults.push_back(scanResult);
114 }
115
116 observer->OnBleBatchScanResultsEvent(scanResults);
117 });
118 }
119
120 void OnStartOrStopScanEvent(int resultCode, bool isStartScan) override
121 {
122 HILOGD("resultCode: %{public}d, isStartScan: %{public}d", resultCode, isStartScan);
123 callbacks_.ForEach(
124 [resultCode, isStartScan](std::shared_ptr<BleCentralManagerCallback> observer) {
125 observer->OnStartOrStopScanEvent(resultCode, isStartScan);
126 });
127 }
128
129 void OnNotifyMsgReportFromLpDevice(const bluetooth::Uuid &uuid, int msgType,
130 const std::vector<uint8_t> &value) override
131 {
132 callbacks_.ForEach(
133 [uuid, msgType, value](std::shared_ptr<BleCentralManagerCallback> observer) {
134 UUID btUuid = UUID::ConvertFrom128Bits(uuid.ConvertTo128Bits());
135 observer->OnNotifyMsgReportFromLpDevice(btUuid, msgType, value);
136 });
137 }
138 public:
139 BluetoothObserverList<BleCentralManagerCallback> callbacks_;
140 private:
141 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothBleCentralManagerCallbackImp);
142 };
143 sptr<BluetoothBleCentralManagerCallbackImp> callbackImp_ = nullptr;
144
145 int32_t scannerId_ = BLE_SCAN_INVALID_ID; // lock by scannerIdMutex_
146 std::mutex scannerIdMutex_ {};
147 bool enableRandomAddrMode_ = true;
148 int32_t profileRegisterId = 0;
149 };
150
impl()151 BleCentralManager::impl::impl()
152 {
153 callbackImp_ = new BluetoothBleCentralManagerCallbackImp();
154 auto bleTurnOnFunc = [this](sptr<IRemoteObject> remote) {
155 sptr<IBluetoothBleCentralManager> proxy = iface_cast<IBluetoothBleCentralManager>(remote);
156 CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
157 std::lock_guard<std::mutex> lock(scannerIdMutex_);
158 if (scannerId_ == BLE_SCAN_INVALID_ID) {
159 proxy->RegisterBleCentralManagerCallback(scannerId_, enableRandomAddrMode_, callbackImp_);
160 }
161 };
162 auto bluetoothTurnOffFunc = [this]() {
163 scannerId_ = BLE_SCAN_INVALID_ID;
164 };
165 ProfileFunctions profileFunctions = {
166 .bluetoothLoadedfunc = nullptr,
167 .bleTurnOnFunc = bleTurnOnFunc,
168 .bluetoothTurnOffFunc = bluetoothTurnOffFunc,
169 };
170 profileRegisterId = BluetoothProfileManager::GetInstance().RegisterFunc(
171 BLE_CENTRAL_MANAGER_SERVER, profileFunctions);
172 }
173
InitScannerId(void)174 bool BleCentralManager::impl::InitScannerId(void)
175 {
176 sptr<IBluetoothBleCentralManager> proxy =
177 GetRemoteProxy<IBluetoothBleCentralManager>(BLE_CENTRAL_MANAGER_SERVER);
178 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
179
180 std::lock_guard<std::mutex> lock(scannerIdMutex_);
181 if (scannerId_ != BLE_SCAN_INVALID_ID) {
182 HILOGI("scannerId(%{public}d) is already registered", scannerId_);
183 return true;
184 }
185
186 proxy->RegisterBleCentralManagerCallback(scannerId_, enableRandomAddrMode_, callbackImp_);
187 return scannerId_ != BLE_SCAN_INVALID_ID;
188 }
189
ConvertBleScanSetting(const BleScanSettings &inSettings, BluetoothBleScanSettings &outSetting)190 void BleCentralManager::impl::ConvertBleScanSetting(const BleScanSettings &inSettings,
191 BluetoothBleScanSettings &outSetting)
192 {
193 outSetting.SetReportDelay(0);
194 outSetting.SetScanMode(inSettings.GetScanMode());
195 outSetting.SetLegacy(inSettings.GetLegacy());
196 outSetting.SetPhy(inSettings.GetPhy());
197 outSetting.SetCallbackType(inSettings.GetCallbackType());
198 outSetting.SetMatchTrackAdvType(inSettings.GetMatchTrackAdvType());
199 }
200
ConvertBleScanFilter(const std::vector<BleScanFilter> &filters, std::vector<BluetoothBleScanFilter> &bluetoothBleScanFilters)201 void BleCentralManager::impl::ConvertBleScanFilter(const std::vector<BleScanFilter> &filters,
202 std::vector<BluetoothBleScanFilter> &bluetoothBleScanFilters)
203 {
204 for (auto filter : filters) {
205 BluetoothBleScanFilter scanFilter;
206 scanFilter.SetDeviceId(filter.GetDeviceId());
207 scanFilter.SetName(filter.GetName());
208 if (filter.HasServiceUuid()) {
209 scanFilter.SetServiceUuid(bluetooth::Uuid::ConvertFromString(
210 filter.GetServiceUuid().ToString()));
211 }
212 if (filter.HasServiceUuidMask()) {
213 scanFilter.SetServiceUuidMask(bluetooth::Uuid::ConvertFromString(
214 filter.GetServiceUuidMask().ToString()));
215 }
216 if (filter.HasSolicitationUuid()) {
217 scanFilter.SetServiceSolicitationUuid(bluetooth::Uuid::ConvertFromString(
218 filter.GetServiceSolicitationUuid().ToString()));
219 }
220 if (filter.HasSolicitationUuidMask()) {
221 scanFilter.SetServiceSolicitationUuidMask(bluetooth::Uuid::ConvertFromString(
222 filter.GetServiceSolicitationUuidMask().ToString()));
223 }
224 scanFilter.SetServiceData(filter.GetServiceData());
225 scanFilter.SetServiceDataMask(filter.GetServiceDataMask());
226 scanFilter.SetManufacturerId(filter.GetManufacturerId());
227 scanFilter.SetManufactureData(filter.GetManufactureData());
228 scanFilter.SetManufactureDataMask(filter.GetManufactureDataMask());
229 scanFilter.SetAdvIndReportFlag(filter.GetAdvIndReportFlag());
230 bluetoothBleScanFilters.push_back(scanFilter);
231 }
232 }
233
ConvertAdvertiserSetting(const BleAdvertiserSettings &inSettings, BluetoothBleAdvertiserSettings &outSettings)234 void BleCentralManager::impl::ConvertAdvertiserSetting(const BleAdvertiserSettings &inSettings,
235 BluetoothBleAdvertiserSettings &outSettings)
236 {
237 outSettings.SetConnectable(inSettings.IsConnectable());
238 outSettings.SetInterval(inSettings.GetInterval());
239 outSettings.SetLegacyMode(inSettings.IsLegacyMode());
240 outSettings.SetTxPower(inSettings.GetTxPower());
241 }
242
ConvertActiveDeviceInfo(const std::vector<BleActiveDeviceInfo> &inDeviceInfos, std::vector<BluetoothActiveDeviceInfo> &outDeviceInfos)243 void BleCentralManager::impl::ConvertActiveDeviceInfo(const std::vector<BleActiveDeviceInfo> &inDeviceInfos,
244 std::vector<BluetoothActiveDeviceInfo> &outDeviceInfos)
245 {
246 for (auto info : inDeviceInfos) {
247 BluetoothActiveDeviceInfo deviceInfo;
248 deviceInfo.deviceId = info.deviceId;
249 deviceInfo.status = info.status;
250 deviceInfo.timeOut = info.timeOut;
251 outDeviceInfos.push_back(deviceInfo);
252 }
253 }
254
CheckScanParams(const BleScanSettings &settings, const std::vector<BleScanFilter> &filters)255 int32_t BleCentralManager::impl::CheckScanParams(const BleScanSettings &settings,
256 const std::vector<BleScanFilter> &filters)
257 {
258 uint8_t callbackType = settings.GetCallbackType();
259 if (callbackType != BLE_SCAN_CALLBACK_TYPE_ALL_MATCH &&
260 callbackType != BLE_SCAN_CALLBACK_TYPE_FIRST_MATCH &&
261 callbackType != BLE_SCAN_CALLBACK_TYPE_LOST_MATCH &&
262 callbackType != BLE_SCAN_CALLBACK_TYPE_FIRST_AND_LOST_MATCH) {
263 HILOGE("Illegal callbackType argument %{public}d", callbackType);
264 return BT_ERR_INVALID_PARAM;
265 }
266
267 if ((callbackType & BLE_SCAN_CALLBACK_TYPE_FIRST_AND_LOST_MATCH) != 0) {
268 if (filters.size() == 0) {
269 HILOGE("onFound/onLost need non-empty filters callbackType: %{public}d", callbackType);
270 return BT_ERR_INVALID_PARAM;
271 }
272 for (auto filter : filters) {
273 BleScanFilter emptyFilter;
274 if (filter == emptyFilter) {
275 HILOGE("onFound/onLost need non-empty filter callbackType: %{public}d", callbackType);
276 return BT_ERR_INVALID_PARAM;
277 }
278 }
279 }
280
281 uint8_t matchTrackAdvType = settings.GetMatchTrackAdvType();
282 if (matchTrackAdvType < ONE_MATCH_TRACK_ADV || matchTrackAdvType > MAX_MATCH_TRACK_ADV) {
283 HILOGE("Illegal matchTrackAdvType argument %{public}d", matchTrackAdvType);
284 return BT_ERR_INVALID_PARAM;
285 }
286
287 return BT_NO_ERROR;
288 }
289
~impl()290 BleCentralManager::impl::~impl()
291 {
292 HILOGD("start");
293 BluetoothProfileManager::GetInstance().DeregisterFunc(profileRegisterId);
294 sptr<IBluetoothBleCentralManager> proxy =
295 GetRemoteProxy<IBluetoothBleCentralManager>(BLE_CENTRAL_MANAGER_SERVER);
296 CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
297 proxy->DeregisterBleCentralManagerCallback(scannerId_, callbackImp_);
298 }
299
BleCentralManager(BleCentralManagerCallback &callback)300 BleCentralManager::BleCentralManager(BleCentralManagerCallback &callback) : pimpl(nullptr)
301 {
302 if (pimpl == nullptr) {
303 pimpl = std::make_unique<impl>();
304 if (pimpl == nullptr) {
305 HILOGE("failed, no pimpl");
306 return;
307 }
308 }
309
310 HILOGI("successful");
311 std::shared_ptr<BleCentralManagerCallback> pointer(&callback, [](BleCentralManagerCallback *) {});
312 bool ret = pimpl->callbackImp_->callbacks_.Register(pointer);
313 if (ret)
314 return;
315 }
316
BleCentralManager(std::shared_ptr<BleCentralManagerCallback> callback, bool enableRandomAddrMode)317 BleCentralManager::BleCentralManager(std::shared_ptr<BleCentralManagerCallback> callback, bool enableRandomAddrMode)
318 : pimpl(nullptr)
319 {
320 if (pimpl == nullptr) {
321 pimpl = std::make_unique<impl>();
322 if (pimpl == nullptr) {
323 HILOGE("failed, no pimpl");
324 return;
325 }
326 }
327 HILOGI("successful");
328 pimpl->enableRandomAddrMode_ = enableRandomAddrMode;
329 pimpl->callbackImp_->callbacks_.Register(callback);
330 }
331
~BleCentralManager()332 BleCentralManager::~BleCentralManager()
333 {
334 HILOGD("~BleCentralManager");
335 }
336
StartScan(const BleScanSettings &settings, const std::vector<BleScanFilter> &filters)337 int BleCentralManager::StartScan(const BleScanSettings &settings, const std::vector<BleScanFilter> &filters)
338 {
339 if (!IS_BLE_ENABLED()) {
340 HILOGD("bluetooth is off.");
341 return BT_ERR_INVALID_STATE;
342 }
343
344 int ret = pimpl->CheckScanParams(settings, filters);
345 if (ret != BT_NO_ERROR) {
346 return ret;
347 }
348
349 if (pimpl->scannerId_ == BLE_SCAN_INVALID_ID && !pimpl->InitScannerId()) {
350 HILOGE("init scannerId failed");
351 return BT_ERR_INTERNAL_ERROR;
352 }
353
354 HILOGD("StartScan with params, scannerId: %{public}d, callbackType: %{public}d",
355 pimpl->scannerId_, settings.GetCallbackType());
356 BluetoothBleScanSettings parcelSettings;
357 pimpl->ConvertBleScanSetting(settings, parcelSettings);
358
359 std::vector<BluetoothBleScanFilter> parcelFilters;
360 pimpl->ConvertBleScanFilter(filters, parcelFilters);
361
362 sptr<IBluetoothBleCentralManager> proxy =
363 GetRemoteProxy<IBluetoothBleCentralManager>(BLE_CENTRAL_MANAGER_SERVER);
364 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "failed: no proxy");
365 return proxy->StartScan(pimpl->scannerId_, parcelSettings, parcelFilters);
366 }
367
StopScan()368 int BleCentralManager::StopScan()
369 {
370 if (!IS_BLE_ENABLED()) {
371 HILOGD("bluetooth is off.");
372 return BT_ERR_INVALID_STATE;
373 }
374
375 if (pimpl->scannerId_ == BLE_SCAN_INVALID_ID && !pimpl->InitScannerId()) {
376 HILOGE("init scannerId failed");
377 return BT_ERR_INTERNAL_ERROR;
378 }
379
380 sptr<IBluetoothBleCentralManager> proxy =
381 GetRemoteProxy<IBluetoothBleCentralManager>(BLE_CENTRAL_MANAGER_SERVER);
382 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "failed: no proxy");
383 HILOGD("scannerId_: %{public}d", pimpl->scannerId_);
384
385 int ret = proxy->StopScan(pimpl->scannerId_);
386 proxy->RemoveScanFilter(pimpl->scannerId_);
387 return ret;
388 }
389
SetLpDeviceAdvParam(int duration, int maxExtAdvEvents, int window, int interval, int advHandle)390 int BleCentralManager::SetLpDeviceAdvParam(int duration, int maxExtAdvEvents, int window, int interval, int advHandle)
391 {
392 if (!IS_BLE_ENABLED()) {
393 HILOGE("bluetooth is off.");
394 return BT_ERR_INTERNAL_ERROR;
395 }
396
397 sptr<IBluetoothBleCentralManager> proxy =
398 GetRemoteProxy<IBluetoothBleCentralManager>(BLE_CENTRAL_MANAGER_SERVER);
399 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "failed: no proxy");
400 return proxy->SetLpDeviceAdvParam(duration, maxExtAdvEvents, window, interval, advHandle);
401 }
402
SetScanReportChannelToLpDevice(bool enable)403 int BleCentralManager::SetScanReportChannelToLpDevice(bool enable)
404 {
405 if (!IS_BLE_ENABLED()) {
406 HILOGE("bluetooth is off.");
407 return BT_ERR_INTERNAL_ERROR;
408 }
409
410 // need start scan first
411 if (pimpl->scannerId_ == BLE_SCAN_INVALID_ID && !pimpl->InitScannerId()) {
412 HILOGE("init scannerId failed");
413 return BT_ERR_INTERNAL_ERROR;
414 }
415 sptr<IBluetoothBleCentralManager> proxy =
416 GetRemoteProxy<IBluetoothBleCentralManager>(BLE_CENTRAL_MANAGER_SERVER);
417 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "failed: no proxy");
418 return proxy->SetScanReportChannelToLpDevice(pimpl->scannerId_, enable);
419 }
420
EnableSyncDataToLpDevice()421 int BleCentralManager::EnableSyncDataToLpDevice()
422 {
423 if (!IS_BLE_ENABLED()) {
424 HILOGE("bluetooth is off.");
425 return BT_ERR_INTERNAL_ERROR;
426 }
427
428 sptr<IBluetoothBleCentralManager> proxy =
429 GetRemoteProxy<IBluetoothBleCentralManager>(BLE_CENTRAL_MANAGER_SERVER);
430 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "failed: no proxy");
431 return proxy->EnableSyncDataToLpDevice();
432 }
433
DisableSyncDataToLpDevice()434 int BleCentralManager::DisableSyncDataToLpDevice()
435 {
436 if (!IS_BLE_ENABLED()) {
437 HILOGE("bluetooth is off.");
438 return BT_ERR_INTERNAL_ERROR;
439 }
440
441 sptr<IBluetoothBleCentralManager> proxy =
442 GetRemoteProxy<IBluetoothBleCentralManager>(BLE_CENTRAL_MANAGER_SERVER);
443 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "failed: no proxy");
444 return proxy->DisableSyncDataToLpDevice();
445 }
446
SendParamsToLpDevice(const std::vector<uint8_t> &dataValue, int32_t type)447 int BleCentralManager::SendParamsToLpDevice(const std::vector<uint8_t> &dataValue, int32_t type)
448 {
449 if (!IS_BLE_ENABLED()) {
450 HILOGE("bluetooth is off.");
451 return BT_ERR_INTERNAL_ERROR;
452 }
453
454 sptr<IBluetoothBleCentralManager> proxy =
455 GetRemoteProxy<IBluetoothBleCentralManager>(BLE_CENTRAL_MANAGER_SERVER);
456 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "failed: no proxy");
457 return proxy->SendParamsToLpDevice(dataValue, type);
458 }
459
IsLpDeviceAvailable()460 bool BleCentralManager::IsLpDeviceAvailable()
461 {
462 if (!IS_BLE_ENABLED()) {
463 HILOGE("bluetooth is off.");
464 return false;
465 }
466
467 sptr<IBluetoothBleCentralManager> proxy =
468 GetRemoteProxy<IBluetoothBleCentralManager>(BLE_CENTRAL_MANAGER_SERVER);
469 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
470 return proxy->IsLpDeviceAvailable();
471 }
472
SetLpDeviceParam(const BleLpDeviceParamSet &lpDeviceParamSet)473 int BleCentralManager::SetLpDeviceParam(const BleLpDeviceParamSet &lpDeviceParamSet)
474 {
475 if (!IS_BLE_ENABLED()) {
476 HILOGE("bluetooth is off.");
477 return BT_ERR_INTERNAL_ERROR;
478 }
479
480 BluetoothLpDeviceParamSet paramSet;
481 if ((lpDeviceParamSet.fieldValidFlagBit & BLE_LPDEVICE_SCAN_SETTING_VALID_BIT) != 0) {
482 pimpl->ConvertBleScanSetting(lpDeviceParamSet.scanSettings, paramSet.btScanSettings);
483 }
484
485 if ((lpDeviceParamSet.fieldValidFlagBit & BLE_LPDEVICE_SCAN_FILTER_VALID_BIT) != 0) {
486 pimpl->ConvertBleScanFilter(lpDeviceParamSet.scanFilters, paramSet.btScanFilters);
487 }
488
489 if ((lpDeviceParamSet.fieldValidFlagBit & BLE_LPDEVICE_ADV_SETTING_VALID_BIT) != 0) {
490 pimpl->ConvertAdvertiserSetting(lpDeviceParamSet.advSettings, paramSet.btAdvSettings);
491 }
492
493 if ((lpDeviceParamSet.fieldValidFlagBit & BLE_LPDEVICE_ADVDATA_VALID_BIT) != 0) {
494 paramSet.btAdvData.SetPayload(std::string(lpDeviceParamSet.advData.begin(),
495 lpDeviceParamSet.advData.end()));
496 }
497
498 if ((lpDeviceParamSet.fieldValidFlagBit & BLE_LPDEVICE_RESPDATA_VALID_BIT) != 0) {
499 paramSet.btRespData.SetPayload(std::string(lpDeviceParamSet.respData.begin(),
500 lpDeviceParamSet.respData.end()));
501 }
502
503 if ((lpDeviceParamSet.fieldValidFlagBit & BLE_LPDEVICE_ADV_DEVICEINFO_VALID_BIT) != 0) {
504 pimpl->ConvertActiveDeviceInfo(lpDeviceParamSet.activeDeviceInfos, paramSet.activeDeviceInfos);
505 }
506 paramSet.fieldValidFlagBit = lpDeviceParamSet.fieldValidFlagBit;
507 paramSet.uuid = bluetooth::Uuid::ConvertFromString(lpDeviceParamSet.uuid.ToString());
508 paramSet.advHandle = lpDeviceParamSet.advHandle;
509 paramSet.deliveryMode = lpDeviceParamSet.deliveryMode;
510 paramSet.duration = lpDeviceParamSet.duration;
511 sptr<IBluetoothBleCentralManager> proxy =
512 GetRemoteProxy<IBluetoothBleCentralManager>(BLE_CENTRAL_MANAGER_SERVER);
513 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "failed: no proxy");
514 return proxy->SetLpDeviceParam(paramSet);
515 }
516
RemoveLpDeviceParam(const UUID &uuid)517 int BleCentralManager::RemoveLpDeviceParam(const UUID &uuid)
518 {
519 if (!IS_BLE_ENABLED()) {
520 HILOGE("bluetooth is off.");
521 return BT_ERR_INTERNAL_ERROR;
522 }
523
524 sptr<IBluetoothBleCentralManager> proxy =
525 GetRemoteProxy<IBluetoothBleCentralManager>(BLE_CENTRAL_MANAGER_SERVER);
526 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "failed: no proxy");
527 bluetooth::Uuid btUuid = bluetooth::Uuid::ConvertFromString(uuid.ToString());
528 return proxy->RemoveLpDeviceParam(btUuid);
529 }
530
BleScanResult()531 BleScanResult::BleScanResult()
532 {}
533
~BleScanResult()534 BleScanResult::~BleScanResult()
535 {}
536
GetServiceUuids() const537 std::vector<UUID> BleScanResult::GetServiceUuids() const
538 {
539 return serviceUuids_;
540 }
541
GetManufacturerData() const542 std::map<uint16_t, std::string> BleScanResult::GetManufacturerData() const
543 {
544 return manufacturerSpecificData_;
545 }
546
GetServiceData() const547 std::map<UUID, std::string> BleScanResult::GetServiceData() const
548 {
549 return serviceData_;
550 }
551
GetPeripheralDevice() const552 BluetoothRemoteDevice BleScanResult::GetPeripheralDevice() const
553 {
554 return peripheralDevice_;
555 }
556
GetRssi() const557 int8_t BleScanResult::GetRssi() const
558 {
559 return rssi_;
560 }
561
IsConnectable() const562 bool BleScanResult::IsConnectable() const
563 {
564 return connectable_;
565 }
566
GetAdvertiseFlag() const567 uint8_t BleScanResult::GetAdvertiseFlag() const
568 {
569 return advertiseFlag_;
570 }
571
GetPayload() const572 std::vector<uint8_t> BleScanResult::GetPayload() const
573 {
574 return payload_;
575 }
576
AddManufacturerData(uint16_t manufacturerId, const std::string &data)577 void BleScanResult::AddManufacturerData(uint16_t manufacturerId, const std::string &data)
578 {
579 manufacturerSpecificData_.insert(std::make_pair(manufacturerId, data));
580 }
581
AddServiceData(const UUID &uuid, const std::string &data)582 void BleScanResult::AddServiceData(const UUID &uuid, const std::string &data)
583 {
584 serviceData_.insert(std::make_pair(uuid, data));
585 }
586
AddServiceUuid(const UUID &serviceUuid)587 void BleScanResult::AddServiceUuid(const UUID &serviceUuid)
588 {
589 serviceUuids_.push_back(serviceUuid);
590 }
591
SetPayload(std::string payload)592 void BleScanResult::SetPayload(std::string payload)
593 {
594 payload_.assign(payload.begin(), payload.end());
595 }
596
SetPeripheralDevice(const BluetoothRemoteDevice &device)597 void BleScanResult::SetPeripheralDevice(const BluetoothRemoteDevice &device)
598 {
599 peripheralDevice_ = device;
600 }
601
SetRssi(int8_t rssi)602 void BleScanResult::SetRssi(int8_t rssi)
603 {
604 rssi_ = rssi;
605 }
606
SetConnectable(bool connectable)607 void BleScanResult::SetConnectable(bool connectable)
608 {
609 connectable_ = connectable;
610 }
611
SetAdvertiseFlag(uint8_t flag)612 void BleScanResult::SetAdvertiseFlag(uint8_t flag)
613 {
614 advertiseFlag_ = flag;
615 }
616
SetName(const std::string &name)617 void BleScanResult::SetName(const std::string &name)
618 {
619 name_ = name;
620 }
621
GetName(void)622 std::string BleScanResult::GetName(void)
623 {
624 return name_;
625 }
626
SetEventType(uint16_t eventType)627 void BleScanResult::SetEventType(uint16_t eventType)
628 {
629 eventType_ = eventType;
630 }
631
GetEventType(void) const632 uint16_t BleScanResult::GetEventType(void) const
633 {
634 return eventType_;
635 }
636
BleScanSettings()637 BleScanSettings::BleScanSettings()
638 {}
639
~BleScanSettings()640 BleScanSettings::~BleScanSettings()
641 {}
642
SetReportDelay(long reportDelayMillis)643 void BleScanSettings::SetReportDelay(long reportDelayMillis)
644 {
645 reportDelayMillis_ = reportDelayMillis;
646 }
647
GetReportDelayMillisValue() const648 long BleScanSettings::GetReportDelayMillisValue() const
649 {
650 return reportDelayMillis_;
651 }
652
SetScanMode(int scanMode)653 int BleScanSettings::SetScanMode(int scanMode)
654 {
655 if (scanMode < SCAN_MODE_LOW_POWER || scanMode >= SCAN_MODE_INVALID) {
656 return RET_BAD_PARAM;
657 }
658
659 scanMode_ = scanMode;
660 return RET_NO_ERROR;
661 }
662
GetScanMode() const663 int BleScanSettings::GetScanMode() const
664 {
665 return scanMode_;
666 }
667
SetLegacy(bool legacy)668 void BleScanSettings::SetLegacy(bool legacy)
669 {
670 legacy_ = legacy;
671 }
672
GetLegacy() const673 bool BleScanSettings::GetLegacy() const
674 {
675 return legacy_;
676 }
677
SetPhy(int phy)678 void BleScanSettings::SetPhy(int phy)
679 {
680 phy_ = phy;
681 }
682
GetPhy() const683 int BleScanSettings::GetPhy() const
684 {
685 return phy_;
686 }
687
SetCallbackType(uint8_t callbackType)688 void BleScanSettings::SetCallbackType(uint8_t callbackType)
689 {
690 callbackType_ = callbackType;
691 }
692
GetCallbackType() const693 uint8_t BleScanSettings::GetCallbackType() const
694 {
695 return callbackType_;
696 }
697
SetMatchTrackAdvType(uint8_t matchTrackAdvType)698 void BleScanSettings::SetMatchTrackAdvType(uint8_t matchTrackAdvType)
699 {
700 matchTrackAdvType_ = matchTrackAdvType;
701 }
702
GetMatchTrackAdvType() const703 uint8_t BleScanSettings::GetMatchTrackAdvType() const
704 {
705 return matchTrackAdvType_;
706 }
707
BleScanFilter()708 BleScanFilter::BleScanFilter()
709 {}
710
~BleScanFilter()711 BleScanFilter::~BleScanFilter()
712 {}
713
SetDeviceId(std::string deviceId)714 void BleScanFilter::SetDeviceId(std::string deviceId)
715 {
716 deviceId_ = deviceId;
717 }
718
GetDeviceId() const719 std::string BleScanFilter::GetDeviceId() const
720 {
721 return deviceId_;
722 }
723
SetName(std::string name)724 void BleScanFilter::SetName(std::string name)
725 {
726 name_ = name;
727 }
728
GetName() const729 std::string BleScanFilter::GetName() const
730 {
731 return name_;
732 }
733
SetServiceUuid(const UUID &uuid)734 void BleScanFilter::SetServiceUuid(const UUID &uuid)
735 {
736 serviceUuid_ = uuid;
737 hasServiceUuid_ = true;
738 }
739
HasServiceUuid()740 bool BleScanFilter::HasServiceUuid()
741 {
742 return hasServiceUuid_;
743 }
744
GetServiceUuid() const745 UUID BleScanFilter::GetServiceUuid() const
746 {
747 return serviceUuid_;
748 }
749
SetServiceUuidMask(const UUID &serviceUuidMask)750 void BleScanFilter::SetServiceUuidMask(const UUID &serviceUuidMask)
751 {
752 serviceUuidMask_ = serviceUuidMask;
753 hasServiceUuidMask_ = true;
754 }
755
HasServiceUuidMask()756 bool BleScanFilter::HasServiceUuidMask()
757 {
758 return hasServiceUuidMask_;
759 }
760
GetServiceUuidMask() const761 UUID BleScanFilter::GetServiceUuidMask() const
762 {
763 return serviceUuidMask_;
764 }
765
SetServiceSolicitationUuid(const UUID &serviceSolicitationUuid)766 void BleScanFilter::SetServiceSolicitationUuid(const UUID &serviceSolicitationUuid)
767 {
768 serviceSolicitationUuid_ = serviceSolicitationUuid;
769 hasSolicitationUuid_ = true;
770 }
771
HasSolicitationUuid()772 bool BleScanFilter::HasSolicitationUuid()
773 {
774 return hasSolicitationUuid_;
775 }
776
GetServiceSolicitationUuid() const777 UUID BleScanFilter::GetServiceSolicitationUuid() const
778 {
779 return serviceSolicitationUuid_;
780 }
781
SetServiceSolicitationUuidMask(const UUID &serviceSolicitationUuidMask)782 void BleScanFilter::SetServiceSolicitationUuidMask(const UUID &serviceSolicitationUuidMask)
783 {
784 serviceSolicitationUuidMask_ = serviceSolicitationUuidMask;
785 hasSolicitationUuidMask_ = true;
786 }
787
HasSolicitationUuidMask()788 bool BleScanFilter::HasSolicitationUuidMask()
789 {
790 return hasSolicitationUuidMask_;
791 }
792
GetServiceSolicitationUuidMask() const793 UUID BleScanFilter::GetServiceSolicitationUuidMask() const
794 {
795 return serviceSolicitationUuidMask_;
796 }
797
SetServiceData(std::vector<uint8_t> serviceData)798 void BleScanFilter::SetServiceData(std::vector<uint8_t> serviceData)
799
800 {
801 serviceData_ = serviceData;
802 }
803
GetServiceData() const804 std::vector<uint8_t> BleScanFilter::GetServiceData() const
805 {
806 return serviceData_;
807 }
808
SetServiceDataMask(std::vector<uint8_t> serviceDataMask)809 void BleScanFilter::SetServiceDataMask(std::vector<uint8_t> serviceDataMask)
810 {
811 serviceDataMask_ = serviceDataMask;
812 }
813
GetServiceDataMask() const814 std::vector<uint8_t> BleScanFilter::GetServiceDataMask() const
815 {
816 return serviceDataMask_;
817 }
818
SetManufacturerId(uint16_t manufacturerId)819 void BleScanFilter::SetManufacturerId(uint16_t manufacturerId)
820 {
821 manufacturerId_ = manufacturerId;
822 }
823
GetManufacturerId() const824 uint16_t BleScanFilter::GetManufacturerId() const
825 {
826 return manufacturerId_;
827 }
828
SetManufactureData(std::vector<uint8_t> manufactureData)829 void BleScanFilter::SetManufactureData(std::vector<uint8_t> manufactureData)
830 {
831 manufactureData_ = manufactureData;
832 }
833
GetManufactureData() const834 std::vector<uint8_t> BleScanFilter::GetManufactureData() const
835 {
836 return manufactureData_;
837 }
838
SetManufactureDataMask(std::vector<uint8_t> manufactureDataMask)839 void BleScanFilter::SetManufactureDataMask(std::vector<uint8_t> manufactureDataMask)
840 {
841 manufactureDataMask_ = manufactureDataMask;
842 }
843
GetManufactureDataMask() const844 std::vector<uint8_t> BleScanFilter::GetManufactureDataMask() const
845 {
846 return manufactureDataMask_;
847 }
848
SetAdvIndReportFlag(bool advIndReprot)849 void BleScanFilter::SetAdvIndReportFlag(bool advIndReprot)
850 {
851 advIndReprot_ = advIndReprot;
852 }
853
GetAdvIndReportFlag() const854 bool BleScanFilter::GetAdvIndReportFlag() const
855 {
856 return advIndReprot_;
857 }
858
859 } // namespace Bluetooth
860 } // namespace OHOS
861