mirror of
https://github.com/h2zero/esp-nimble-cpp.git
synced 2024-11-21 20:50:55 +01:00
Add new getServer() method.
Previously we used createServer() to get a reference to the server instance. This was problematic when using advertising only as it would create a server when starting advertising. This prevents that and provides better semantics.
This commit is contained in:
parent
03cb7b21d9
commit
fc1022a46d
3 changed files with 22 additions and 8 deletions
|
@ -193,12 +193,14 @@ void NimBLEAdvertising::start() {
|
|||
}
|
||||
|
||||
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
||||
NimBLEServer* pServer = NimBLEDevice::createServer();
|
||||
if(!pServer->m_gattsStarted){
|
||||
pServer->start();
|
||||
} else if(pServer->getConnectedCount() >= NIMBLE_MAX_CONNECTIONS) {
|
||||
NIMBLE_LOGW(LOG_TAG, "Max connections reached - not advertising");
|
||||
return;
|
||||
NimBLEServer* pServer = NimBLEDevice::getServer();
|
||||
if(pServer != nullptr) {
|
||||
if(!pServer->m_gattsStarted){
|
||||
pServer->start();
|
||||
} else if(pServer->getConnectedCount() >= NIMBLE_MAX_CONNECTIONS) {
|
||||
NIMBLE_LOGW(LOG_TAG, "Max connections reached - not advertising");
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -358,10 +360,12 @@ void NimBLEAdvertising::start() {
|
|||
|
||||
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
||||
rc = ble_gap_adv_start(addressType, NULL, BLE_HS_FOREVER,
|
||||
&m_advParams, NimBLEServer::handleGapEvent, NimBLEDevice::createServer()); //get a reference to the server (does not create a new one)
|
||||
&m_advParams,
|
||||
(pServer != nullptr) ? NimBLEServer::handleGapEvent : NULL,
|
||||
pServer);
|
||||
#else
|
||||
rc = ble_gap_adv_start(addressType, NULL, BLE_HS_FOREVER,
|
||||
&m_advParams, NULL,NULL);
|
||||
&m_advParams, NULL,NULL);
|
||||
#endif
|
||||
if (rc != 0) {
|
||||
NIMBLE_LOGC(LOG_TAG, "Error enabling advertising; rc=%d, %s", rc, NimBLEUtils::returnCodeToString(rc));
|
||||
|
|
|
@ -77,6 +77,15 @@ NimBLESecurityCallbacks* NimBLEDevice::m_securityCallbacks = nullptr;
|
|||
|
||||
return m_pServer;
|
||||
} // createServer
|
||||
|
||||
|
||||
/**
|
||||
* @brief Get the instance of the server.
|
||||
* @return A pointer to the server instance.
|
||||
*/
|
||||
/* STATIC */ NimBLEServer* NimBLEDevice::getServer() {
|
||||
return m_pServer;
|
||||
} // getServer
|
||||
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
||||
|
||||
|
||||
|
|
|
@ -102,6 +102,7 @@ public:
|
|||
|
||||
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
||||
static NimBLEServer* createServer();
|
||||
static NimBLEServer* getServer();
|
||||
#endif
|
||||
|
||||
static void setPower(esp_power_level_t powerLevel, esp_ble_power_type_t powerType=ESP_BLE_PWR_TYPE_DEFAULT);
|
||||
|
|
Loading…
Reference in a new issue