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 report mode. 329 * 330 * @param reportMode Report mode. 331 * @since 19 332 */ 333 void SetReportMode(int reportMode); 334 335 /** 336 * @brief Get report mode. 337 * 338 * @return Report mode. 339 * @since 19 340 */ 341 int GetReportMode() const; 342 343 /** 344 * @brief Set legacy flag. 345 * 346 * @param legacy Legacy value. 347 * @since 6 348 */ 349 void SetLegacy(bool legacy); 350 351 /** 352 * @brief Get legacy flag. 353 * 354 * @return Legacy flag. 355 * @since 6 356 */ 357 bool GetLegacy() const; 358 359 /** 360 * @brief Set phy value. 361 * 362 * @param phy Phy value. 363 * @since 6 364 */ 365 void SetPhy(int phy); 366 367 /** 368 * @brief Get phy value. 369 * 370 * @return Phy value. 371 * @since 6 372 */ 373 int GetPhy() const; 374 375 /** 376 * @brief Set callback type. 377 * 378 * @param callbackType callback type. 379 * @since 12 380 */ 381 void SetCallbackType(uint8_t callbackType); 382 383 /** 384 * @brief Get callback type. 385 * 386 * @return callback type value. 387 * @since 12 388 */ 389 uint8_t GetCallbackType() const; 390 391 /** 392 * @brief Set sensitivity mode. 393 * 394 * @param sensitivityMode sensitivity mode. 395 * @since 15 396 */ 397 void SetSensitivityMode(uint8_t sensitivityMode); 398 399 /** 400 * @brief Get sensitivity mode. 401 * 402 * @return sensitivity mode value. 403 * @since 15 404 */ 405 uint8_t GetSensitivityMode() const; 406 407 /** 408 * @brief Set match track adv type for total number of advertisers to track per filter. 409 * 410 * @param matchTrackAdvType match track adv type value. 411 * @since 12 412 */ 413 void SetMatchTrackAdvType(uint8_t matchTrackAdvType); 414 /** 415 * @brief Get match track adv type. 416 * 417 * @return match track adv type value. 418 * @since 12 419 */ 420 uint8_t GetMatchTrackAdvType() const; 421 422 private: 423 long reportDelayMillis_ = 0; 424 int scanMode_ = SCAN_MODE_LOW_POWER; 425 int reportMode_ = REPORT_MODE_NORMAL; 426 bool legacy_ = true; 427 int phy_ = PHY_LE_1M; 428 uint8_t callbackType_ = BLE_SCAN_CALLBACK_TYPE_ALL_MATCH; 429 uint8_t sensitivityMode_ = SENSITIVITY_MODE::SENSITIVITY_MODE_HIGH; 430 uint8_t matchTrackAdvType_ = MAX_MATCH_TRACK_ADV; 431 }; 432 433 /** 434 * @brief Represents Scan filter. 435 * 436 */ 437 class BLUETOOTH_API BleScanFilter { 438 public: 439 /** 440 * @brief A constructor used to create a <b>BleScanFilter</b> instance. 441 * 442 */ 443 BleScanFilter(); 444 445 /** 446 * @brief A destructor used to delete the <b>BleScanFilter</b> instance. 447 * 448 */ 449 ~BleScanFilter(); 450 451 void SetDeviceId(std::string deviceId); 452 453 std::string GetDeviceId() const; 454 455 void SetName(std::string name); 456 457 std::string GetName() const; 458 459 void SetServiceUuid(const UUID &uuid); 460 461 bool HasServiceUuid(); 462 463 UUID GetServiceUuid() const; 464 465 void SetServiceUuidMask(const UUID &serviceUuidMask); 466 467 bool HasServiceUuidMask(); 468 469 UUID GetServiceUuidMask() const; 470 471 void SetServiceSolicitationUuid(const UUID &serviceSolicitationUuid); 472 473 bool HasSolicitationUuid(); 474 475 UUID GetServiceSolicitationUuid() const; 476 477 void SetServiceSolicitationUuidMask(const UUID &erviceSolicitationUuidMask); 478 479 bool HasSolicitationUuidMask(); 480 481 UUID GetServiceSolicitationUuidMask() const; 482 483 void SetServiceData(std::vector<uint8_t> serviceData); 484 485 std::vector<uint8_t> GetServiceData() const; 486 487 void SetServiceDataMask(std::vector<uint8_t> serviceDataMask); 488 489 std::vector<uint8_t> GetServiceDataMask() const; 490 491 void SetManufacturerId(uint16_t manufacturerId); 492 493 uint16_t GetManufacturerId() const; 494 495 void SetManufactureData(std::vector<uint8_t> manufactureData); 496 497 std::vector<uint8_t> GetManufactureData() const; 498 499 void SetManufactureDataMask(std::vector<uint8_t> manufactureDataMask); 500 501 std::vector<uint8_t> GetManufactureDataMask() const; 502 503 void SetAdvIndReportFlag(bool advIndReprot); 504 505 bool GetAdvIndReportFlag() const; 506 507 void SetFilterIndex(uint8_t index); 508 509 uint8_t GetFilterIndex() const; 510 /** 511 * @brief Compare two BleScanFilter whether are same or not. 512 * 513 * @param rhs Compared BleScanFilter instance. 514 * @return Returns <b>true</b> if this BleScanFilter is the same as compared BleScanFilter; 515 * returns <b>false</b> if this BleScanFilter is not the same as compared BleScanFilter. 516 */ 517 bool operator==(const BleScanFilter &rhs) const 518 { 519 return (deviceId_ == rhs.deviceId_) && 520 (name_ == rhs.name_) && 521 (serviceUuid_ == rhs.serviceUuid_) && 522 (serviceUuidMask_ == rhs.serviceUuidMask_) && 523 (serviceSolicitationUuid_ == rhs.serviceSolicitationUuid_) && 524 (serviceSolicitationUuidMask_ == rhs.serviceSolicitationUuidMask_) && 525 (hasServiceUuid_ == rhs.hasServiceUuid_) && 526 (hasServiceUuidMask_ == rhs.hasServiceUuidMask_) && 527 (hasSolicitationUuid_ == rhs.hasSolicitationUuid_) && 528 (hasSolicitationUuidMask_ == rhs.hasSolicitationUuidMask_) && 529 (serviceData_ == rhs.serviceData_) && 530 (serviceDataMask_ == rhs.serviceDataMask_) && 531 (manufacturerId_ == rhs.manufacturerId_) && 532 (manufactureData_ == rhs.manufactureData_) && 533 (manufactureDataMask_ == rhs.manufactureDataMask_) && 534 (advIndReprot_ == rhs.advIndReprot_) && 535 (filterIndex_ == rhs.filterIndex_); 536 } 537 538 private: 539 std::string deviceId_; 540 std::string name_; 541 542 UUID serviceUuid_; 543 UUID serviceUuidMask_; 544 UUID serviceSolicitationUuid_; 545 UUID serviceSolicitationUuidMask_; 546 bool hasServiceUuid_ = false; 547 bool hasServiceUuidMask_ = false; 548 bool hasSolicitationUuid_ = false; 549 bool hasSolicitationUuidMask_ = false; 550 551 std::vector<uint8_t> serviceData_; 552 std::vector<uint8_t> serviceDataMask_; 553 554 uint16_t manufacturerId_ = 0; 555 std::vector<uint8_t> manufactureData_; 556 std::vector<uint8_t> manufactureDataMask_; 557 bool advIndReprot_ = false; 558 uint8_t filterIndex_ = 0; 559 }; 560 561 struct BleActiveDeviceInfo { 562 std::vector<int8_t> deviceId; 563 int32_t status; 564 int32_t timeOut; 565 }; 566 567 struct BleLpDeviceParamSet { 568 BleScanSettings scanSettings; 569 std::vector<BleScanFilter> scanFilters; 570 BleAdvertiserSettings advSettings; 571 std::vector<uint8_t> advData; 572 std::vector<uint8_t> respData; 573 UUID uuid; 574 std::vector<BleActiveDeviceInfo> activeDeviceInfos; 575 int32_t deliveryMode; 576 int32_t advHandle; 577 int32_t duration; 578 uint32_t fieldValidFlagBit; 579 }; 580 581 /** 582 * @brief Represents central manager. 583 * 584 * @since 6 585 */ 586 class BLUETOOTH_API BleCentralManager { 587 public: 588 /** 589 * @brief A constructor used to create a <b>BleCentralManager</b> instance. 590 * 591 * @param cllback Central manager callback to create an <b>BleCentralManagerCallback</b> instance. 592 * @since 6 593 */ 594 explicit BleCentralManager(BleCentralManagerCallback &callback); 595 596 /** 597 * @brief A constructor used to create a <b>BleCentralManager</b> instance. 598 * 599 * @param cllback Central manager callback to create an <b>BleCentralManagerCallback</b> instance. 600 * @param enableRandomAddrMode Indicates whether to use random address for interface interaction. 601 * @since 6 602 */ 603 explicit BleCentralManager(std::shared_ptr<BleCentralManagerCallback> callback, bool enableRandomAddrMode = true); 604 605 /** 606 * @brief A destructor used to delete the <b>BleCentralManager</b> instance. 607 * 608 * @since 6 609 */ 610 ~BleCentralManager(); 611 612 /** 613 * @brief Start scan. 614 * 615 * @param settings Scan settings. 616 * @param filters Scan filters. 617 * @since 11 618 */ 619 int StartScan(const BleScanSettings &settings, const std::vector<BleScanFilter> &filters); 620 621 /** 622 * @brief Stop scan. 623 * 624 * @since 6 625 */ 626 int StopScan(); 627 628 /** 629 * @brief set low power device adv param. 630 * 631 * @param duration advertise duration. 632 * @param maxExtAdvEvents maximum number of extended advertising events. 633 * @param window work window. 634 * @param interval work interval. 635 * @param advHandle Indicates the advertisement handle. 636 * @return Result. 637 */ 638 int SetLpDeviceAdvParam(int duration, int maxExtAdvEvents, int window, int interval, int advHandle); 639 640 /** 641 * @brief set scan result report channel. 642 * 643 * @param enable the switch of report.(true: report msg to low power device; false: not report). 644 * @return Result. 645 */ 646 int SetScanReportChannelToLpDevice(bool enable); 647 648 /** 649 * @brief Enable sync data to low power device. 650 * 651 * @return Result. 652 */ 653 int EnableSyncDataToLpDevice(); 654 655 /** 656 * @brief Disable sync data to low power device. 657 * 658 * @return Result. 659 */ 660 int DisableSyncDataToLpDevice(); 661 662 /** 663 * @brief Translate ParamData to low power device. 664 * 665 * @param data Indicates the pointer to the data. 666 * @param dataSize Indicates the data size. 667 * @param type Indicates the data type. 668 * @return Result. 669 */ 670 int SendParamsToLpDevice(const std::vector<uint8_t> &dataValue, int32_t type); 671 672 /** 673 * @brief Get whether support low power device. 674 * 675 * @return true: available; false: not available. 676 * @since 6 677 */ 678 bool IsLpDeviceAvailable(); 679 680 /** 681 * @brief Set low power device param data. 682 * 683 * @param lpDeviceParamSet low power device param data. 684 * @return Result 685 * @since 6 686 */ 687 int SetLpDeviceParam(const BleLpDeviceParamSet &lpDeviceParamSet); 688 689 /** 690 * @brief Remove low power device param data. 691 * 692 * @param uuid Uuid. 693 * @return Result 694 * @since 6 695 */ 696 int RemoveLpDeviceParam(const UUID &uuid); 697 698 /** 699 * @brief Change a scan with BleScanConfigs and filter. 700 * If don't change ble scan filter, set std::vector<BleScanFilter> empty or set filterAction none. 701 * 702 * @param settings Scan settings. 703 * @param filters Scan filters. 704 * @param filterAction Indicates change filter behavior. see {@link BleScanUpdateFilterAction}. 705 * @return Returns {@link BT_NO_ERROR} if the scan is started. 706 * returns an error code defined in {@link BtStatus} otherwise. 707 * @since 16 708 */ 709 int ChangeScanParams(const BleScanSettings &settings, const std::vector<BleScanFilter> &filter, 710 uint32_t filterAction); 711 712 void SetNewApiFlag(); 713 void CheckValidScannerId(); 714 private: 715 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BleCentralManager); 716 BLUETOOTH_DECLARE_IMPL(); 717 bool isNewApi_ = false; 718 }; 719 } // namespace Bluetooth 720 } // namespace OHOS 721 #endif