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 16 /** 17 * @addtogroup Bluetooth 18 * @{ 19 * 20 * @brief Defines central manager, including scan filter and settings, and common functions. 21 * 22 * @since 6 23 */ 24 25 /** 26 * @file bluetooth_ble_central_manager.h 27 * 28 * @brief Central manager common functions. 29 * 30 * @since 6 31 */ 32 33 #ifndef BLUETOOTH_BLE_CENTRAL_MANAGER_H 34 #define BLUETOOTH_BLE_CENTRAL_MANAGER_H 35 36 #include "bluetooth_def.h" 37 #include "bluetooth_types.h" 38 #include "bluetooth_remote_device.h" 39 #include "bluetooth_ble_advertiser.h" 40 41 namespace OHOS { 42 namespace Bluetooth { 43 /** 44 * @brief Represents scan result. 45 * 46 * @since 6 47 */ 48 class BLUETOOTH_API BleScanResult { 49 public: 50 /** 51 * @brief A constructor used to create a <b>BleScanResult</b> instance. 52 * 53 * @since 6 54 */ 55 BleScanResult(); 56 57 /** 58 * @brief A destructor used to delete the <b>BleScanResult</b> instance. 59 * 60 * @since 6 61 */ 62 ~BleScanResult(); 63 64 /** 65 * @brief Get service uuids. 66 * 67 * @return Returns service uuids. 68 * @since 6 69 */ 70 std::vector<UUID> GetServiceUuids() const; 71 72 /** 73 * @brief Get manufacture data. 74 * 75 * @return Returns manufacture data. 76 * @since 6 77 */ 78 std::map<uint16_t, std::string> GetManufacturerData() const; 79 80 /** 81 * @brief Get service data. 82 * 83 * @return Returns service data. 84 * @since 6 85 */ 86 std::map<UUID, std::string> GetServiceData() const; 87 88 /** 89 * @brief Get peripheral device. 90 * 91 * @return Returns peripheral device pointer. 92 * @since 6 93 */ 94 BluetoothRemoteDevice GetPeripheralDevice() const; 95 96 /** 97 * @brief Get peer device rssi. 98 * 99 * @return Returns peer device rssi. 100 * @since 6 101 */ 102 int8_t GetRssi() const; 103 104 /** 105 * @brief Check if device is connctable. 106 * 107 * @return Returns <b>true</b> if device is connctable; 108 * returns <b>false</b> if device is not connctable. 109 * @since 6 110 */ 111 bool IsConnectable() const; 112 113 /** 114 * @brief Get advertiser flag. 115 * 116 * @return Returns advertiser flag. 117 * @since 6 118 */ 119 uint8_t GetAdvertiseFlag() const; 120 121 /** 122 * @brief Get payload. 123 * 124 * @return Returns payload. 125 * @since 6 126 */ 127 std::vector<uint8_t> GetPayload() const; 128 /** 129 * @brief Add manufacture data. 130 * 131 * @param manufacturerId Manufacture Id which addad data. 132 * @param data Manufacture data 133 * @since 6 134 */ 135 136 void AddManufacturerData(uint16_t manufacturerId, const std::string &data); 137 138 /** 139 * @brief Add service data. 140 * 141 * @param uuid Uuid of service data. 142 * @param serviceData Service data. 143 * @since 6 144 */ 145 void AddServiceData(const UUID &uuid, const std::string &serviceData); 146 147 /** 148 * @brief Add service uuid. 149 * 150 * @param serviceUuid Service uuid. 151 * @since 6 152 */ 153 void AddServiceUuid(const UUID &serviceUuid); 154 155 /** 156 * @brief Add data to payload. 157 * 158 * @param payload Added payload. 159 * @since 6 160 */ 161 void SetPayload(std::string payload); 162 163 /** 164 * @brief Set peripheral device. 165 * 166 * @param device Remote device. 167 * @since 6 168 */ 169 void SetPeripheralDevice(const BluetoothRemoteDevice &device); 170 171 /** 172 * @brief Set peer device rssi. 173 * 174 * @param rssi Peer device rssi. 175 * @since 6 176 */ 177 void SetRssi(int8_t rssi); 178 179 /** 180 * @brief Set connectable. 181 * 182 * @param connectable Whether it is connectable. 183 * @since 6 184 */ 185 void SetConnectable(bool connectable); 186 187 /** 188 * @brief Set advertiser flag. 189 * 190 * @param flag Advertiser flag. 191 * @since 6 192 */ 193 void SetAdvertiseFlag(uint8_t flag); 194 195 void SetName(const std::string &name); 196 std::string GetName(void); 197 198 void SetEventType(uint16_t eventType); 199 uint16_t GetEventType(void) const; 200 201 private: 202 std::vector<UUID> serviceUuids_ {}; 203 std::map<uint16_t, std::string> manufacturerSpecificData_ {}; 204 std::map<UUID, std::string> serviceData_ {}; 205 BluetoothRemoteDevice peripheralDevice_ {}; 206 int8_t rssi_ {}; 207 bool connectable_ {}; 208 uint8_t advertiseFlag_ {}; 209 std::vector<uint8_t> payload_ {}; 210 std::string name_ {}; 211 uint16_t eventType_ {}; 212 }; 213 /** 214 * @brief Represents central manager callback. 215 * 216 * @since 6 217 */ 218 class BleCentralManagerCallback { 219 public: 220 /** 221 * @brief A destructor used to delete the <b>BleCentralManagerCallback</b> instance. 222 * 223 * @since 6 224 */ 225 virtual ~BleCentralManagerCallback() = default; 226 227 /** 228 * @brief Scan result callback. 229 * 230 * @param result Scan result. 231 * @since 6 232 */ 233 virtual void OnScanCallback(const BleScanResult &result) = 0; 234 235 /** 236 * @brief Scan result for found or lost callback type. 237 * 238 * @param result Scan result. 239 * @param callbackType callback Type. 240 * @since 12 241 */ 242 virtual void OnFoundOrLostCallback(const BleScanResult &result, uint8_t callbackType) = 0; 243 244 /** 245 * @brief Batch scan results event callback. 246 * 247 * @param results Scan results. 248 * @since 6 249 */ 250 virtual void OnBleBatchScanResultsEvent(const std::vector<BleScanResult> &results) = 0; 251 252 /** 253 * @brief Start or Stop scan event callback. 254 * 255 * @param resultCode Scan result code. 256 * @param isStartScan true->start scan, false->stop scan. 257 * @since 6 258 */ 259 virtual void OnStartOrStopScanEvent(int resultCode, bool isStartScan) = 0; 260 261 /** 262 * @brief low power device msg report event callback. 263 * 264 * @param uuid Service uuid. 265 * @param msgType Report msg type. 266 * @param value Msg data. 267 * @since 6 268 */ OnNotifyMsgReportFromLpDevice(const UUID & uuid,int msgType,const std::vector<uint8_t> & value)269 virtual void OnNotifyMsgReportFromLpDevice(const UUID &uuid, int msgType, const std::vector<uint8_t> &value) {}; 270 }; 271 272 /** 273 * @brief Represents Scan settings. 274 * 275 * @since 6 276 */ 277 class BLUETOOTH_API BleScanSettings { 278 public: 279 /** 280 * @brief A constructor used to create a <b>BleScanSettings</b> instance. 281 * 282 * @since 6 283 */ 284 BleScanSettings(); 285 286 /** 287 * @brief A destructor used to delete the <b>BleScanSettings</b> instance. 288 * 289 * @since 6 290 */ 291 ~BleScanSettings(); 292 293 /** 294 * @brief Set repport delay time. 295 * 296 * @param reportDelayMillis Repport delay time. 297 * @since 6 298 */ 299 void SetReportDelay(long reportDelayMillis = 0); 300 301 /** 302 * @brief Get repport delay time. 303 * 304 * @return Returns Repport delay time. 305 * @since 6 306 */ 307 long GetReportDelayMillisValue() const; 308 309 /** 310 * @brief Set scan mode. 311 * 312 * @param scanMode Scan mode. 313 * @return Returns <b>true</b> if set scanMode successfully; 314 * returns <b>false</b> if set scanMode failed. 315 * @since 6 316 */ 317 int SetScanMode(int scanMode); 318 319 /** 320 * @brief Get scan mode. 321 * 322 * @return Scan mode. 323 * @since 6 324 */ 325 int GetScanMode() const; 326 327 /** 328 * @brief Set legacy flag. 329 * 330 * @param legacy Legacy value. 331 * @since 6 332 */ 333 void SetLegacy(bool legacy); 334 335 /** 336 * @brief Get legacy flag. 337 * 338 * @return Legacy flag. 339 * @since 6 340 */ 341 bool GetLegacy() const; 342 343 /** 344 * @brief Set phy value. 345 * 346 * @param phy Phy value. 347 * @since 6 348 */ 349 void SetPhy(int phy); 350 351 /** 352 * @brief Get phy value. 353 * 354 * @return Phy value. 355 * @since 6 356 */ 357 int GetPhy() const; 358 359 /** 360 * @brief Set callback type. 361 * 362 * @param callbackType callback type. 363 * @since 12 364 */ 365 void SetCallbackType(uint8_t callbackType); 366 367 /** 368 * @brief Get callback type. 369 * 370 * @return callback type value. 371 * @since 12 372 */ 373 uint8_t GetCallbackType() const; 374 375 /** 376 * @brief Set match track adv type for total number of advertisers to track per filter. 377 * 378 * @param matchTrackAdvType match track adv type value. 379 * @since 12 380 */ 381 void SetMatchTrackAdvType(uint8_t matchTrackAdvType); 382 /** 383 * @brief Get match track adv type. 384 * 385 * @return match track adv type value. 386 * @since 12 387 */ 388 uint8_t GetMatchTrackAdvType() const; 389 390 private: 391 long reportDelayMillis_ = 0; 392 int scanMode_ = SCAN_MODE_LOW_POWER; 393 bool legacy_ = true; 394 int phy_ = PHY_LE_1M; 395 uint8_t callbackType_ = BLE_SCAN_CALLBACK_TYPE_ALL_MATCH; 396 uint8_t matchTrackAdvType_ = MAX_MATCH_TRACK_ADV; 397 }; 398 399 /** 400 * @brief Represents Scan filter. 401 * 402 */ 403 class BLUETOOTH_API BleScanFilter { 404 public: 405 /** 406 * @brief A constructor used to create a <b>BleScanFilter</b> instance. 407 * 408 */ 409 BleScanFilter(); 410 411 /** 412 * @brief A destructor used to delete the <b>BleScanFilter</b> instance. 413 * 414 */ 415 ~BleScanFilter(); 416 417 void SetDeviceId(std::string deviceId); 418 419 std::string GetDeviceId() const; 420 421 void SetName(std::string name); 422 423 std::string GetName() const; 424 425 void SetServiceUuid(const UUID &uuid); 426 427 bool HasServiceUuid(); 428 429 UUID GetServiceUuid() const; 430 431 void SetServiceUuidMask(const UUID &serviceUuidMask); 432 433 bool HasServiceUuidMask(); 434 435 UUID GetServiceUuidMask() const; 436 437 void SetServiceSolicitationUuid(const UUID &serviceSolicitationUuid); 438 439 bool HasSolicitationUuid(); 440 441 UUID GetServiceSolicitationUuid() const; 442 443 void SetServiceSolicitationUuidMask(const UUID &erviceSolicitationUuidMask); 444 445 bool HasSolicitationUuidMask(); 446 447 UUID GetServiceSolicitationUuidMask() const; 448 449 void SetServiceData(std::vector<uint8_t> serviceData); 450 451 std::vector<uint8_t> GetServiceData() const; 452 453 void SetServiceDataMask(std::vector<uint8_t> serviceDataMask); 454 455 std::vector<uint8_t> GetServiceDataMask() const; 456 457 void SetManufacturerId(uint16_t manufacturerId); 458 459 uint16_t GetManufacturerId() const; 460 461 void SetManufactureData(std::vector<uint8_t> manufactureData); 462 463 std::vector<uint8_t> GetManufactureData() const; 464 465 void SetManufactureDataMask(std::vector<uint8_t> manufactureDataMask); 466 467 std::vector<uint8_t> GetManufactureDataMask() const; 468 469 void SetAdvIndReportFlag(bool advIndReprot); 470 471 bool GetAdvIndReportFlag() const; 472 473 /** 474 * @brief Compare two BleScanFilter whether are same or not. 475 * 476 * @param rhs Compared BleScanFilter instance. 477 * @return Returns <b>true</b> if this BleScanFilter is the same as compared BleScanFilter; 478 * returns <b>false</b> if this BleScanFilter is not the same as compared BleScanFilter. 479 */ 480 bool operator==(const BleScanFilter &rhs) const 481 { 482 return (deviceId_ == rhs.deviceId_) && 483 (name_ == rhs.name_) && 484 (serviceUuid_ == rhs.serviceUuid_) && 485 (serviceUuidMask_ == rhs.serviceUuidMask_) && 486 (serviceSolicitationUuid_ == rhs.serviceSolicitationUuid_) && 487 (serviceSolicitationUuidMask_ == rhs.serviceSolicitationUuidMask_) && 488 (hasServiceUuid_ == rhs.hasServiceUuid_) && 489 (hasServiceUuidMask_ == rhs.hasServiceUuidMask_) && 490 (hasSolicitationUuid_ == rhs.hasSolicitationUuid_) && 491 (hasSolicitationUuidMask_ == rhs.hasSolicitationUuidMask_) && 492 (serviceData_ == rhs.serviceData_) && 493 (serviceDataMask_ == rhs.serviceDataMask_) && 494 (manufacturerId_ == rhs.manufacturerId_) && 495 (manufactureData_ == rhs.manufactureData_) && 496 (manufactureDataMask_ == rhs.manufactureDataMask_) && 497 (advIndReprot_ == rhs.advIndReprot_); 498 } 499 500 private: 501 std::string deviceId_; 502 std::string name_; 503 504 UUID serviceUuid_; 505 UUID serviceUuidMask_; 506 UUID serviceSolicitationUuid_; 507 UUID serviceSolicitationUuidMask_; 508 bool hasServiceUuid_ = false; 509 bool hasServiceUuidMask_ = false; 510 bool hasSolicitationUuid_ = false; 511 bool hasSolicitationUuidMask_ = false; 512 513 std::vector<uint8_t> serviceData_; 514 std::vector<uint8_t> serviceDataMask_; 515 516 uint16_t manufacturerId_ = 0; 517 std::vector<uint8_t> manufactureData_; 518 std::vector<uint8_t> manufactureDataMask_; 519 bool advIndReprot_ = false; 520 }; 521 522 struct BleActiveDeviceInfo { 523 std::vector<int8_t> deviceId; 524 int32_t status; 525 int32_t timeOut; 526 }; 527 528 struct BleLpDeviceParamSet { 529 BleScanSettings scanSettings; 530 std::vector<BleScanFilter> scanFilters; 531 BleAdvertiserSettings advSettings; 532 std::vector<uint8_t> advData; 533 std::vector<uint8_t> respData; 534 UUID uuid; 535 std::vector<BleActiveDeviceInfo> activeDeviceInfos; 536 int32_t deliveryMode; 537 int32_t advHandle; 538 int32_t duration; 539 uint32_t fieldValidFlagBit; 540 }; 541 542 /** 543 * @brief Represents central manager. 544 * 545 * @since 6 546 */ 547 class BLUETOOTH_API BleCentralManager { 548 public: 549 /** 550 * @brief A constructor used to create a <b>BleCentralManager</b> instance. 551 * 552 * @param cllback Central manager callback to create an <b>BleCentralManagerCallback</b> instance. 553 * @since 6 554 */ 555 explicit BleCentralManager(BleCentralManagerCallback &callback); 556 557 /** 558 * @brief A constructor used to create a <b>BleCentralManager</b> instance. 559 * 560 * @param cllback Central manager callback to create an <b>BleCentralManagerCallback</b> instance. 561 * @param enableRandomAddrMode Indicates whether to use random address for interface interaction. 562 * @since 6 563 */ 564 explicit BleCentralManager(std::shared_ptr<BleCentralManagerCallback> callback, bool enableRandomAddrMode = true); 565 566 /** 567 * @brief A destructor used to delete the <b>BleCentralManager</b> instance. 568 * 569 * @since 6 570 */ 571 ~BleCentralManager(); 572 573 /** 574 * @brief Start scan. 575 * 576 * @param settings Scan settings. 577 * @param filters Scan filters. 578 * @since 11 579 */ 580 int StartScan(const BleScanSettings &settings, const std::vector<BleScanFilter> &filters); 581 582 /** 583 * @brief Stop scan. 584 * 585 * @since 6 586 */ 587 int StopScan(); 588 589 /** 590 * @brief set low power device adv param. 591 * 592 * @param duration advertise duration. 593 * @param maxExtAdvEvents maximum number of extended advertising events. 594 * @param window work window. 595 * @param interval work interval. 596 * @param advHandle Indicates the advertisement handle. 597 * @return Result. 598 */ 599 int SetLpDeviceAdvParam(int duration, int maxExtAdvEvents, int window, int interval, int advHandle); 600 601 /** 602 * @brief set scan result report channel. 603 * 604 * @param enable the switch of report.(true: report msg to low power device; false: not report). 605 * @return Result. 606 */ 607 int SetScanReportChannelToLpDevice(bool enable); 608 609 /** 610 * @brief Enable sync data to low power device. 611 * 612 * @return Result. 613 */ 614 int EnableSyncDataToLpDevice(); 615 616 /** 617 * @brief Disable sync data to low power device. 618 * 619 * @return Result. 620 */ 621 int DisableSyncDataToLpDevice(); 622 623 /** 624 * @brief Translate ParamData to low power device. 625 * 626 * @param data Indicates the pointer to the data. 627 * @param dataSize Indicates the data size. 628 * @param type Indicates the data type. 629 * @return Result. 630 */ 631 int SendParamsToLpDevice(const std::vector<uint8_t> &dataValue, int32_t type); 632 633 /** 634 * @brief Get whether support low power device. 635 * 636 * @return true: available; false: not available. 637 * @since 6 638 */ 639 bool IsLpDeviceAvailable(); 640 641 /** 642 * @brief Set low power device param data. 643 * 644 * @param lpDeviceParamSet low power device param data. 645 * @return Result 646 * @since 6 647 */ 648 int SetLpDeviceParam(const BleLpDeviceParamSet &lpDeviceParamSet); 649 650 /** 651 * @brief Remove low power device param data. 652 * 653 * @param uuid Uuid. 654 * @return Result 655 * @since 6 656 */ 657 int RemoveLpDeviceParam(const UUID &uuid); 658 659 private: 660 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BleCentralManager); 661 BLUETOOTH_DECLARE_IMPL(); 662 }; 663 } // namespace Bluetooth 664 } // namespace OHOS 665 #endif