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 39 namespace OHOS { 40 namespace Bluetooth { 41 /** 42 * @brief Represents advertise data. 43 * 44 * @since 6 45 */ 46 class BLUETOOTH_API BleAdvertiserData { 47 public: 48 /** 49 * @brief A constructor used to create a <b>BleAdvertiserData</b> instance. 50 * 51 * @since 6 52 */ 53 BleAdvertiserData(); 54 55 /** 56 * @brief A destructor used to delete the <b>BleAdvertiserData</b> instance. 57 * 58 * @since 6 59 */ 60 ~BleAdvertiserData(); 61 62 /** 63 * @brief Add manufacture data. 64 * 65 * @param manufacturerId Manufacture Id which addad data. 66 * @param data Manufacture data 67 * @since 6 68 */ 69 void AddManufacturerData(uint16_t manufacturerId, const std::string &data); 70 71 /** 72 * @brief Add service data. 73 * 74 * @param uuid Uuid of service data. 75 * @param serviceData Service data. 76 * @since 6 77 */ 78 void AddServiceData(const ParcelUuid &uuid, const std::string &serviceData); 79 80 /** 81 * @brief Add service uuid. 82 * 83 * @param serviceUuid Service uuid. 84 * @since 6 85 */ 86 void AddServiceUuid(const ParcelUuid &serviceUuid); 87 88 /** 89 * @brief Get manufacture data. 90 * 91 * @return Returns manufacture data. 92 * @since 6 93 */ 94 std::map<uint16_t, std::string> GetManufacturerData() const; 95 96 /** 97 * @brief Get service data. 98 * 99 * @return Returns service data. 100 * @since 6 101 */ 102 std::map<ParcelUuid, std::string> GetServiceData() const; 103 104 /** 105 * @brief Get service uuids. 106 * 107 * @return Returns service uuids. 108 * @since 6 109 */ 110 std::vector<ParcelUuid> GetServiceUuids() const; 111 112 /** 113 * @brief Get advertiser flag. 114 * 115 * @return Returns advertiser flag. 116 * @since 6 117 */ 118 uint8_t GetAdvFlag() const; 119 120 /** 121 * @brief Set advertiser flag. 122 * 123 * @param flag Advertiser flag. 124 * @return Returns error flag. 125 * @since 6 126 */ 127 void SetAdvFlag(uint8_t flag); 128 129 /** 130 * @brief Get whether the device name will be included in the advertisement packet. 131 * 132 * @return Returns includeDeviceName flag. 133 * @since 6 134 */ 135 bool GetIncludeDeviceName() const; 136 137 /** 138 * @brief Set whether the device name will be included in the advertisement packet. 139 * 140 * @param flag includeDeviceName flag. 141 * @since 6 142 */ 143 void SetIncludeDeviceName(bool flag); 144 145 private: 146 std::vector<ParcelUuid> serviceUuids_{}; 147 std::map<uint16_t, std::string> manufacturerSpecificData_{}; 148 std::map<ParcelUuid, std::string> serviceData_{}; 149 uint8_t advFlag_ = BLE_ADV_FLAG_GEN_DISC; 150 bool includeDeviceName_ = false; 151 }; 152 153 /** 154 * @brief Represents advertise settings. 155 * 156 * @since 6 157 */ 158 class BLUETOOTH_API BleAdvertiserSettings { 159 public: 160 /** 161 * @brief A constructor used to create a <b>BleAdvertiseSettings</b> instance. 162 * 163 * @since 6 164 */ 165 BleAdvertiserSettings(); 166 167 /** 168 * @brief A destructor used to delete the <b>BleAdvertiseSettings</b> instance. 169 * 170 * @since 6 171 */ 172 ~BleAdvertiserSettings(); 173 174 /** 175 * @brief Check if device service is connctable. 176 * 177 * @return Returns <b>true</b> if device service is connctable; 178 * returns <b>false</b> if device service is not connctable. 179 * @since 6 180 */ 181 bool IsConnectable() const; 182 183 /** 184 * @brief Check if advertiser is legacy mode. 185 * 186 * @return Returns <b>true</b> if advertiser is legacy mode; 187 * returns <b>false</b> if advertiser is not legacy mode. 188 * @since 6 189 */ 190 bool IsLegacyMode() const; 191 192 /** 193 * @brief Set connectable. 194 * 195 * @param connectable Whether it is connectable. 196 * @since 6 197 */ 198 void SetConnectable(bool connectable); 199 200 /** 201 * @brief Set legacyMode. 202 * 203 * @param legacyMode Whether it is legacyMode. 204 * @since 6 205 */ 206 void SetLegacyMode(bool legacyMode); 207 208 /** 209 * @brief Get advertise interval. 210 * 211 * @return Returns advertise interval. 212 * @since 6 213 */ 214 uint16_t GetInterval() const; 215 216 /** 217 * @brief Get Tx power. 218 * 219 * @return Returns Tx power. 220 * @since 6 221 */ 222 uint8_t GetTxPower() const; 223 224 /** 225 * @brief Set advertise interval. 226 * 227 * @param interval Advertise interval. 228 * @since 6 229 */ 230 void SetInterval(uint16_t interval); 231 232 /** 233 * @brief Set Tx power. 234 * 235 * @param txPower Tx power. 236 * @since 6 237 */ 238 void SetTxPower(uint8_t txPower); 239 240 /** 241 * @brief Get primary phy. 242 * 243 * @return Returns primary phy. 244 * @since 6 245 */ 246 int GetPrimaryPhy() const; 247 248 /** 249 * @brief Set primary phy. 250 * 251 * @param primaryPhy Primary phy. 252 * @since 6 253 */ 254 void SetPrimaryPhy(int primaryPhy); 255 256 /** 257 * @brief Get second phy. 258 * 259 * @return Returns primary phy. 260 * @since 6 261 */ 262 int GetSecondaryPhy() const; 263 264 /** 265 * @brief Set second phy. 266 * 267 * @param secondaryPhy Second phy. 268 * @since 6 269 */ 270 void SetSecondaryPhy(int secondaryPhy); 271 272 private: 273 bool connectable_ = true; 274 bool legacyMode_ = true; 275 uint16_t interval_ = BLE_ADV_DEFAULT_INTERVAL; 276 uint8_t txPower_ = BLE_ADV_TX_POWER_LOW; 277 int primaryPhy_ = BLE_ADVERTISEMENT_PHY_1M; 278 int secondaryPhy_ = BLE_ADVERTISEMENT_PHY_1M; 279 }; 280 281 /** 282 * @brief Represents advertise callback. 283 * 284 * @since 6 285 */ 286 class BleAdvertiseCallback { 287 public: 288 /** 289 * @brief A destructor used to delete the <b>BleAdvertiseCallback</b> instance. 290 * 291 * @since 6 292 */ 293 virtual ~BleAdvertiseCallback() = default; 294 295 /** 296 * @brief Start advertising result event callback. 297 * 298 * @param result Start advertising result. 299 * @since 6 300 */ 301 virtual void OnStartResultEvent(int result) = 0; 302 303 /** 304 * @brief Set advertising data result event callback. 305 * 306 * @param result Set advertising data result 307 * @since 6 308 */ 309 virtual void OnSetAdvDataEvent(int result) = 0; 310 }; 311 312 /** 313 * @brief Represents advertiser. 314 * 315 * @since 6 316 */ 317 class BLUETOOTH_API BleAdvertiser { 318 public: 319 /** 320 * @brief A constructor used to create a <b>BleAdvertiser</b> instance. 321 * 322 * @since 6 323 */ 324 explicit BleAdvertiser(); 325 326 /** 327 * @brief A destructor used to delete the <b>BleAdvertiser</b> instance. 328 * 329 * @since 6 330 */ 331 ~BleAdvertiser(); 332 333 /** 334 * @brief Start advertising. 335 * 336 * @param settings Advertise settings. 337 * @param advData Advertise data. 338 * @param scanResponse Scan response. 339 * @param callback Advertise callback. 340 * @since 6 341 */ 342 int StartAdvertising(const BleAdvertiserSettings &settings, const BleAdvertiserData &advData, 343 const BleAdvertiserData &scanResponse, BleAdvertiseCallback &callback); 344 345 /** 346 * @brief Start advertising. 347 * 348 * @param settings Advertise settings. 349 * @param advData Advertise data. 350 * @param scanResponse Scan response. 351 * @param callback Advertise callback. 352 * @since 6 353 */ 354 int StartAdvertising(const BleAdvertiserSettings &settings, const std::vector<uint8_t> &advData, 355 const std::vector<uint8_t> &scanResponse, BleAdvertiseCallback &callback); 356 357 void SetAdvertisingData(const std::vector<uint8_t> &advData, const std::vector<uint8_t> &scanResponse, 358 BleAdvertiseCallback &callback); 359 int StopAdvertising(BleAdvertiseCallback &callback); 360 361 /** 362 * @brief Cleans up advertisers. 363 * 364 * @param callback Advertise callback. 365 * @since 6 366 */ 367 void Close(BleAdvertiseCallback &callback); 368 369 /** 370 * @brief Get Advertise handle. 371 * 372 * @param callback Advertise callback. 373 * @since 6 374 */ 375 uint8_t GetAdvHandle(BleAdvertiseCallback &callback); 376 377 private: 378 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BleAdvertiser); 379 BLUETOOTH_DECLARE_IMPL(); 380 }; 381 } // namespace Bluetooth 382 } // namespace OHOS 383 #endif