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