Skip to content

Commit

Permalink
feat(NimBLEAdvertising): support std::function for advertising comple…
Browse files Browse the repository at this point in the history
…te 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.
  • Loading branch information
finger563 authored and h2zero committed Jun 15, 2024
1 parent 7f6959c commit 6934e87
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/NimBLEAdvertising.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ void NimBLEAdvertising::setScanResponseData(NimBLEAdvertisementData& advertiseme
* @param [in] dirAddr The address of a peer to directly advertise to.
* @return True if advertising started successfully.
*/
bool NimBLEAdvertising::start(uint32_t duration, void (*advCompleteCB)(NimBLEAdvertising *pAdv), NimBLEAddress* dirAddr) {
bool NimBLEAdvertising::start(uint32_t duration, advCompleteCB_t advCompleteCB, NimBLEAddress* dirAddr) {
NIMBLE_LOGD(LOG_TAG, ">> Advertising start: customAdvData: %d, customScanResponseData: %d",
m_customAdvData, m_customScanResponseData);

Expand Down
8 changes: 6 additions & 2 deletions src/NimBLEAdvertising.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "NimBLEUUID.h"
#include "NimBLEAddress.h"

#include <functional>
#include <vector>

/* COMPATIBILITY - DO NOT USE */
Expand All @@ -44,6 +45,9 @@
#define ESP_BLE_ADV_FLAG_NON_LIMIT_DISC (0x00 )
/* ************************* */

class NimBLEAdvertising;

typedef std::function<void(NimBLEAdvertising*)> advCompleteCB_t;

/**
* @brief Advertisement data set by the programmer to be published by the %BLE server.
Expand Down Expand Up @@ -93,7 +97,7 @@ class NimBLEAdvertising {
void addServiceUUID(const NimBLEUUID &serviceUUID);
void addServiceUUID(const char* serviceUUID);
void removeServiceUUID(const NimBLEUUID &serviceUUID);
bool start(uint32_t duration = 0, void (*advCompleteCB)(NimBLEAdvertising *pAdv) = nullptr, NimBLEAddress* dirAddr = nullptr);
bool start(uint32_t duration = 0, advCompleteCB_t advCompleteCB = nullptr, NimBLEAddress* dirAddr = nullptr);
bool stop();
void setAppearance(uint16_t appearance);
void setName(const std::string &name);
Expand Down Expand Up @@ -130,7 +134,7 @@ class NimBLEAdvertising {
bool m_customScanResponseData;
bool m_scanResp;
bool m_advDataSet;
void (*m_advCompCB)(NimBLEAdvertising *pAdv);
advCompleteCB_t m_advCompCB{nullptr};
uint8_t m_slaveItvl[4];
uint32_t m_duration;
std::vector<uint8_t> m_svcData16;
Expand Down

0 comments on commit 6934e87

Please sign in to comment.