Skip to content

Commit 6934e87

Browse files
finger563h2zero
authored andcommitted
feat(NimBLEAdvertising): support std::function for advertising complete callback (#157)
Updates the interface to use a typedef'd std::function for the advertise callback, which is backwards compatible but also allows std::bind and lambda functions.
1 parent 7f6959c commit 6934e87

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/NimBLEAdvertising.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ void NimBLEAdvertising::setScanResponseData(NimBLEAdvertisementData& advertiseme
400400
* @param [in] dirAddr The address of a peer to directly advertise to.
401401
* @return True if advertising started successfully.
402402
*/
403-
bool NimBLEAdvertising::start(uint32_t duration, void (*advCompleteCB)(NimBLEAdvertising *pAdv), NimBLEAddress* dirAddr) {
403+
bool NimBLEAdvertising::start(uint32_t duration, advCompleteCB_t advCompleteCB, NimBLEAddress* dirAddr) {
404404
NIMBLE_LOGD(LOG_TAG, ">> Advertising start: customAdvData: %d, customScanResponseData: %d",
405405
m_customAdvData, m_customScanResponseData);
406406

src/NimBLEAdvertising.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "NimBLEUUID.h"
3434
#include "NimBLEAddress.h"
3535

36+
#include <functional>
3637
#include <vector>
3738

3839
/* COMPATIBILITY - DO NOT USE */
@@ -44,6 +45,9 @@
4445
#define ESP_BLE_ADV_FLAG_NON_LIMIT_DISC (0x00 )
4546
/* ************************* */
4647

48+
class NimBLEAdvertising;
49+
50+
typedef std::function<void(NimBLEAdvertising*)> advCompleteCB_t;
4751

4852
/**
4953
* @brief Advertisement data set by the programmer to be published by the %BLE server.
@@ -93,7 +97,7 @@ class NimBLEAdvertising {
9397
void addServiceUUID(const NimBLEUUID &serviceUUID);
9498
void addServiceUUID(const char* serviceUUID);
9599
void removeServiceUUID(const NimBLEUUID &serviceUUID);
96-
bool start(uint32_t duration = 0, void (*advCompleteCB)(NimBLEAdvertising *pAdv) = nullptr, NimBLEAddress* dirAddr = nullptr);
100+
bool start(uint32_t duration = 0, advCompleteCB_t advCompleteCB = nullptr, NimBLEAddress* dirAddr = nullptr);
97101
bool stop();
98102
void setAppearance(uint16_t appearance);
99103
void setName(const std::string &name);
@@ -130,7 +134,7 @@ class NimBLEAdvertising {
130134
bool m_customScanResponseData;
131135
bool m_scanResp;
132136
bool m_advDataSet;
133-
void (*m_advCompCB)(NimBLEAdvertising *pAdv);
137+
advCompleteCB_t m_advCompCB{nullptr};
134138
uint8_t m_slaveItvl[4];
135139
uint32_t m_duration;
136140
std::vector<uint8_t> m_svcData16;

0 commit comments

Comments
 (0)