Skip to content

Commit 29b7e90

Browse files
authored
Add duration and callback parameter to NimBLEAdvertising::start
* Adds functionality to advertise for a set duration, similar to NimBLEScan::start. The first parameter being the duration (in seconds). The second parameter is a pointer to a callback function that is invoked when advertising stops. * NimBLEAdvertising::isAdvertising method added, returns true if advertising is currently active.
1 parent 19b8af5 commit 29b7e90

File tree

3 files changed

+76
-15
lines changed

3 files changed

+76
-15
lines changed

src/NimBLEAdvertising.cpp

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ NimBLEAdvertising::NimBLEAdvertising() {
5454
m_advParams.itvl_min = 0;
5555
m_advParams.itvl_max = 0;
5656

57+
m_customAdvData = false;
58+
m_customScanResponseData = false;
59+
m_scanResp = true;
60+
m_advDataSet = false;
61+
5762
} // NimBLEAdvertising
5863

5964

@@ -217,8 +222,10 @@ void NimBLEAdvertising::setScanResponseData(NimBLEAdvertisementData& advertiseme
217222

218223
/**
219224
* @brief Start advertising.
225+
* @param [in] duration The duration, in seconds, to advertise, 0 == advertise forever.
226+
* @param [in] advCompleteCB A pointer to a callback to be invoked when advertising ends.
220227
*/
221-
void NimBLEAdvertising::start() {
228+
void NimBLEAdvertising::start(uint32_t duration, void (*advCompleteCB)(NimBLEAdvertising *pAdv)) {
222229
NIMBLE_LOGD(LOG_TAG, ">> Advertising start: customAdvData: %d, customScanResponseData: %d", m_customAdvData, m_customScanResponseData);
223230

224231
// If Host is not synced we cannot start advertising.
@@ -244,6 +251,15 @@ void NimBLEAdvertising::start() {
244251
return;
245252
}
246253

254+
if(duration == 0){
255+
duration = BLE_HS_FOREVER;
256+
}
257+
else{
258+
duration = duration*1000; // convert duration to milliseconds
259+
}
260+
261+
m_advCompCB = advCompleteCB;
262+
247263
int rc = 0;
248264

249265
if (!m_customAdvData && !m_advDataSet) {
@@ -389,13 +405,13 @@ void NimBLEAdvertising::start() {
389405
}
390406

391407
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
392-
rc = ble_gap_adv_start(0, NULL, BLE_HS_FOREVER,
408+
rc = ble_gap_adv_start(0, NULL, duration,
393409
&m_advParams,
394-
(pServer != nullptr) ? NimBLEServer::handleGapEvent : NULL,
395-
pServer);
410+
(pServer != nullptr) ? NimBLEServer::handleGapEvent : NimBLEAdvertising::handleGapEvent,
411+
(pServer != nullptr) ? (void*)pServer : (void*)this);
396412
#else
397-
rc = ble_gap_adv_start(0, NULL, BLE_HS_FOREVER,
398-
&m_advParams, NULL,NULL);
413+
rc = ble_gap_adv_start(0, NULL, duration,
414+
&m_advParams, NimBLEAdvertising::handleGapEvent, this);
399415
#endif
400416
if (rc != 0) {
401417
NIMBLE_LOGC(LOG_TAG, "Error enabling advertising; rc=%d, %s", rc, NimBLEUtils::returnCodeToString(rc));
@@ -421,6 +437,25 @@ void NimBLEAdvertising::stop() {
421437
} // stop
422438

423439

440+
/**
441+
* @brief Handles the callback when advertising stops.
442+
*/
443+
void NimBLEAdvertising::advCompleteCB() {
444+
if(m_advCompCB != nullptr) {
445+
m_advCompCB(this);
446+
}
447+
}
448+
449+
450+
/**
451+
* @brief Check if currently advertising.
452+
* @return true if advertising is active.
453+
*/
454+
bool NimBLEAdvertising::isAdvertising() {
455+
return ble_gap_adv_active();
456+
}
457+
458+
424459
/*
425460
* Host reset seems to clear advertising data,
426461
* we need clear the flag so it reloads it.
@@ -430,6 +465,22 @@ void NimBLEAdvertising::onHostReset() {
430465
}
431466

432467

468+
/**
469+
* @brief Handler for gap events when not using peripheral role.
470+
* @param [in] event the event data.
471+
* @param [in] arg pointer to the advertising instance.
472+
*/
473+
/*STATIC*/
474+
int NimBLEAdvertising::handleGapEvent(struct ble_gap_event *event, void *arg) {
475+
NimBLEAdvertising *pAdv = (NimBLEAdvertising*)arg;
476+
477+
if(event->type == BLE_GAP_EVENT_ADV_COMPLETE) {
478+
pAdv->advCompleteCB();
479+
}
480+
return 0;
481+
}
482+
483+
433484
/**
434485
* @brief Add data to the payload to be advertised.
435486
* @param [in] data The data to be added to the payload.

src/NimBLEAdvertising.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class NimBLEAdvertising {
7777
void addServiceUUID(const NimBLEUUID &serviceUUID);
7878
void addServiceUUID(const char* serviceUUID);
7979
void removeServiceUUID(const NimBLEUUID &serviceUUID);
80-
void start();
80+
void start(uint32_t duration = 0, void (*advCompleteCB)(NimBLEAdvertising *pAdv) = nullptr);
8181
void stop();
8282
void setAppearance(uint16_t appearance);
8383
void setAdvertisementType(uint8_t adv_type);
@@ -87,20 +87,24 @@ class NimBLEAdvertising {
8787
void setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly);
8888
void setScanResponseData(NimBLEAdvertisementData& advertisementData);
8989
void setScanResponse(bool);
90+
void advCompleteCB();
91+
bool isAdvertising();
9092

9193
private:
9294
friend class NimBLEDevice;
9395

94-
void onHostReset();
96+
void onHostReset();
97+
static int handleGapEvent(struct ble_gap_event *event, void *arg);
9598

96-
ble_hs_adv_fields m_advData;
97-
ble_hs_adv_fields m_scanData;
98-
ble_gap_adv_params m_advParams;
99+
ble_hs_adv_fields m_advData;
100+
ble_hs_adv_fields m_scanData;
101+
ble_gap_adv_params m_advParams;
99102
std::vector<NimBLEUUID> m_serviceUUIDs;
100-
bool m_customAdvData = false; // Are we using custom advertising data?
101-
bool m_customScanResponseData = false; // Are we using custom scan response data?
102-
bool m_scanResp = true;
103-
bool m_advDataSet = false;
103+
bool m_customAdvData;
104+
bool m_customScanResponseData;
105+
bool m_scanResp;
106+
bool m_advDataSet;
107+
void (*m_advCompCB)(NimBLEAdvertising *pAdv);
104108

105109
};
106110

src/NimBLEServer.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,12 @@ size_t NimBLEServer::getConnectedCount() {
353353
return 0;
354354
} // BLE_GAP_EVENT_NOTIFY_TX
355355

356+
case BLE_GAP_EVENT_ADV_COMPLETE: {
357+
NIMBLE_LOGD(LOG_TAG, "Advertising Complete");
358+
NimBLEDevice::getAdvertising()->advCompleteCB();
359+
return 0;
360+
}
361+
356362
case BLE_GAP_EVENT_CONN_UPDATE: {
357363
NIMBLE_LOGD(LOG_TAG, "Connection parameters updated.");
358364
return 0;

0 commit comments

Comments
 (0)