mirror of
https://github.com/h2zero/esp-nimble-cpp.git
synced 2024-11-22 13:10:55 +01:00
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.
This commit is contained in:
parent
1a52245012
commit
91b5916cf4
3 changed files with 76 additions and 15 deletions
|
@ -54,6 +54,11 @@ NimBLEAdvertising::NimBLEAdvertising() {
|
||||||
m_advParams.itvl_min = 0;
|
m_advParams.itvl_min = 0;
|
||||||
m_advParams.itvl_max = 0;
|
m_advParams.itvl_max = 0;
|
||||||
|
|
||||||
|
m_customAdvData = false;
|
||||||
|
m_customScanResponseData = false;
|
||||||
|
m_scanResp = true;
|
||||||
|
m_advDataSet = false;
|
||||||
|
|
||||||
} // NimBLEAdvertising
|
} // NimBLEAdvertising
|
||||||
|
|
||||||
|
|
||||||
|
@ -217,8 +222,10 @@ void NimBLEAdvertising::setScanResponseData(NimBLEAdvertisementData& advertiseme
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Start advertising.
|
* @brief Start advertising.
|
||||||
|
* @param [in] duration The duration, in seconds, to advertise, 0 == advertise forever.
|
||||||
|
* @param [in] advCompleteCB A pointer to a callback to be invoked when advertising ends.
|
||||||
*/
|
*/
|
||||||
void NimBLEAdvertising::start() {
|
void NimBLEAdvertising::start(uint32_t duration, void (*advCompleteCB)(NimBLEAdvertising *pAdv)) {
|
||||||
NIMBLE_LOGD(LOG_TAG, ">> Advertising start: customAdvData: %d, customScanResponseData: %d", m_customAdvData, m_customScanResponseData);
|
NIMBLE_LOGD(LOG_TAG, ">> Advertising start: customAdvData: %d, customScanResponseData: %d", m_customAdvData, m_customScanResponseData);
|
||||||
|
|
||||||
// If Host is not synced we cannot start advertising.
|
// If Host is not synced we cannot start advertising.
|
||||||
|
@ -244,6 +251,15 @@ void NimBLEAdvertising::start() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(duration == 0){
|
||||||
|
duration = BLE_HS_FOREVER;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
duration = duration*1000; // convert duration to milliseconds
|
||||||
|
}
|
||||||
|
|
||||||
|
m_advCompCB = advCompleteCB;
|
||||||
|
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
|
|
||||||
if (!m_customAdvData && !m_advDataSet) {
|
if (!m_customAdvData && !m_advDataSet) {
|
||||||
|
@ -389,13 +405,13 @@ void NimBLEAdvertising::start() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
||||||
rc = ble_gap_adv_start(0, NULL, BLE_HS_FOREVER,
|
rc = ble_gap_adv_start(0, NULL, duration,
|
||||||
&m_advParams,
|
&m_advParams,
|
||||||
(pServer != nullptr) ? NimBLEServer::handleGapEvent : NULL,
|
(pServer != nullptr) ? NimBLEServer::handleGapEvent : NimBLEAdvertising::handleGapEvent,
|
||||||
pServer);
|
(pServer != nullptr) ? (void*)pServer : (void*)this);
|
||||||
#else
|
#else
|
||||||
rc = ble_gap_adv_start(0, NULL, BLE_HS_FOREVER,
|
rc = ble_gap_adv_start(0, NULL, duration,
|
||||||
&m_advParams, NULL,NULL);
|
&m_advParams, NimBLEAdvertising::handleGapEvent, this);
|
||||||
#endif
|
#endif
|
||||||
if (rc != 0) {
|
if (rc != 0) {
|
||||||
NIMBLE_LOGC(LOG_TAG, "Error enabling advertising; rc=%d, %s", rc, NimBLEUtils::returnCodeToString(rc));
|
NIMBLE_LOGC(LOG_TAG, "Error enabling advertising; rc=%d, %s", rc, NimBLEUtils::returnCodeToString(rc));
|
||||||
|
@ -421,6 +437,25 @@ void NimBLEAdvertising::stop() {
|
||||||
} // stop
|
} // stop
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Handles the callback when advertising stops.
|
||||||
|
*/
|
||||||
|
void NimBLEAdvertising::advCompleteCB() {
|
||||||
|
if(m_advCompCB != nullptr) {
|
||||||
|
m_advCompCB(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Check if currently advertising.
|
||||||
|
* @return true if advertising is active.
|
||||||
|
*/
|
||||||
|
bool NimBLEAdvertising::isAdvertising() {
|
||||||
|
return ble_gap_adv_active();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Host reset seems to clear advertising data,
|
* Host reset seems to clear advertising data,
|
||||||
* we need clear the flag so it reloads it.
|
* we need clear the flag so it reloads it.
|
||||||
|
@ -430,6 +465,22 @@ void NimBLEAdvertising::onHostReset() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Handler for gap events when not using peripheral role.
|
||||||
|
* @param [in] event the event data.
|
||||||
|
* @param [in] arg pointer to the advertising instance.
|
||||||
|
*/
|
||||||
|
/*STATIC*/
|
||||||
|
int NimBLEAdvertising::handleGapEvent(struct ble_gap_event *event, void *arg) {
|
||||||
|
NimBLEAdvertising *pAdv = (NimBLEAdvertising*)arg;
|
||||||
|
|
||||||
|
if(event->type == BLE_GAP_EVENT_ADV_COMPLETE) {
|
||||||
|
pAdv->advCompleteCB();
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Add data to the payload to be advertised.
|
* @brief Add data to the payload to be advertised.
|
||||||
* @param [in] data The data to be added to the payload.
|
* @param [in] data The data to be added to the payload.
|
||||||
|
|
|
@ -77,7 +77,7 @@ public:
|
||||||
void addServiceUUID(const NimBLEUUID &serviceUUID);
|
void addServiceUUID(const NimBLEUUID &serviceUUID);
|
||||||
void addServiceUUID(const char* serviceUUID);
|
void addServiceUUID(const char* serviceUUID);
|
||||||
void removeServiceUUID(const NimBLEUUID &serviceUUID);
|
void removeServiceUUID(const NimBLEUUID &serviceUUID);
|
||||||
void start();
|
void start(uint32_t duration = 0, void (*advCompleteCB)(NimBLEAdvertising *pAdv) = nullptr);
|
||||||
void stop();
|
void stop();
|
||||||
void setAppearance(uint16_t appearance);
|
void setAppearance(uint16_t appearance);
|
||||||
void setAdvertisementType(uint8_t adv_type);
|
void setAdvertisementType(uint8_t adv_type);
|
||||||
|
@ -87,20 +87,24 @@ public:
|
||||||
void setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly);
|
void setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly);
|
||||||
void setScanResponseData(NimBLEAdvertisementData& advertisementData);
|
void setScanResponseData(NimBLEAdvertisementData& advertisementData);
|
||||||
void setScanResponse(bool);
|
void setScanResponse(bool);
|
||||||
|
void advCompleteCB();
|
||||||
|
bool isAdvertising();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class NimBLEDevice;
|
friend class NimBLEDevice;
|
||||||
|
|
||||||
void onHostReset();
|
void onHostReset();
|
||||||
|
static int handleGapEvent(struct ble_gap_event *event, void *arg);
|
||||||
|
|
||||||
ble_hs_adv_fields m_advData;
|
ble_hs_adv_fields m_advData;
|
||||||
ble_hs_adv_fields m_scanData;
|
ble_hs_adv_fields m_scanData;
|
||||||
ble_gap_adv_params m_advParams;
|
ble_gap_adv_params m_advParams;
|
||||||
std::vector<NimBLEUUID> m_serviceUUIDs;
|
std::vector<NimBLEUUID> m_serviceUUIDs;
|
||||||
bool m_customAdvData = false; // Are we using custom advertising data?
|
bool m_customAdvData;
|
||||||
bool m_customScanResponseData = false; // Are we using custom scan response data?
|
bool m_customScanResponseData;
|
||||||
bool m_scanResp = true;
|
bool m_scanResp;
|
||||||
bool m_advDataSet = false;
|
bool m_advDataSet;
|
||||||
|
void (*m_advCompCB)(NimBLEAdvertising *pAdv);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -353,6 +353,12 @@ size_t NimBLEServer::getConnectedCount() {
|
||||||
return 0;
|
return 0;
|
||||||
} // BLE_GAP_EVENT_NOTIFY_TX
|
} // BLE_GAP_EVENT_NOTIFY_TX
|
||||||
|
|
||||||
|
case BLE_GAP_EVENT_ADV_COMPLETE: {
|
||||||
|
NIMBLE_LOGD(LOG_TAG, "Advertising Complete");
|
||||||
|
NimBLEDevice::getAdvertising()->advCompleteCB();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
case BLE_GAP_EVENT_CONN_UPDATE: {
|
case BLE_GAP_EVENT_CONN_UPDATE: {
|
||||||
NIMBLE_LOGD(LOG_TAG, "Connection parameters updated.");
|
NIMBLE_LOGD(LOG_TAG, "Connection parameters updated.");
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in a new issue