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