• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_errorcode.h"
19 #include "bluetooth_host.h"
20 #include "bluetooth_log.h"
21 #include "bluetooth_observer_list.h"
22 #include "bluetooth_utils.h"
23 #include "i_bluetooth_ble_central_manager.h"
24 #include "iservice_registry.h"
25 #include "system_ability_definition.h"
26 
27 namespace OHOS {
28 namespace Bluetooth {
29 struct BleCentralManager::impl {
30     impl();
31     ~impl();
32 
33     void BindServer();
34     bool MatchesScanFilters(const BluetoothBleScanResult &result);
35     bool MatchesScanFilter(const BluetoothBleScanFilter &filter, const BluetoothBleScanResult &result);
36     bool MatchesAddrAndName(const BluetoothBleScanFilter &filter, const BluetoothBleScanResult &result);
37     bool MatchesServiceUuids(const BluetoothBleScanFilter &filter, const BluetoothBleScanResult &result);
38     bool MatchesUuid(bluetooth::Uuid filterUuid, bluetooth::Uuid uuid, bluetooth::Uuid uuidMask);
39     bool MatchesManufacturerDatas(const BluetoothBleScanFilter &filter, const BluetoothBleScanResult &result);
40     bool MatchesServiceDatas(const BluetoothBleScanFilter &filter, const BluetoothBleScanResult &result);
41     std::string ParseServiceData(bluetooth::Uuid uuid, std::string data);
42     bool MatchesData(std::vector<uint8_t> fData, std::string rData, std::vector<uint8_t> dataMask);
43 
44     class BluetoothBleCentralManagerCallbackImp : public BluetoothBleCentralManagerCallBackStub {
45     public:
BluetoothBleCentralManagerCallbackImp(BleCentralManager::impl & bleCentralManger)46         explicit BluetoothBleCentralManagerCallbackImp(BleCentralManager::impl &bleCentralManger)
47             : bleCentralManger_(bleCentralManger){};
48         ~BluetoothBleCentralManagerCallbackImp() override = default;
OnScanCallback(const BluetoothBleScanResult & result)49         void OnScanCallback(const BluetoothBleScanResult &result) override
50         {
51             {
52                 std::lock_guard<std::mutex> lock(bleCentralManger_.blesCanFiltersMutex_);
53                 if (!bleCentralManger_.bleScanFilters_.empty() && bleCentralManger_.IsNeedFilterMatches_ &&
54                     !bleCentralManger_.MatchesScanFilters(result)) {
55                     return;
56                 }
57             }
58 
59             bleCentralManger_.callbacks_.ForEach([&result](std::shared_ptr<BleCentralManagerCallback> observer) {
60                 BluetoothBleScanResult tempResult(result);
61                 BleScanResult scanResult;
62                 for (auto &manufacturerData : tempResult.GetManufacturerData()) {
63                     scanResult.AddManufacturerData(manufacturerData.first, manufacturerData.second);
64                 }
65 
66                 for (auto &serviceUuidData : tempResult.GetServiceUuids()) {
67                     UUID uuid = UUID::ConvertFrom128Bits(serviceUuidData.ConvertTo128Bits());
68                     scanResult.AddServiceUuid(uuid);
69                 }
70 
71                 for (auto &serviceData : tempResult.GetServiceData()) {
72                     UUID uuid = UUID::ConvertFrom128Bits(serviceData.first.ConvertTo128Bits());
73                     scanResult.AddServiceData(uuid, serviceData.second);
74                 }
75 
76                 scanResult.SetAdvertiseFlag(tempResult.GetAdvertiseFlag());
77                 scanResult.SetRssi(tempResult.GetRssi());
78                 scanResult.SetConnectable(tempResult.IsConnectable());
79                 BluetoothRemoteDevice device(tempResult.GetPeripheralDevice().GetAddress(), BT_TRANSPORT_BLE);
80                 scanResult.SetPeripheralDevice(device);
81                 scanResult.SetPayload(tempResult.GetPayload());
82                 scanResult.SetName(tempResult.GetName());
83 
84                 observer->OnScanCallback(scanResult);
85             });
86         }
OnBleBatchScanResultsEvent(std::vector<BluetoothBleScanResult> & results)87         void OnBleBatchScanResultsEvent(std::vector<BluetoothBleScanResult> &results) override
88         {
89             HILOGI("enter");
90             bleCentralManger_.callbacks_.ForEach([&results](std::shared_ptr<BleCentralManagerCallback> observer) {
91                 std::vector<BleScanResult> scanResults;
92                 for (auto &result : results) {
93                     BleScanResult scanResult;
94                     for (auto &manufacturerData : result.GetManufacturerData()) {
95                         scanResult.AddManufacturerData(manufacturerData.first, manufacturerData.second);
96                     }
97 
98                     for (auto &serviceData : result.GetServiceData()) {
99                         UUID uuid = UUID::ConvertFrom128Bits(serviceData.first.ConvertTo128Bits());
100                         scanResult.AddServiceData(uuid, serviceData.second);
101                     }
102 
103                     for (auto &serviceUuidData : result.GetServiceUuids()) {
104                         UUID uuid = UUID::ConvertFrom128Bits(serviceUuidData.ConvertTo128Bits());
105                         scanResult.AddServiceUuid(uuid);
106                     }
107 
108                     scanResult.SetAdvertiseFlag(result.GetAdvertiseFlag());
109                     scanResult.SetConnectable(result.IsConnectable());
110                     scanResult.SetRssi(result.GetRssi());
111                     BluetoothRemoteDevice device(result.GetPeripheralDevice().GetAddress(), BT_TRANSPORT_BLE);
112                     scanResult.SetPeripheralDevice(device);
113                     scanResult.SetPayload(result.GetPayload());
114 
115                     scanResults.push_back(scanResult);
116                 }
117 
118                 observer->OnBleBatchScanResultsEvent(scanResults);
119             });
120         }
121 
OnStartOrStopScanEvent(int resultCode,bool isStartScan)122         void OnStartOrStopScanEvent(int resultCode, bool isStartScan) override
123         {
124             HILOGI("resultCode: %{public}d, isStartScan: %{public}d", resultCode, isStartScan);
125             bleCentralManger_.callbacks_.ForEach(
126                 [resultCode, isStartScan](std::shared_ptr<BleCentralManagerCallback> observer) {
127                     observer->OnStartOrStopScanEvent(resultCode, isStartScan);
128             });
129         }
130 
131     private:
132         BleCentralManager::impl &bleCentralManger_;
133         BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothBleCentralManagerCallbackImp);
134     };
135     sptr<BluetoothBleCentralManagerCallbackImp> callbackImp_ = nullptr;
136 
137     sptr<IBluetoothBleCentralManager> proxy_ = nullptr;
138     BluetoothObserverList<BleCentralManagerCallback> callbacks_;
139 
140     std::vector<BluetoothBleScanFilter> bleScanFilters_;
141     bool IsNeedFilterMatches_ = true;
142     std::mutex blesCanFiltersMutex_;
143 };
144 
impl()145 BleCentralManager::impl::impl()
146 {
147     sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
148     if (!samgr) {
149         HILOGE("samgr is null");
150         return;
151     }
152 
153     sptr<IRemoteObject> hostRemote = samgr->GetSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
154     if (!hostRemote) {
155         HILOGE("hostRemote is null");
156         return;
157     }
158     sptr<IBluetoothHost> hostProxy = iface_cast<IBluetoothHost>(hostRemote);
159     if (!hostProxy) {
160         HILOGE("hostProxy is null");
161         return;
162     }
163     sptr<IRemoteObject> remote = hostProxy->GetBleRemote(BLE_CENTRAL_MANAGER_SERVER);
164     if (!remote) {
165         HILOGE("remote is null");
166         return;
167     }
168     proxy_ = iface_cast<IBluetoothBleCentralManager>(remote);
169     if (!proxy_) {
170         HILOGE("proxy_ is null");
171         return;
172     }
173     callbackImp_ = new BluetoothBleCentralManagerCallbackImp(*this);
174     proxy_->RegisterBleCentralManagerCallback(callbackImp_);
175 }
176 
MatchesScanFilters(const BluetoothBleScanResult & result)177 bool BleCentralManager::impl::MatchesScanFilters(const BluetoothBleScanResult &result)
178 {
179     for (const auto &filter : bleScanFilters_) {
180         if (!MatchesScanFilter(filter, result)) {
181             continue;
182         }
183         return true;
184     }
185     return false;
186 }
187 
MatchesScanFilter(const BluetoothBleScanFilter & filter,const BluetoothBleScanResult & result)188 bool BleCentralManager::impl::MatchesScanFilter(const BluetoothBleScanFilter &filter,
189     const BluetoothBleScanResult &result)
190 {
191     if (!MatchesAddrAndName(filter, result)) {
192         return false;
193     }
194 
195     if (filter.HasServiceUuid() && !MatchesServiceUuids(filter, result)) {
196         return false;
197     }
198 
199     if (!MatchesManufacturerDatas(filter, result)) {
200         return false;
201     }
202 
203     if (!MatchesServiceDatas(filter, result)) {
204         return false;
205     }
206 
207     return true;
208 }
209 
MatchesAddrAndName(const BluetoothBleScanFilter & filter,const BluetoothBleScanResult & result)210 bool BleCentralManager::impl::MatchesAddrAndName(const BluetoothBleScanFilter &filter,
211     const BluetoothBleScanResult &result)
212 {
213     std::string address = result.GetPeripheralDevice().GetAddress();
214     if (address.empty()) {
215         return false;
216     }
217 
218     std::string deviceId = filter.GetDeviceId();
219     if (!deviceId.empty() && deviceId != address) {
220         return false;
221     }
222 
223     std::string name = filter.GetName();
224     if (!name.empty()) {
225         std::string rName = result.GetName();
226         if (rName.empty() || rName != name) {
227             return false;
228         }
229     }
230 
231     return true;
232 }
233 
MatchesServiceUuids(const BluetoothBleScanFilter & filter,const BluetoothBleScanResult & result)234 bool BleCentralManager::impl::MatchesServiceUuids(const BluetoothBleScanFilter &filter,
235     const BluetoothBleScanResult &result)
236 {
237     std::vector<bluetooth::Uuid> rUuids = result.GetServiceUuids();
238     if (rUuids.empty()) {
239         return false;
240     }
241 
242     bluetooth::Uuid filterUuid = filter.GetServiceUuid();
243     bool hasUuidMask = filter.HasServiceUuidMask();
244 
245     for (auto &uuid : rUuids) {
246         if (!hasUuidMask) {
247             if (filterUuid.operator==(uuid)) {
248                 return true;
249             }
250         } else {
251             bluetooth::Uuid uuidMask = filter.GetServiceUuidMask();
252             if (MatchesUuid(filterUuid, uuid, uuidMask)) {
253                 return true;
254             }
255         }
256     }
257     return false;
258 }
259 
MatchesUuid(bluetooth::Uuid filterUuid,bluetooth::Uuid uuid,bluetooth::Uuid uuidMask)260 bool BleCentralManager::impl::MatchesUuid(bluetooth::Uuid filterUuid, bluetooth::Uuid uuid, bluetooth::Uuid uuidMask)
261 {
262     HILOGI("enter");
263     uint8_t uuid128[bluetooth::Uuid::UUID128_BYTES_TYPE];
264     uint8_t uuidMask128[bluetooth::Uuid::UUID128_BYTES_TYPE];
265     uint8_t resultUuid128[bluetooth::Uuid::UUID128_BYTES_TYPE];
266     if (!filterUuid.ConvertToBytesLE(uuid128)) {
267         HILOGE("Convert filter uuid faild.");
268         return false;
269     }
270     if (!uuidMask.ConvertToBytesLE(uuidMask128)) {
271         HILOGE("Convert uuid mask faild.");
272         return false;
273     }
274     if (!uuid.ConvertToBytesLE(resultUuid128)) {
275         HILOGE("Convert result uuid faild.");
276         return false;
277     }
278     size_t maskLength = sizeof(uuidMask128);
279     if (maskLength <= 0) {
280         return false;
281     }
282 
283     for (size_t i = 0; i < maskLength; i++) {
284         if ((uuid128[i] & uuidMask128[i]) != (resultUuid128[i] & uuidMask128[i])) {
285             return false;
286         }
287     }
288 
289     return true;
290 }
291 
MatchesManufacturerDatas(const BluetoothBleScanFilter & filter,const BluetoothBleScanResult & result)292 bool BleCentralManager::impl::MatchesManufacturerDatas(const BluetoothBleScanFilter &filter,
293     const BluetoothBleScanResult &result)
294 {
295     uint16_t manufacturerId = filter.GetManufacturerId();
296     std::vector<uint8_t> data = filter.GetManufactureData();
297     if (data.size() == 0) {
298         return true;
299     }
300 
301     std::vector<uint8_t> dataMask = filter.GetManufactureDataMask();
302 
303     for (auto &manufacturerData : result.GetManufacturerData()) {
304         if (manufacturerId != manufacturerData.first) {
305             continue;
306         }
307 
308         if (MatchesData(data, manufacturerData.second, dataMask)) {
309             return true;
310         }
311     }
312     return false;
313 }
314 
MatchesServiceDatas(const BluetoothBleScanFilter & filter,const BluetoothBleScanResult & result)315 bool BleCentralManager::impl::MatchesServiceDatas(const BluetoothBleScanFilter &filter,
316     const BluetoothBleScanResult &result)
317 {
318     std::vector<uint8_t> data = filter.GetServiceData();
319     if (data.size() == 0) {
320         return true;
321     }
322 
323     std::vector<uint8_t> dataMask = filter.GetServiceDataMask();
324 
325     for (auto &serviceData : result.GetServiceData()) {
326         std::string rSData = ParseServiceData(serviceData.first, serviceData.second);
327         if (MatchesData(data, rSData, dataMask)) {
328             return true;
329         }
330     }
331     return false;
332 }
333 
ParseServiceData(bluetooth::Uuid uuid,std::string data)334 std::string BleCentralManager::impl::ParseServiceData(bluetooth::Uuid uuid, std::string data)
335 {
336     std::string tmpServcieData;
337     int uuidType = uuid.GetUuidType();
338     switch (uuidType) {
339         case bluetooth::Uuid::UUID16_BYTES_TYPE: {
340             uint16_t uuid16 = uuid.ConvertTo16Bits();
341             tmpServcieData = std::string(reinterpret_cast<char *>(&uuid16), BLE_UUID_LEN_16);
342             break;
343         }
344         case bluetooth::Uuid::UUID32_BYTES_TYPE: {
345             uint32_t uuid32 = uuid.ConvertTo32Bits();
346             tmpServcieData = std::string(reinterpret_cast<char *>(&uuid32), BLE_UUID_LEN_32);
347             break;
348         }
349         case bluetooth::Uuid::UUID128_BYTES_TYPE: {
350             uint8_t uuid128[bluetooth::Uuid::UUID128_BYTES_TYPE];
351             if (!uuid.ConvertToBytesLE(uuid128)) {
352                 HILOGE("Convert filter uuid faild.");
353             }
354             tmpServcieData = std::string(reinterpret_cast<char *>(&uuid128), BLE_UUID_LEN_128);
355             break;
356         }
357         default:
358             break;
359     }
360     return tmpServcieData + data;
361 }
362 
MatchesData(std::vector<uint8_t> fData,std::string rData,std::vector<uint8_t> dataMask)363 bool BleCentralManager::impl::MatchesData(std::vector<uint8_t> fData, std::string rData, std::vector<uint8_t> dataMask)
364 {
365     if (rData.empty()) {
366         return false;
367     }
368 
369     size_t length = fData.size();
370     std::vector<uint8_t> vec(rData.begin(), rData.end());
371     if (vec.size() < length) {
372         return false;
373     }
374 
375     if (dataMask.empty() || dataMask.size() != length) {
376         for (size_t i = 0; i < length; i++) {
377             if (fData[i] != vec[i]) {
378                 return false;
379             }
380         }
381         return true;
382     }
383     for (size_t i = 0; i < length; i++) {
384         if ((fData[i] & dataMask[i]) != (vec[i] & dataMask[i])) {
385             return false;
386         }
387     }
388     return true;
389 }
390 
~impl()391 BleCentralManager::impl::~impl()
392 {
393     proxy_->DeregisterBleCentralManagerCallback(callbackImp_);
394 }
395 
BleCentralManager(BleCentralManagerCallback & callback)396 BleCentralManager::BleCentralManager(BleCentralManagerCallback &callback) : callback_(&callback), pimpl(nullptr)
397 {
398     if (pimpl == nullptr) {
399         pimpl = std::make_unique<impl>();
400         if (pimpl == nullptr) {
401             HILOGE("failed, no pimpl");
402         }
403     }
404 
405     HILOGI("successful");
406     std::shared_ptr<BleCentralManagerCallback> pointer(&callback, [](BleCentralManagerCallback *) {});
407     bool ret = pimpl->callbacks_.Register(pointer);
408     if (ret)
409         return;
410     callback_ = &callback;
411 }
412 
~BleCentralManager()413 BleCentralManager::~BleCentralManager()
414 {
415     callback_ = nullptr;
416 }
417 
StartScan()418 int BleCentralManager::StartScan()
419 {
420     CHECK_PROXY_RETURN(pimpl->proxy_);
421     if (!BluetoothHost::GetDefaultHost().IsBleEnabled()) {
422         HILOGE("BLE is not enabled");
423         return BT_ERR_INVALID_STATE;
424     }
425 
426     HILOGI("StartScan without param.");
427     return pimpl->proxy_->StartScan();
428 }
429 
StartScan(const BleScanSettings & settings)430 int BleCentralManager::StartScan(const BleScanSettings &settings)
431 {
432     CHECK_PROXY_RETURN(pimpl->proxy_);
433     if (!BluetoothHost::GetDefaultHost().IsBleEnabled()) {
434         HILOGE("BLE is not enabled");
435         return BT_ERR_INVALID_STATE;
436     }
437 
438     HILOGI("StartScan with params.");
439     BluetoothBleScanSettings setting;
440     // not use report delay scan. settings.GetReportDelayMillisValue()
441     setting.SetReportDelay(0);
442     setting.SetScanMode(settings.GetScanMode());
443     setting.SetLegacy(settings.GetLegacy());
444     setting.SetPhy(settings.GetPhy());
445     return pimpl->proxy_->StartScan(setting);
446 }
447 
StopScan()448 int BleCentralManager::StopScan()
449 {
450     CHECK_PROXY_RETURN(pimpl->proxy_);
451     if (!BluetoothHost::GetDefaultHost().IsBleEnabled()) {
452         HILOGE("BLE is not enabled");
453         return BT_ERR_INVALID_STATE;
454     }
455 
456     HILOGI("clientId: %{public}d", clientId_);
457     std::lock_guard<std::mutex> lock(pimpl->blesCanFiltersMutex_);
458 
459     int ret = pimpl->proxy_->StopScan();
460     if (clientId_ != 0) {
461         pimpl->proxy_->RemoveScanFilter(clientId_);
462         clientId_ = 0;
463     }
464     pimpl->bleScanFilters_.clear();
465     pimpl->IsNeedFilterMatches_ = true;
466     return ret;
467 }
468 
ConfigScanFilter(const std::vector<BleScanFilter> & filters)469 int BleCentralManager::ConfigScanFilter(const std::vector<BleScanFilter> &filters)
470 {
471     CHECK_PROXY_RETURN(pimpl->proxy_);
472     if (!BluetoothHost::GetDefaultHost().IsBleEnabled()) {
473         HILOGE("BLE is not enabled");
474         return BT_ERR_INVALID_STATE;
475     }
476 
477     std::lock_guard<std::mutex> lock(pimpl->blesCanFiltersMutex_);
478     std::vector<BluetoothBleScanFilter> bluetoothBleScanFilters;
479     for (auto filter : filters) {
480         BluetoothBleScanFilter scanFilter;
481         scanFilter.SetDeviceId(filter.GetDeviceId());
482         scanFilter.SetName(filter.GetName());
483         if (filter.HasServiceUuid()) {
484             scanFilter.SetServiceUuid(bluetooth::Uuid::ConvertFromString(
485                 filter.GetServiceUuid().ToString()));
486         }
487         if (filter.HasServiceUuidMask()) {
488             scanFilter.SetServiceUuidMask(bluetooth::Uuid::ConvertFromString(
489                 filter.GetServiceUuidMask().ToString()));
490         }
491         if (filter.HasSolicitationUuid()) {
492             scanFilter.SetServiceSolicitationUuid(bluetooth::Uuid::ConvertFromString(
493                 filter.GetServiceSolicitationUuid().ToString()));
494         }
495         if (filter.HasSolicitationUuidMask()) {
496             scanFilter.SetServiceSolicitationUuidMask(bluetooth::Uuid::ConvertFromString(
497                 filter.GetServiceSolicitationUuidMask().ToString()));
498         }
499         scanFilter.SetServiceData(filter.GetServiceData());
500         scanFilter.SetServiceDataMask(filter.GetServiceDataMask());
501         scanFilter.SetManufacturerId(filter.GetManufacturerId());
502         scanFilter.SetManufactureData(filter.GetManufactureData());
503         scanFilter.SetManufactureDataMask(filter.GetManufactureDataMask());
504         bluetoothBleScanFilters.push_back(scanFilter);
505         pimpl->bleScanFilters_.push_back(scanFilter);
506     }
507     clientId_ = pimpl->proxy_->ConfigScanFilter(clientId_, bluetoothBleScanFilters);
508     HILOGI("clientId: %{public}d", clientId_);
509 
510     if (filters.empty()) {
511         HILOGI("filters is empty can not config");
512         pimpl->IsNeedFilterMatches_ = false;
513     }
514     return BT_SUCCESS;
515 }
516 
BleScanResult()517 BleScanResult::BleScanResult()
518 {}
519 
~BleScanResult()520 BleScanResult::~BleScanResult()
521 {}
522 
GetServiceUuids() const523 std::vector<UUID> BleScanResult::GetServiceUuids() const
524 {
525     return serviceUuids_;
526 }
527 
GetManufacturerData() const528 std::map<uint16_t, std::string> BleScanResult::GetManufacturerData() const
529 {
530     return manufacturerSpecificData_;
531 }
532 
GetServiceData() const533 std::map<UUID, std::string> BleScanResult::GetServiceData() const
534 {
535     return serviceData_;
536 }
537 
GetPeripheralDevice() const538 BluetoothRemoteDevice BleScanResult::GetPeripheralDevice() const
539 {
540     return peripheralDevice_;
541 }
542 
GetRssi() const543 int8_t BleScanResult::GetRssi() const
544 {
545     return rssi_;
546 }
547 
IsConnectable() const548 bool BleScanResult::IsConnectable() const
549 {
550     return connectable_;
551 }
552 
GetAdvertiseFlag() const553 uint8_t BleScanResult::GetAdvertiseFlag() const
554 {
555     return advertiseFlag_;
556 }
557 
GetPayload() const558 std::vector<uint8_t> BleScanResult::GetPayload() const
559 {
560     return payload_;
561 }
562 
AddManufacturerData(uint16_t manufacturerId,const std::string & data)563 void BleScanResult::AddManufacturerData(uint16_t manufacturerId, const std::string &data)
564 {
565     manufacturerSpecificData_.insert(std::make_pair(manufacturerId, data));
566 }
567 
AddServiceData(const UUID & uuid,const std::string & data)568 void BleScanResult::AddServiceData(const UUID &uuid, const std::string &data)
569 {
570     serviceData_.insert(std::make_pair(uuid, data));
571 }
572 
AddServiceUuid(const UUID & serviceUuid)573 void BleScanResult::AddServiceUuid(const UUID &serviceUuid)
574 {
575     serviceUuids_.push_back(serviceUuid);
576 }
577 
SetPayload(std::string payload)578 void BleScanResult::SetPayload(std::string payload)
579 {
580     payload_.assign(payload.begin(), payload.end());
581 }
582 
SetPeripheralDevice(const BluetoothRemoteDevice & device)583 void BleScanResult::SetPeripheralDevice(const BluetoothRemoteDevice &device)
584 {
585     peripheralDevice_ = device;
586 }
587 
SetRssi(int8_t rssi)588 void BleScanResult::SetRssi(int8_t rssi)
589 {
590     rssi_ = rssi;
591 }
592 
SetConnectable(bool connectable)593 void BleScanResult::SetConnectable(bool connectable)
594 {
595     connectable_ = connectable;
596 }
597 
SetAdvertiseFlag(uint8_t flag)598 void BleScanResult::SetAdvertiseFlag(uint8_t flag)
599 {
600     advertiseFlag_ = flag;
601 }
602 
SetName(const std::string & name)603 void BleScanResult::SetName(const std::string &name)
604 {
605     name_ = name;
606 }
GetName(void)607 std::string BleScanResult::GetName(void)
608 {
609     return name_;
610 }
611 
BleScanSettings()612 BleScanSettings::BleScanSettings()
613 {}
614 
~BleScanSettings()615 BleScanSettings::~BleScanSettings()
616 {}
617 
SetReportDelay(long reportDelayMillis)618 void BleScanSettings::SetReportDelay(long reportDelayMillis)
619 {
620     reportDelayMillis_ = reportDelayMillis;
621 }
622 
GetReportDelayMillisValue() const623 long BleScanSettings::GetReportDelayMillisValue() const
624 {
625     return reportDelayMillis_;
626 }
627 
SetScanMode(int scanMode)628 int BleScanSettings::SetScanMode(int scanMode)
629 {
630     if (scanMode < SCAN_MODE_LOW_POWER || scanMode >= SCAN_MODE_INVALID) {
631         return RET_BAD_PARAM;
632     }
633 
634     scanMode_ = scanMode;
635     return RET_NO_ERROR;
636 }
637 
GetScanMode() const638 int BleScanSettings::GetScanMode() const
639 {
640     return scanMode_;
641 }
642 
SetLegacy(bool legacy)643 void BleScanSettings::SetLegacy(bool legacy)
644 {
645     legacy_ = legacy;
646 }
647 
GetLegacy() const648 bool BleScanSettings::GetLegacy() const
649 {
650     return legacy_;
651 }
652 
SetPhy(int phy)653 void BleScanSettings::SetPhy(int phy)
654 {
655     phy_ = phy;
656 }
657 
GetPhy() const658 int BleScanSettings::GetPhy() const
659 {
660     return phy_;
661 }
662 
BleScanFilter()663 BleScanFilter::BleScanFilter()
664 {}
665 
~BleScanFilter()666 BleScanFilter::~BleScanFilter()
667 {}
668 
SetDeviceId(std::string deviceId)669 void BleScanFilter::SetDeviceId(std::string deviceId)
670 {
671     deviceId_ = deviceId;
672 }
673 
GetDeviceId() const674 std::string BleScanFilter::GetDeviceId() const
675 {
676     return deviceId_;
677 }
678 
SetName(std::string name)679 void BleScanFilter::SetName(std::string name)
680 {
681     name_ = name;
682 }
683 
GetName() const684 std::string BleScanFilter::GetName() const
685 {
686     return name_;
687 }
688 
SetServiceUuid(const UUID & uuid)689 void BleScanFilter::SetServiceUuid(const UUID &uuid)
690 {
691     serviceUuid_ = uuid;
692     hasServiceUuid_ = true;
693 }
694 
HasServiceUuid()695 bool BleScanFilter::HasServiceUuid()
696 {
697     return hasServiceUuid_;
698 }
699 
GetServiceUuid() const700 UUID BleScanFilter::GetServiceUuid() const
701 {
702     return serviceUuid_;
703 }
704 
SetServiceUuidMask(const UUID & serviceUuidMask)705 void BleScanFilter::SetServiceUuidMask(const UUID &serviceUuidMask)
706 {
707     serviceUuidMask_ = serviceUuidMask;
708     hasServiceUuidMask_ = true;
709 }
710 
HasServiceUuidMask()711 bool BleScanFilter::HasServiceUuidMask()
712 {
713     return hasServiceUuidMask_;
714 }
715 
GetServiceUuidMask() const716 UUID BleScanFilter::GetServiceUuidMask() const
717 {
718     return serviceUuidMask_;
719 }
720 
SetServiceSolicitationUuid(const UUID & serviceSolicitationUuid)721 void BleScanFilter::SetServiceSolicitationUuid(const UUID &serviceSolicitationUuid)
722 {
723     serviceSolicitationUuid_ = serviceSolicitationUuid;
724     hasSolicitationUuid_ = true;
725 }
726 
HasSolicitationUuid()727 bool BleScanFilter::HasSolicitationUuid()
728 {
729     return hasSolicitationUuid_;
730 }
731 
GetServiceSolicitationUuid() const732 UUID BleScanFilter::GetServiceSolicitationUuid() const
733 {
734     return serviceSolicitationUuid_;
735 }
736 
SetServiceSolicitationUuidMask(const UUID & serviceSolicitationUuidMask)737 void BleScanFilter::SetServiceSolicitationUuidMask(const UUID &serviceSolicitationUuidMask)
738 {
739     serviceSolicitationUuidMask_ = serviceSolicitationUuidMask;
740     hasSolicitationUuidMask_ = true;
741 }
742 
HasSolicitationUuidMask()743 bool BleScanFilter::HasSolicitationUuidMask()
744 {
745     return hasSolicitationUuidMask_;
746 }
747 
GetServiceSolicitationUuidMask() const748 UUID BleScanFilter::GetServiceSolicitationUuidMask() const
749 {
750     return serviceSolicitationUuidMask_;
751 }
752 
SetServiceData(std::vector<uint8_t> serviceData)753 void BleScanFilter::SetServiceData(std::vector<uint8_t> serviceData)
754 
755 {
756     serviceData_ = serviceData;
757 }
758 
GetServiceData() const759 std::vector<uint8_t> BleScanFilter::GetServiceData() const
760 {
761     return serviceData_;
762 }
763 
SetServiceDataMask(std::vector<uint8_t> serviceDataMask)764 void BleScanFilter::SetServiceDataMask(std::vector<uint8_t> serviceDataMask)
765 {
766     serviceDataMask_ = serviceDataMask;
767 }
768 
GetServiceDataMask() const769 std::vector<uint8_t> BleScanFilter::GetServiceDataMask() const
770 {
771     return serviceDataMask_;
772 }
773 
SetManufacturerId(uint16_t manufacturerId)774 void BleScanFilter::SetManufacturerId(uint16_t manufacturerId)
775 {
776     manufacturerId_ = manufacturerId;
777 }
778 
GetManufacturerId() const779 uint16_t BleScanFilter::GetManufacturerId() const
780 {
781     return manufacturerId_;
782 }
783 
SetManufactureData(std::vector<uint8_t> manufactureData)784 void BleScanFilter::SetManufactureData(std::vector<uint8_t> manufactureData)
785 {
786     manufactureData_ = manufactureData;
787 }
788 
GetManufactureData() const789 std::vector<uint8_t> BleScanFilter::GetManufactureData() const
790 {
791     return manufactureData_;
792 }
793 
SetManufactureDataMask(std::vector<uint8_t> manufactureDataMask)794 void BleScanFilter::SetManufactureDataMask(std::vector<uint8_t> manufactureDataMask)
795 {
796     manufactureDataMask_ = manufactureDataMask;
797 }
798 
GetManufactureDataMask() const799 std::vector<uint8_t> BleScanFilter::GetManufactureDataMask() const
800 {
801     return manufactureDataMask_;
802 }
803 }  // namespace Bluetooth
804 }  // namespace OHOS
805