esp-nimble-cpp  1.4.1
NimBLEAdvertising Class Reference

Perform and manage BLE advertising. More...

Public Member Functions

 NimBLEAdvertising ()
 Construct a default advertising object.
 
void addServiceUUID (const NimBLEUUID &serviceUUID)
 Add a service uuid to exposed list of services. More...
 
void addServiceUUID (const char *serviceUUID)
 Add a service uuid to exposed list of services. More...
 
void removeServiceUUID (const NimBLEUUID &serviceUUID)
 Add a service uuid to exposed list of services. More...
 
bool start (uint32_t duration=0, void(*advCompleteCB)(NimBLEAdvertising *pAdv)=nullptr)
 Start advertising. More...
 
bool stop ()
 Stop advertising. More...
 
void setAppearance (uint16_t appearance)
 Set the device appearance in the advertising data. The codes for distinct appearances can be found here:
https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.gap.appearance.xml. More...
 
void setName (const std::string &name)
 Set the advertised name of the device. More...
 
void setManufacturerData (const std::string &data)
 Set the advertised manufacturer data. More...
 
void setURI (const std::string &uri)
 Set the advertised URI. More...
 
void setServiceData (const NimBLEUUID &uuid, const std::string &data)
 Set the service data advertised for the UUID. More...
 
void setAdvertisementType (uint8_t adv_type)
 Set the type of advertisment to use. More...
 
void setMaxInterval (uint16_t maxinterval)
 Set the maximum advertising interval. More...
 
void setMinInterval (uint16_t mininterval)
 Set the minimum advertising interval. More...
 
void setAdvertisementData (NimBLEAdvertisementData &advertisementData)
 Set the advertisement data that is to be published in a regular advertisement. More...
 
void setScanFilter (bool scanRequestWhitelistOnly, bool connectWhitelistOnly)
 Set the filtering for the scan filter. More...
 
void setScanResponseData (NimBLEAdvertisementData &advertisementData)
 Set the advertisement data that is to be published in a scan response. More...
 
void setScanResponse (bool)
 Set if scan response is available. More...
 
void setMinPreferred (uint16_t)
 Set the advertised min connection interval preferred by this device. More...
 
void setMaxPreferred (uint16_t)
 Set the advertised max connection interval preferred by this device. More...
 
void addTxPower ()
 Add the transmission power level to the advertisement packet.
 
void reset ()
 Stops the current advertising and resets the advertising data to the default values.
 
void advCompleteCB ()
 Handles the callback when advertising stops.
 
bool isAdvertising ()
 Check if currently advertising. More...
 

Detailed Description

Perform and manage BLE advertising.

A BLE server will want to perform advertising in order to make itself known to BLE clients.

Member Function Documentation

◆ addServiceUUID() [1/2]

void NimBLEAdvertising::addServiceUUID ( const char *  serviceUUID)

Add a service uuid to exposed list of services.

Parameters
[in]serviceUUIDThe string representation of the service to expose.

◆ addServiceUUID() [2/2]

void NimBLEAdvertising::addServiceUUID ( const NimBLEUUID serviceUUID)

Add a service uuid to exposed list of services.

Parameters
[in]serviceUUIDThe UUID of the service to expose.

◆ isAdvertising()

bool NimBLEAdvertising::isAdvertising ( )

Check if currently advertising.

Returns
true if advertising is active.

◆ removeServiceUUID()

void NimBLEAdvertising::removeServiceUUID ( const NimBLEUUID serviceUUID)

Add a service uuid to exposed list of services.

Parameters
[in]serviceUUIDThe UUID of the service to expose.

◆ setAdvertisementData()

void NimBLEAdvertising::setAdvertisementData ( NimBLEAdvertisementData advertisementData)

Set the advertisement data that is to be published in a regular advertisement.

Parameters
[in]advertisementDataThe data to be advertised.

The use of this function will replace any data set with addServiceUUID
or setAppearance. If you wish for these to be advertised you must include them
in the advertisementData parameter sent.

◆ setAdvertisementType()

void NimBLEAdvertising::setAdvertisementType ( uint8_t  adv_type)

Set the type of advertisment to use.

Parameters
[in]adv_type
  • BLE_GAP_CONN_MODE_NON (0) - not connectable advertising
  • BLE_GAP_CONN_MODE_DIR (1) - directed connectable advertising
  • BLE_GAP_CONN_MODE_UND (2) - undirected connectable advertising

◆ setAppearance()

void NimBLEAdvertising::setAppearance ( uint16_t  appearance)

Set the device appearance in the advertising data. The codes for distinct appearances can be found here:
https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.gap.appearance.xml.

Parameters
[in]appearanceThe appearance of the device in the advertising data.

◆ setManufacturerData()

void NimBLEAdvertising::setManufacturerData ( const std::string &  data)

Set the advertised manufacturer data.

Parameters
[in]dataThe data to advertise.

◆ setMaxInterval()

void NimBLEAdvertising::setMaxInterval ( uint16_t  maxinterval)

Set the maximum advertising interval.

Parameters
[in]maxintervalMaximum value for advertising interval in 0.625ms units, 0 = use default.

◆ setMaxPreferred()

void NimBLEAdvertising::setMaxPreferred ( uint16_t  maxinterval)

Set the advertised max connection interval preferred by this device.

Parameters
[in]maxintervalthe max interval value. Range = 0x0006 to 0x0C80.

Values not within the range will cancel advertising of this data.
Consumes 6 bytes of advertising space (combined with min interval).

◆ setMinInterval()

void NimBLEAdvertising::setMinInterval ( uint16_t  mininterval)

Set the minimum advertising interval.

Parameters
[in]minintervalMinimum value for advertising interval in 0.625ms units, 0 = use default.

◆ setMinPreferred()

void NimBLEAdvertising::setMinPreferred ( uint16_t  mininterval)

Set the advertised min connection interval preferred by this device.

Parameters
[in]minintervalthe max interval value. Range = 0x0006 to 0x0C80.

Values not within the range will cancel advertising of this data.
Consumes 6 bytes of advertising space (combined with max interval).

◆ setName()

void NimBLEAdvertising::setName ( const std::string &  name)

Set the advertised name of the device.

Parameters
[in]nameThe name to advertise.

◆ setScanFilter()

void NimBLEAdvertising::setScanFilter ( bool  scanRequestWhitelistOnly,
bool  connectWhitelistOnly 
)

Set the filtering for the scan filter.

Parameters
[in]scanRequestWhitelistOnlyIf true, only allow scan requests from those on the white list.
[in]connectWhitelistOnlyIf true, only allow connections from those on the white list.

◆ setScanResponse()

void NimBLEAdvertising::setScanResponse ( bool  set)

Set if scan response is available.

Parameters
[in]settrue = scan response available.

◆ setScanResponseData()

void NimBLEAdvertising::setScanResponseData ( NimBLEAdvertisementData advertisementData)

Set the advertisement data that is to be published in a scan response.

Parameters
[in]advertisementDataThe data to be advertised.

Calling this without also using setAdvertisementData will have no effect.
When using custom scan response data you must also use custom advertisement data.

◆ setServiceData()

void NimBLEAdvertising::setServiceData ( const NimBLEUUID uuid,
const std::string &  data 
)

Set the service data advertised for the UUID.

Parameters
[in]uuidThe UUID the service data belongs to.
[in]dataThe data to advertise.
Note
If data length is 0 the service data will not be advertised.

◆ setURI()

void NimBLEAdvertising::setURI ( const std::string &  uri)

Set the advertised URI.

Parameters
[in]uriThe URI to advertise.

◆ start()

bool NimBLEAdvertising::start ( uint32_t  duration = 0,
void(*)(NimBLEAdvertising *pAdv)  advCompleteCB = nullptr 
)

Start advertising.

Parameters
[in]durationThe duration, in seconds, to advertise, 0 == advertise forever.
[in]advCompleteCBA pointer to a callback to be invoked when advertising ends.
Returns
True if advertising started successfully.

◆ stop()

bool NimBLEAdvertising::stop ( )

Stop advertising.

Returns
True if advertising stopped successfully.