NimBLE-Arduino 2.1.2
Loading...
Searching...
No Matches
NimBLEAdvertising.h
1/*
2 * Copyright 2020-2024 Ryan Powell <ryan@nable-embedded.io> and
3 * esp-nimble-cpp, NimBLE-Arduino contributors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef NIMBLE_CPP_ADVERTISING_H_
19#define NIMBLE_CPP_ADVERTISING_H_
20
21#include "nimconfig.h"
22#if (defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER) && !CONFIG_BT_NIMBLE_EXT_ADV) || \
23 defined(_DOXYGEN_)
24
25# if defined(CONFIG_NIMBLE_CPP_IDF)
26# include "host/ble_gap.h"
27# else
28# include "nimble/nimble/host/include/host/ble_gap.h"
29# endif
30
31/**** FIX COMPILATION ****/
32# undef min
33# undef max
34/**************************/
35
36# include "NimBLEUUID.h"
37# include "NimBLEAddress.h"
38# include "NimBLEAdvertisementData.h"
39
40# include <functional>
41# include <string>
42# include <vector>
43
45typedef std::function<void(NimBLEAdvertising*)> advCompleteCB_t;
46
53 public:
55 bool start(uint32_t duration = 0, const NimBLEAddress* dirAddr = nullptr);
56 void setAdvertisingCompleteCallback(advCompleteCB_t callback);
57 bool stop();
58 bool setConnectableMode(uint8_t mode);
59 bool setDiscoverableMode(uint8_t mode);
60 bool reset();
61 bool isAdvertising();
62 void setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly);
63 void enableScanResponse(bool enable);
64 void setAdvertisingInterval(uint16_t interval);
65 void setMaxInterval(uint16_t maxInterval);
66 void setMinInterval(uint16_t minInterval);
67
68 bool setAdvertisementData(const NimBLEAdvertisementData& advertisementData);
69 bool setScanResponseData(const NimBLEAdvertisementData& advertisementData);
72 void clearData();
74
75 bool addServiceUUID(const NimBLEUUID& serviceUUID);
76 bool addServiceUUID(const char* serviceUUID);
77 bool removeServiceUUID(const NimBLEUUID& serviceUUID);
78 bool removeServiceUUID(const char* serviceUUID);
79 bool removeServices();
80 bool setAppearance(uint16_t appearance);
81 bool setPreferredParams(uint16_t minInterval, uint16_t maxInterval);
82 bool addTxPower();
83 bool setName(const std::string& name);
84 bool setManufacturerData(const uint8_t* data, size_t length);
85 bool setManufacturerData(const std::string& data);
86 bool setManufacturerData(const std::vector<uint8_t>& data);
87 bool setURI(const std::string& uri);
88 bool setServiceData(const NimBLEUUID& uuid, const uint8_t* data, size_t length);
89 bool setServiceData(const NimBLEUUID& uuid, const std::string& data);
90 bool setServiceData(const NimBLEUUID& uuid, const std::vector<uint8_t>& data);
91
92 private:
93 friend class NimBLEDevice;
94 friend class NimBLEServer;
95
96 void onHostSync();
97 static int handleGapEvent(ble_gap_event* event, void* arg);
98
100 NimBLEAdvertisementData m_scanData;
101 ble_gap_adv_params m_advParams;
102 advCompleteCB_t m_advCompCb;
103 uint8_t m_slaveItvl[4];
104 uint32_t m_duration;
105 bool m_scanResp : 1;
106 bool m_advDataSet : 1;
107};
108
109#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_BROADCASTER && !CONFIG_BT_NIMBLE_EXT_ADV */
110#endif /* NIMBLE_CPP_ADVERTISING_H_ */
A BLE device address.
Definition NimBLEAddress.h:41
Advertisement data set by the programmer to be published by the BLE server.
Definition NimBLEAdvertisementData.h:33
Perform and manage BLE advertising.
Definition NimBLEAdvertising.h:52
bool addTxPower()
Add the transmission power level to the advertisement packet.
Definition NimBLEAdvertising.cpp:501
const NimBLEAdvertisementData & getAdvertisementData()
Get the current advertisement data.
Definition NimBLEAdvertising.cpp:348
bool setAppearance(uint16_t appearance)
Set the device appearance in the advertising data.
Definition NimBLEAdvertising.cpp:468
void setAdvertisingInterval(uint16_t interval)
Set the advertising interval.
Definition NimBLEAdvertising.cpp:123
bool stop()
Stop advertising.
Definition NimBLEAdvertising.cpp:249
void setAdvertisingCompleteCallback(advCompleteCB_t callback)
Set the callback to be invoked when advertising stops.
Definition NimBLEAdvertising.cpp:263
bool addServiceUUID(const NimBLEUUID &serviceUUID)
Add a service uuid to exposed list of services.
Definition NimBLEAdvertising.cpp:410
void setMinInterval(uint16_t minInterval)
Set the minimum advertising interval.
Definition NimBLEAdvertising.cpp:132
void enableScanResponse(bool enable)
Enable scan response data.
Definition NimBLEAdvertising.cpp:149
bool setDiscoverableMode(uint8_t mode)
Set the discoverable mode to use.
Definition NimBLEAdvertising.cpp:99
bool setName(const std::string &name)
Set the advertised name of the device.
Definition NimBLEAdvertising.cpp:519
bool setServiceData(const NimBLEUUID &uuid, const uint8_t *data, size_t length)
Set the service data advertised for the UUID.
Definition NimBLEAdvertising.cpp:592
bool start(uint32_t duration=0, const NimBLEAddress *dirAddr=nullptr)
Start advertising.
Definition NimBLEAdvertising.cpp:184
bool reset()
Stops the current advertising and resets the advertising data to the default values.
Definition NimBLEAdvertising.cpp:60
bool setScanResponseData(const NimBLEAdvertisementData &advertisementData)
Set the data that is to be provided in a scan response.
Definition NimBLEAdvertising.cpp:359
void setMaxInterval(uint16_t maxInterval)
Set the maximum advertising interval.
Definition NimBLEAdvertising.cpp:140
bool setPreferredParams(uint16_t minInterval, uint16_t maxInterval)
Set the preferred min and max connection intervals to advertise.
Definition NimBLEAdvertising.cpp:486
bool setURI(const std::string &uri)
Set the advertised URI.
Definition NimBLEAdvertising.cpp:573
void setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly)
Set the filtering for the scan filter.
Definition NimBLEAdvertising.cpp:159
bool setAdvertisementData(const NimBLEAdvertisementData &advertisementData)
Set the advertisement data that is to be broadcast in a regular advertisement.
Definition NimBLEAdvertising.cpp:331
bool removeServiceUUID(const NimBLEUUID &serviceUUID)
Remove a service UUID from the advertisement.
Definition NimBLEAdvertising.cpp:435
NimBLEAdvertising()
Construct a default advertising object.
Definition NimBLEAdvertising.cpp:38
bool isAdvertising()
Check if currently advertising.
Definition NimBLEAdvertising.cpp:271
bool setManufacturerData(const uint8_t *data, size_t length)
Set the advertised manufacturer data.
Definition NimBLEAdvertising.cpp:539
bool removeServices()
Remove all service UUIDs from the advertisement.
Definition NimBLEAdvertising.cpp:455
bool refreshAdvertisingData()
Refresh advertsing data dynamically without stop/start cycle. For instance allows refreshing manufact...
Definition NimBLEAdvertising.cpp:396
const NimBLEAdvertisementData & getScanData()
Get the current scan response data.
Definition NimBLEAdvertising.cpp:375
bool setConnectableMode(uint8_t mode)
Set the type of connectable mode to advertise.
Definition NimBLEAdvertising.cpp:77
void clearData()
Clear the advertisement and scan response data and set the flags to BLE_HS_ADV_F_DISC_GEN.
Definition NimBLEAdvertising.cpp:382
A model of a BLE Device from which all the BLE roles are created.
Definition NimBLEDevice.h:109
The model of a BLE server.
Definition NimBLEServer.h:60
A model of a BLE UUID.
Definition NimBLEUUID.h:41
Definition ble_gap.h:220
Definition ble_gap.h:510