Compare commits

..

3 Commits
1.0.1 ... 1.0.2

Author SHA1 Message Date
h2zero
d9e11ee630 Release 1.0.2 2020-09-13 21:37:13 -06:00
h2zero
91b5916cf4 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.
2020-09-13 20:36:59 -06:00
h2zero
1a52245012 Update documentation.
* typo fix
2020-09-13 20:02:15 -06:00
8 changed files with 125 additions and 35 deletions

View File

@@ -2,6 +2,19 @@
All notable changes to this project will be documented in this file.
## [1.0.2] - 2020-09-13
### Changed
- `NimBLEAdvertising::start` Now takes 2 optional parameters, the first is the duration to advertise for (in seconds), the second is a
callback that is invoked when advertsing ends and takes a pointer to a `NimBLEAdvertising` object (similar to the `NimBLEScan::start` API).
- (Arduino) Maximum BLE connections can now be altered by only changing the value of `CONFIG_BT_NIMBLE_MAX_CONNECTIONS` in `nimconfig.h`.
Any changes to the controller max connection settings in `sdkconfig.h` will now have no effect when using this library.
- (Arduino) Revert the previous change to fix the advertising start delay. Instead a replacement fix that routes all BLE controller commands from
a task running on core 0 (same as the controller) has been implemented. This improves response times and reliability for all BLE functions.
## [1.0.1] - 2020-09-02
### Added

View File

@@ -38,7 +38,7 @@ PROJECT_NAME = "esp-nimble-cpp / NimBLE-Arduino"
# could be handy for archiving the generated documentation or if some version
# control system is used.
PROJECT_NUMBER = 1.0.0
PROJECT_NUMBER = 1.0.2
# Using the PROJECT_BRIEF tag one can provide an optional one line description
# for a project that appears at the top of each page and should give viewer a

View File

@@ -4,6 +4,7 @@ Many improvements have been made to this library vs the original, this is a brie
Refer to the [class documentation](https://h2zero.github.io/esp-nimble-cpp/annotated.html) for futher information on class specifics.
* [Server](#server)
* [Advertising](#advertising)
* [Client](#client)
* [General](#general)
<br/>
@@ -60,6 +61,16 @@ it's characteristics / descriptors will remain valid and the service can be re-a
using `NimBLEServer::addService`.
<br/>
<a name="advertising"></a>
# Advertising
`NimBLEAdvertising::start`
Now takes 2 optional parameters, the first is the duration to advertise for (in seconds), the second is a callback
that is invoked when advertsing ends and takes a pointer to a `NimBLEAdvertising` object (similar to the `NimBLEScan::start` API).
This provides an opportunity to update the advertisment data if desired.
<br/>
<a name="client"></a>
# Client

View File

@@ -209,12 +209,27 @@ This can be changed to use passkey authentication or numeric comparison. See [Se
<a name="advertising-api"></a>
## Advertising API
Advertising works the same as the original API except with the removal of:
Advertising works the same as the original API except:
> BLEAdvertising::setMinPreferred
> BLEAdvertising::setMaxPreferred
These methods were found to not provide useful functionality and consumed valuable advertising space (6 bytes of 31) if used unknowingly.
If you wish to advertise these parameters you can still do so manually via `NimBLEAdvertisementData::addData`.
If you wish to advertise these parameters you can still do so manually via `BLEAdvertisementData::addData` (`NimBLEAdvertisementData::addData`).
<br/>
Calling `NimBLEAdvertising::setAdvertisementData` will entirely replace any data set with `NimBLEAdvertising::addServiceUUID`, or
`NimBLEAdvertising::setAppearance`. You should set all the data you wish to advertise within the `NimBLEAdvertisementData` instead.
Calling `NimBLEAdvertising::setScanResponseData` without also calling `NimBLEAdvertising::setAdvertisementData` will have no effect.
When using custom scan response data you must also use custom advertisement data.
<br/>
> BLEAdvertising::start (NimBLEAdvertising::start)
Now takes 2 optional parameters, the first is the duration to advertise for (in seconds), the second is a callback
that is invoked when advertsing ends and takes a pointer to a `NimBLEAdvertising` object (similar to the `NimBLEScan::start` API).
This provides an opportunity to update the advertisment data if desired.
<br/>
<a name="client-api"></a>

View File

@@ -54,6 +54,11 @@ NimBLEAdvertising::NimBLEAdvertising() {
m_advParams.itvl_min = 0;
m_advParams.itvl_max = 0;
m_customAdvData = false;
m_customScanResponseData = false;
m_scanResp = true;
m_advDataSet = false;
} // NimBLEAdvertising
@@ -178,6 +183,9 @@ void NimBLEAdvertising::setScanFilter(bool scanRequestWhitelistOnly, bool connec
/**
* @brief Set the advertisement data that is to be published in a regular advertisement.
* @param [in] advertisementData The data to be advertised.
* @details The use of this function will replace any data set with addServiceUUID\n
* or setAppearance. If you wish for these to be advertised you must include them\n
* in the advertisementData parameter sent.
*/
void NimBLEAdvertising::setAdvertisementData(NimBLEAdvertisementData& advertisementData) {
@@ -196,6 +204,8 @@ void NimBLEAdvertising::setAdvertisementData(NimBLEAdvertisementData& advertisem
/**
* @brief Set the advertisement data that is to be published in a scan response.
* @param [in] advertisementData The data to be advertised.
* @details Calling this without also using setAdvertisementData will have no effect.\n
* When using custom scan response data you must also use custom advertisement data.
*/
void NimBLEAdvertising::setScanResponseData(NimBLEAdvertisementData& advertisementData) {
NIMBLE_LOGD(LOG_TAG, ">> setScanResponseData");
@@ -212,8 +222,10 @@ void NimBLEAdvertising::setScanResponseData(NimBLEAdvertisementData& advertiseme
/**
* @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);
// If Host is not synced we cannot start advertising.
@@ -227,11 +239,6 @@ void NimBLEAdvertising::start() {
if(pServer != nullptr) {
if(!pServer->m_gattsStarted){
pServer->start();
// When the server instance is created it resets GATT which
// seems to put the controller in a sleep loop? This causes a delay when
// advertising is started the first time. To avoid this we call ble_gap_adv_stop
// to get the controller ready.
ble_gap_adv_stop();
} else if(pServer->getConnectedCount() >= NIMBLE_MAX_CONNECTIONS) {
NIMBLE_LOGW(LOG_TAG, "Max connections reached - not advertising");
return;
@@ -244,6 +251,15 @@ void NimBLEAdvertising::start() {
return;
}
if(duration == 0){
duration = BLE_HS_FOREVER;
}
else{
duration = duration*1000; // convert duration to milliseconds
}
m_advCompCB = advCompleteCB;
int rc = 0;
if (!m_customAdvData && !m_advDataSet) {
@@ -389,13 +405,13 @@ void NimBLEAdvertising::start() {
}
#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,
(pServer != nullptr) ? NimBLEServer::handleGapEvent : NULL,
pServer);
(pServer != nullptr) ? NimBLEServer::handleGapEvent : NimBLEAdvertising::handleGapEvent,
(pServer != nullptr) ? (void*)pServer : (void*)this);
#else
rc = ble_gap_adv_start(0, NULL, BLE_HS_FOREVER,
&m_advParams, NULL,NULL);
rc = ble_gap_adv_start(0, NULL, duration,
&m_advParams, NimBLEAdvertising::handleGapEvent, this);
#endif
if (rc != 0) {
NIMBLE_LOGC(LOG_TAG, "Error enabling advertising; rc=%d, %s", rc, NimBLEUtils::returnCodeToString(rc));
@@ -421,6 +437,25 @@ void NimBLEAdvertising::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,
* 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.
* @param [in] data The data to be added to the payload.

View File

@@ -77,30 +77,34 @@ public:
void addServiceUUID(const NimBLEUUID &serviceUUID);
void addServiceUUID(const char* serviceUUID);
void removeServiceUUID(const NimBLEUUID &serviceUUID);
void start();
void start(uint32_t duration = 0, void (*advCompleteCB)(NimBLEAdvertising *pAdv) = nullptr);
void stop();
void setAppearance(uint16_t appearance);
void setAdvertisementType(uint8_t adv_type);
void setMaxInterval(uint16_t maxinterval);
void setMinInterval(uint16_t mininterval);
void setAdvertisementData(NimBLEAdvertisementData& advertisementData);
void setScanFilter(bool scanRequertWhitelistOnly, bool connectWhitelistOnly);
void setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly);
void setScanResponseData(NimBLEAdvertisementData& advertisementData);
void setScanResponse(bool);
void advCompleteCB();
bool isAdvertising();
private:
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_scanData;
ble_gap_adv_params m_advParams;
ble_hs_adv_fields m_advData;
ble_hs_adv_fields m_scanData;
ble_gap_adv_params m_advParams;
std::vector<NimBLEUUID> m_serviceUUIDs;
bool m_customAdvData = false; // Are we using custom advertising data?
bool m_customScanResponseData = false; // Are we using custom scan response data?
bool m_scanResp = true;
bool m_advDataSet = false;
bool m_customAdvData;
bool m_customScanResponseData;
bool m_scanResp;
bool m_advDataSet;
void (*m_advCompCB)(NimBLEAdvertising *pAdv);
};

View File

@@ -353,6 +353,12 @@ size_t NimBLEServer::getConnectedCount() {
return 0;
} // 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: {
NIMBLE_LOGD(LOG_TAG, "Connection parameters updated.");
return 0;

View File

@@ -101,17 +101,7 @@
*/
#define CONFIG_BT_NIMBLE_MEM_ALLOC_MODE_INTERNAL 1
/**
* @brief Sets the number of simultaneous connections (esp controller max is 9)
* @details To increase max connections in Arduino it is also required to change the
* controller max connections defined in sdkconfig.h.\n
*
* This is located in your Arduino/hardware/espressif/esp32/tools/sdk/include/config folder.\n\n
*
* The values in sdkconfig.h you will need to change are:\n\n
* `CONFIG_BTDM_CONTROLLER_BLE_MAX_CONN 3`\n
* `CONFIG_BTDM_CONTROLLER_BLE_MAX_CONN_EFF 3`
*/
/** @brief Sets the number of simultaneous connections (esp controller max is 9) */
#define CONFIG_BT_NIMBLE_MAX_CONNECTIONS 3
/** @brief Sets the number of devices allowed to store/bond with */