1 /* 2 * Copyright (C) 2021 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 advertiser, including avertise data and callbacks, and advertiser functions. 21 * 22 * @since 6 23 */ 24 25 /** 26 * @file bluetooth_ble_advertiser.h 27 * 28 * @brief Advertiser common functions. 29 * 30 * @since 6 31 */ 32 33 #ifndef BLUETOOTH_BLE_ADVERTISER_H 34 #define BLUETOOTH_BLE_ADVERTISER_H 35 36 #include "bluetooth_def.h" 37 #include "bluetooth_types.h" 38 #include "ohos_bt_def.h" 39 40 namespace OHOS { 41 namespace Bluetooth { 42 /** 43 * @brief Represents advertise data. 44 * 45 * @since 6 46 */ 47 class BLUETOOTH_API BleAdvertiserData { 48 public: 49 /** 50 * @brief A constructor used to create a <b>BleAdvertiserData</b> instance. 51 * 52 * @since 6 53 */ 54 BleAdvertiserData(); 55 56 /** 57 * @brief A destructor used to delete the <b>BleAdvertiserData</b> instance. 58 * 59 * @since 6 60 */ 61 ~BleAdvertiserData(); 62 63 /** 64 * @brief Add manufacture data. 65 * 66 * @param manufacturerId Manufacture Id which addad data. 67 * @param data Manufacture data 68 * @since 6 69 */ 70 void AddManufacturerData(uint16_t manufacturerId, const std::string &data); 71 72 /** 73 * @brief Add service data. 74 * 75 * @param uuid Uuid of service data. 76 * @param serviceData Service data. 77 * @since 6 78 */ 79 void AddServiceData(const ParcelUuid &uuid, const std::string &serviceData); 80 81 /** 82 * @brief Add service uuid. 83 * 84 * @param serviceUuid Service uuid. 85 * @since 6 86 */ 87 void AddServiceUuid(const ParcelUuid &serviceUuid); 88 89 /** 90 * @brief Get manufacture data. 91 * 92 * @return Returns manufacture data. 93 * @since 6 94 */ 95 std::map<uint16_t, std::string> GetManufacturerData() const; 96 97 /** 98 * @brief Get service data. 99 * 100 * @return Returns service data. 101 * @since 6 102 */ 103 std::map<ParcelUuid, std::string> GetServiceData() const; 104 105 /** 106 * @brief Get service uuids. 107 * 108 * @return Returns service uuids. 109 * @since 6 110 */ 111 std::vector<ParcelUuid> GetServiceUuids() const; 112 113 /** 114 * @brief Get advertiser flag. 115 * 116 * @return Returns advertiser flag. 117 * @since 6 118 */ 119 uint8_t GetAdvFlag() const; 120 121 /** 122 * @brief Set advertiser flag. 123 * 124 * @param flag Advertiser flag. 125 * @return Returns error flag. 126 * @since 6 127 */ 128 void SetAdvFlag(uint8_t flag); 129 130 /** 131 * @brief Get whether the device name will be included in the advertisement packet. 132 * 133 * @return Returns includeDeviceName flag. 134 * @since 6 135 */ 136 bool GetIncludeDeviceName() const; 137 138 /** 139 * @brief Set whether the device name will be included in the advertisement packet. 140 * 141 * @param flag includeDeviceName flag. 142 * @since 6 143 */ 144 void SetIncludeDeviceName(bool flag); 145 146 /** 147 * @brief Get whether the txpower will be included in the advertisement packet. 148 * 149 * @return Returns includeTxPower flag. 150 * @since 10 151 */ 152 bool GetIncludeTxPower() const; 153 154 /** 155 * @brief Set whether the txpower will be included in the advertisement packet. 156 * 157 * @param flag includeTxPower flag. 158 * @since 10 159 */ 160 void SetIncludeTxPower(bool flag); 161 162 private: 163 std::vector<ParcelUuid> serviceUuids_{}; 164 std::map<uint16_t, std::string> manufacturerSpecificData_{}; 165 std::map<ParcelUuid, std::string> serviceData_{}; 166 uint8_t advFlag_ = BLE_ADV_FLAG_GEN_DISC; 167 bool includeDeviceName_ = false; 168 bool includeTxPower_ = false; 169 }; 170 171 /** 172 * @brief Represents advertise settings. 173 * 174 * @since 6 175 */ 176 class BLUETOOTH_API BleAdvertiserSettings { 177 public: 178 /** 179 * @brief A constructor used to create a <b>BleAdvertiseSettings</b> instance. 180 * 181 * @since 6 182 */ 183 BleAdvertiserSettings(); 184 185 /** 186 * @brief A destructor used to delete the <b>BleAdvertiseSettings</b> instance. 187 * 188 * @since 6 189 */ 190 ~BleAdvertiserSettings(); 191 192 /** 193 * @brief Check if device service is connctable. 194 * 195 * @return Returns <b>true</b> if device service is connctable; 196 * returns <b>false</b> if device service is not connctable. 197 * @since 6 198 */ 199 bool IsConnectable() const; 200 201 /** 202 * @brief Check if advertiser is legacy mode. 203 * 204 * @return Returns <b>true</b> if advertiser is legacy mode; 205 * returns <b>false</b> if advertiser is not legacy mode. 206 * @since 6 207 */ 208 bool IsLegacyMode() const; 209 210 /** 211 * @brief Set connectable. 212 * 213 * @param connectable Whether it is connectable. 214 * @since 6 215 */ 216 void SetConnectable(bool connectable); 217 218 /** 219 * @brief Set legacyMode. 220 * 221 * @param legacyMode Whether it is legacyMode. 222 * @since 6 223 */ 224 void SetLegacyMode(bool legacyMode); 225 226 /** 227 * @brief Get advertise interval. 228 * 229 * @return Returns advertise interval. 230 * @since 6 231 */ 232 uint16_t GetInterval() const; 233 234 /** 235 * @brief Get Tx power. 236 * 237 * @return Returns Tx power. 238 * @since 6 239 */ 240 int8_t GetTxPower() const; 241 242 /** 243 * @brief Set advertise interval. 244 * 245 * @param interval Advertise interval. 246 * @since 6 247 */ 248 void SetInterval(uint16_t interval); 249 250 /** 251 * @brief Set Tx power. 252 * 253 * @param txPower Tx power. 254 * @since 6 255 */ 256 void SetTxPower(int8_t txPower); 257 258 /** 259 * @brief Get primary phy. 260 * 261 * @return Returns primary phy. 262 * @since 6 263 */ 264 int GetPrimaryPhy() const; 265 266 /** 267 * @brief Set primary phy. 268 * 269 * @param primaryPhy Primary phy. 270 * @since 6 271 */ 272 void SetPrimaryPhy(int primaryPhy); 273 274 /** 275 * @brief Get second phy. 276 * 277 * @return Returns primary phy. 278 * @since 6 279 */ 280 int GetSecondaryPhy() const; 281 282 /** 283 * @brief Set second phy. 284 * 285 * @param secondaryPhy Second phy. 286 * @since 6 287 */ 288 void SetSecondaryPhy(int secondaryPhy); 289 290 /** 291 * @brief Get own address. 292 * 293 * @param addr Own address. 294 * @since 6 295 */ 296 std::array<uint8_t, OHOS_BD_ADDR_LEN> GetOwnAddr() const; 297 298 /** 299 * @brief Set own address. 300 * 301 * @param addr Own address. 302 * @since 6 303 */ 304 void SetOwnAddr(const std::array<uint8_t, OHOS_BD_ADDR_LEN>& addr); 305 306 /** 307 * @brief Get own address type. 308 * 309 * @return Returns own address type. 310 * @since 6 311 */ 312 int8_t GetOwnAddrType() const; 313 314 /** 315 * @brief Set own address type. 316 * 317 * @param addrType Own address type. 318 * @since 6 319 */ 320 void SetOwnAddrType(int8_t addrType); 321 322 private: 323 bool connectable_ = true; 324 bool legacyMode_ = true; 325 uint16_t interval_ = BLE_ADV_DEFAULT_INTERVAL; 326 int8_t txPower_ = BLE_ADV_TX_POWER_MEDIUM_VALUE; 327 int primaryPhy_ = BLE_ADVERTISEMENT_PHY_1M; 328 int secondaryPhy_ = BLE_ADVERTISEMENT_PHY_1M; 329 std::array<uint8_t, OHOS_BD_ADDR_LEN> ownAddr_ = {}; 330 int8_t ownAddrType_ = -1; 331 }; 332 333 /** 334 * @brief Represents advertise callback. 335 * 336 * @since 6 337 */ 338 class BleAdvertiseCallback { 339 public: 340 /** 341 * @brief A destructor used to delete the <b>BleAdvertiseCallback</b> instance. 342 * 343 * @since 6 344 */ 345 virtual ~BleAdvertiseCallback() = default; 346 347 /** 348 * @brief Start advertising result event callback. 349 * 350 * @param result Start advertising result. 351 * @param advHandle advertising handle. 352 * @since 6 353 */ 354 virtual void OnStartResultEvent(int result, int advHandle) = 0; 355 356 /** 357 * @brief Enable advertising result event callback. 358 * 359 * @param result Enable advertising result. 360 * @param advHandle advertising handle. 361 * @since 11 362 */ 363 virtual void OnEnableResultEvent(int result, int advHandle) = 0; 364 365 /** 366 * @brief Disable advertising result event callback. 367 * 368 * @param result Disable advertising result. 369 * @param advHandle advertising handle. 370 * @since 11 371 */ 372 virtual void OnDisableResultEvent(int result, int advHandle) = 0; 373 374 /** 375 * @brief Stop advertising result event callback. 376 * 377 * @param result Stop advertising result. 378 * @param advHandle advertising handle. 379 * @since 11 380 */ 381 virtual void OnStopResultEvent(int result, int advHandle) = 0; 382 383 /** 384 * @brief Set advertising data result event callback. 385 * 386 * @param result Set advertising data result 387 * @since 6 388 */ 389 virtual void OnSetAdvDataEvent(int result) = 0; 390 391 /** 392 * @brief Get advertising handle callback. 393 * 394 * @param result get advertising handle result. 395 * @param advHandle advertising handle. 396 * @since 11 397 */ 398 virtual void OnGetAdvHandleEvent(int result, int advHandle) = 0; 399 }; 400 401 /** 402 * @brief Represents advertiser. 403 * 404 * @since 6 405 */ 406 class BLUETOOTH_API BleAdvertiser { 407 public: 408 /** 409 * @brief A constructor of BleAdvertiser. 410 * 411 * @since 11 412 * 413 */ 414 static std::shared_ptr<BleAdvertiser> CreateInstance(void); 415 416 /** 417 * @brief A destructor used to delete the <b>BleAdvertiser</b> instance. 418 * 419 * @since 6 420 */ 421 ~BleAdvertiser(); 422 423 /** 424 * @brief Start advertising. 425 * 426 * @param settings Advertise settings. 427 * @param advData Advertise data. 428 * @param scanResponse Scan response. 429 * @param duration advertise duration. 430 * @param callback Advertise callback. 431 * @since 6 432 */ 433 int StartAdvertising(const BleAdvertiserSettings &settings, const BleAdvertiserData &advData, 434 const BleAdvertiserData &scanResponse, uint16_t duration, std::shared_ptr<BleAdvertiseCallback> callback); 435 436 /** 437 * @brief Start advertising. 438 * 439 * @param settings Advertise settings. 440 * @param advData Advertise data. 441 * @param scanResponse Scan response. 442 * @param duration advertise duration. 443 * @param callback Advertise callback. 444 * @since 6 445 */ 446 int StartAdvertising(const BleAdvertiserSettings &settings, const std::vector<uint8_t> &advData, 447 const std::vector<uint8_t> &scanResponse, uint16_t duration, std::shared_ptr<BleAdvertiseCallback> callback); 448 449 /** 450 * @brief Enable advertising. 451 * 452 * @param advHandle Advertise handle. 453 * @param duration Advertise duration. 454 * @param callback Advertise callback. 455 * @since 11 456 */ 457 int EnableAdvertising(uint8_t advHandle, uint16_t duration, std::shared_ptr<BleAdvertiseCallback> callback); 458 459 /** 460 * @brief Disable advertising. 461 * 462 * @param advHandle Advertise handle. 463 * @param callback Advertise callback. 464 * @since 11 465 */ 466 int DisableAdvertising(uint8_t advHandle, std::shared_ptr<BleAdvertiseCallback> callback); 467 468 void SetAdvertisingData(const std::vector<uint8_t> &advData, const std::vector<uint8_t> &scanResponse, 469 std::shared_ptr<BleAdvertiseCallback> callback); 470 int StopAdvertising(std::shared_ptr<BleAdvertiseCallback> callback); 471 472 /** 473 * @brief Cleans up advertisers. 474 * 475 * @param callback Advertise callback. 476 * @since 6 477 */ 478 void Close(std::shared_ptr<BleAdvertiseCallback> callback); 479 480 /** 481 * @brief Get Advertise handle. 482 * 483 * @param callback Advertise callback. 484 * @since 6 485 */ 486 uint8_t GetAdvHandle(std::shared_ptr<BleAdvertiseCallback> callback); 487 488 private: 489 BleAdvertiser(); 490 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BleAdvertiser); 491 BLUETOOTH_DECLARE_IMPL(); 492 493 //The passkey pattern of C++ 494 struct PassKey { PassKeyPassKey495 PassKey() {}; 496 }; 497 public: BleAdvertiser(PassKey)498 explicit BleAdvertiser(PassKey) : BleAdvertiser() {}; 499 }; 500 } // namespace Bluetooth 501 } // namespace OHOS 502 #endif