Skip to content

Commit f8f5ff8

Browse files
committed
Fix advertising data after host reset.
Update examples.
1 parent ab3cabf commit f8f5ff8

File tree

6 files changed

+43
-78
lines changed

6 files changed

+43
-78
lines changed

examples/NimBLE_Client/NimBLE_Client.ino

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ class ClientCallbacks : public NimBLEClientCallbacks {
2727
* These settings are 150ms interval, 0 latency, 450ms timout.
2828
* Timeout should be a multiple of the interval, minimum is 100ms.
2929
* I find a multiple of 3-5 * the interval works best for quick response/reconnect.
30-
* Min interval: 120 * 1.25ms = 150, Max interval: 120 * 1.25ms = 150, 0 latency, 45 * 10ms = 450ms timeout
30+
* Min interval: 120 * 1.25ms = 150, Max interval: 120 * 1.25ms = 150, 0 latency, 60 * 10ms = 600ms timeout
3131
*/
32-
pClient->updateConnParams(120,120,0,45);
32+
pClient->updateConnParams(120,120,0,60);
3333
};
3434

3535
void onDisconnect(NimBLEClient* pClient) {
@@ -38,19 +38,23 @@ class ClientCallbacks : public NimBLEClientCallbacks {
3838
NimBLEDevice::getScan()->start(scanTime, scanEndedCB);
3939
};
4040

41-
bool onConnParamsUpdateRequest(NimBLEClient* pClient, const ble_gap_upd_params* params) {
42-
if(params->itvl_min < 24) {
43-
return false;
44-
} else if(params->itvl_max > 40) {
45-
return false;
46-
} else if(params->latency > 2) {
47-
return false;
48-
} else if(params->supervision_timeout > 100) {
49-
return false;
50-
}
41+
/** Called when the peripheral requests a change to the connection parameters.
42+
* Return true to accept and apply them or false to reject and keep
43+
* the currently used parameters. Default will return true.
44+
*/
45+
bool onConnParamsUpdateRequest(NimBLEClient* pClient, const ble_gap_upd_params* params) {
46+
if(params->itvl_min < 24) { /** 1.25ms units */
47+
return false;
48+
} else if(params->itvl_max > 40) { /** 1.25ms units */
49+
return false;
50+
} else if(params->latency > 2) { /** Number of intervals allowed to skip */
51+
return false;
52+
} else if(params->supervision_timeout > 100) { /** 10ms units */
53+
return false;
54+
}
5155

52-
return true;
53-
};
56+
return true;
57+
};
5458

5559
/********************* Security handled here **********************
5660
****** Note: these are the same return values as defaults ********/
@@ -161,9 +165,9 @@ bool connectToServer() {
161165
/** Set initial connection parameters: These settings are 15ms interval, 0 latency, 120ms timout.
162166
* These settings are safe for 3 clients to connect reliably, can go faster if you have less
163167
* connections. Timeout should be a multiple of the interval, minimum is 100ms.
164-
* Min interval: 12 * 1.25ms = 15, Max interval: 12 * 1.25ms = 15, 0 latency, 12 * 10ms = 120ms timeout
168+
* Min interval: 12 * 1.25ms = 15, Max interval: 12 * 1.25ms = 15, 0 latency, 51 * 10ms = 510ms timeout
165169
*/
166-
pClient->setConnectionParams(12,12,0,12);
170+
pClient->setConnectionParams(12,12,0,51);
167171
/** Set how long we are willing to wait for the connection to complete (seconds), default is 30. */
168172
pClient->setConnectTimeout(5);
169173

@@ -333,8 +337,8 @@ void setup (){
333337
//NimBLEDevice::setSecurityAuth(false, false, true);
334338
NimBLEDevice::setSecurityAuth(/*BLE_SM_PAIR_AUTHREQ_BOND | BLE_SM_PAIR_AUTHREQ_MITM |*/ BLE_SM_PAIR_AUTHREQ_SC);
335339

336-
/** Optional: set the transmit power, default is -3db */
337-
NimBLEDevice::setPower(ESP_PWR_LVL_P9); /** 12db */
340+
/** Optional: set the transmit power, default is 3db */
341+
NimBLEDevice::setPower(ESP_PWR_LVL_P9); /** +9db */
338342

339343
/** Optional: set any devices you don't want to get advertisments from */
340344
// NimBLEDevice::addIgnored(NimBLEAddress ("aa:bb:cc:dd:ee:ff"));
@@ -346,8 +350,8 @@ void setup (){
346350
pScan->setAdvertisedDeviceCallbacks(new AdvertisedDeviceCallbacks());
347351

348352
/** Set scan interval (how often) and window (how long) in milliseconds */
349-
pScan->setInterval(400);
350-
pScan->setWindow(100);
353+
pScan->setInterval(45);
354+
pScan->setWindow(15);
351355

352356
/** Active scan will gather scan response data from advertisers
353357
* but will use more energy from both devices

examples/NimBLE_Server/NimBLE_Server.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ void setup() {
142142
/** sets device name */
143143
NimBLEDevice::init("NimBLE-Arduino");
144144

145+
/** Optional: set the transmit power, default is 3db */
146+
NimBLEDevice::setPower(ESP_PWR_LVL_P9); /** +9db */
147+
145148
/** Set the IO capabilities of the device, each option will trigger a different pairing method.
146149
* BLE_HS_IO_DISPLAY_ONLY - Passkey pairing
147150
* BLE_HS_IO_DISPLAY_YESNO - Numeric comparison pairing

src/NimBLEAdvertising.cpp

Lines changed: 12 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -34,43 +34,12 @@ NimBLEAdvertising::NimBLEAdvertising() {
3434
memset(&m_scanData, 0, sizeof m_scanData);
3535
memset(&m_advParams, 0, sizeof m_advParams);
3636
const char *name = ble_svc_gap_device_name();
37-
/*
38-
int pwr;
39-
switch(esp_ble_tx_power_get(ESP_BLE_PWR_TYPE_DEFAULT)) {
40-
case ESP_PWR_LVL_N12:
41-
pwr = -12;
42-
break;
43-
case ESP_PWR_LVL_N9:
44-
pwr = -9;
45-
break;
46-
case ESP_PWR_LVL_N6:
47-
pwr = -6;
48-
break;
49-
case ESP_PWR_LVL_N3:
50-
pwr = -6;
51-
break;
52-
case ESP_PWR_LVL_N0:
53-
pwr = 0;
54-
break;
55-
case ESP_PWR_LVL_P3:
56-
pwr = 3;
57-
break;
58-
case ESP_PWR_LVL_P6:
59-
pwr = 6;
60-
break;
61-
case ESP_PWR_LVL_P9:
62-
pwr = 9;
63-
break;
64-
default:
65-
pwr = BLE_HS_ADV_TX_PWR_LVL_AUTO;
66-
break;
67-
}
68-
*/
37+
6938
m_advData.name = (uint8_t *)name;
7039
m_advData.name_len = strlen(name);
7140
m_advData.name_is_complete = 1;
7241
m_scanData.tx_pwr_lvl_is_present = 1;
73-
m_scanData.tx_pwr_lvl = NimBLEDevice::getPower(ESP_BLE_PWR_TYPE_DEFAULT);//pwr;
42+
m_scanData.tx_pwr_lvl = NimBLEDevice::getPower();
7443
m_advData.flags = (BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP);
7544
m_advData.appearance = 0;
7645
m_advData.appearance_is_present = 0;
@@ -259,11 +228,6 @@ void NimBLEAdvertising::start() {
259228
&m_serviceUUIDs[i].getNative()->u16.value, sizeof(uint16_t));
260229

261230
m_advData.uuids16[m_advData.num_uuids16].u.type = BLE_UUID_TYPE_16;
262-
/*
263-
char buf[BLE_UUID_STR_LEN];
264-
ble_uuid_to_str(&m_advData.uuids16[m_advData.num_uuids16].u, buf);
265-
NIMBLE_LOGI(LOG_TAG, "Advertising UUID: %s", buf);
266-
*/
267231
m_advData.uuids16_is_complete = 1;
268232
m_advData.num_uuids16++;
269233
}
@@ -284,12 +248,7 @@ void NimBLEAdvertising::start() {
284248
memcpy(&m_advData.uuids32[m_advData.num_uuids32].value,
285249
&m_serviceUUIDs[i].getNative()->u32.value, sizeof(uint32_t));
286250

287-
m_advData.uuids32[m_advData.num_uuids32].u.type = BLE_UUID_TYPE_32;
288-
/*
289-
char buf[BLE_UUID_STR_LEN];
290-
ble_uuid_to_str(&m_advData.uuids32[m_advData.num_uuids32].u, buf);
291-
NIMBLE_LOGI(LOG_TAG, "Advertising UUID: %s", buf);
292-
*/
251+
m_advData.uuids32[m_advData.num_uuids32].u.type = BLE_UUID_TYPE_32;
293252
m_advData.uuids32_is_complete = 1;
294253
m_advData.num_uuids32++;
295254
}
@@ -309,12 +268,7 @@ void NimBLEAdvertising::start() {
309268
memcpy(&m_advData.uuids128[m_advData.num_uuids128].value,
310269
&m_serviceUUIDs[i].getNative()->u128.value, 16);
311270

312-
m_advData.uuids128[m_advData.num_uuids128].u.type = BLE_UUID_TYPE_128;
313-
/*
314-
char buf[BLE_UUID_STR_LEN];
315-
ble_uuid_to_str(&m_advData.uuids128[m_advData.num_uuids128].u, buf);
316-
NIMBLE_LOGI(LOG_TAG, "Advertising UUID: %s", buf);
317-
*/
271+
m_advData.uuids128[m_advData.num_uuids128].u.type = BLE_UUID_TYPE_128;
318272
m_advData.uuids128_is_complete = 1;
319273
m_advData.num_uuids128++;
320274
}
@@ -362,7 +316,7 @@ void NimBLEAdvertising::start() {
362316
// throw the tx power data into the advertisment
363317
} else if (payloadLen < 29) {
364318
m_advData.tx_pwr_lvl_is_present = 1;
365-
m_advData.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
319+
m_advData.tx_pwr_lvl = NimBLEDevice::getPower();
366320
}
367321

368322
rc = ble_gap_adv_set_fields(&m_advData);
@@ -425,11 +379,15 @@ void NimBLEAdvertising::stop() {
425379
NIMBLE_LOGD(LOG_TAG, "<< stop");
426380
} // stop
427381

428-
/*
382+
383+
/**
384+
* Host reset seems to clear advertising data,
385+
* we need clear the flag so it reloads it.
386+
*/
429387
void NimBLEAdvertising::onHostReset() {
430-
// m_advSvcsSet = false;
388+
m_advSvcsSet = false;
431389
}
432-
*/
390+
433391

434392
/**
435393
* @brief Add data to the payload to be advertised.

src/NimBLEAdvertising.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class NimBLEAdvertising {
9090

9191
private:
9292
friend class NimBLEDevice;
93-
// void onHostReset();
93+
void onHostReset();
9494
ble_hs_adv_fields m_advData;
9595
ble_hs_adv_fields m_scanData;
9696
ble_gap_adv_params m_advParams;

src/NimBLEDevice.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,11 @@ void NimBLEDevice::stopAdvertising() {
351351
for(auto it = m_cList.cbegin(); it != m_cList.cend(); ++it) {
352352
(*it)->onHostReset();
353353
}
354-
354+
*/
355355
if(m_bleAdvertising != nullptr) {
356356
m_bleAdvertising->onHostReset();
357357
}
358-
*/
358+
359359
NIMBLE_LOGC(LOG_TAG, "Resetting state; reason=%d, %s", reason,
360360
NimBLEUtils::returnCodeToString(reason));
361361
} // onReset

src/NimBLEDevice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class NimBLEDevice {
8686
static NimBLEServer* createServer();
8787
static bool deleteClient(NimBLEClient* pClient);
8888
static void setPower(esp_power_level_t powerLevel);
89-
static int getPower(esp_ble_power_type_t powerType);
89+
static int getPower(esp_ble_power_type_t powerType=ESP_BLE_PWR_TYPE_DEFAULT);
9090
static void setCustomGapHandler(gap_event_handler handler);
9191
static void setSecurityAuth(bool bonding, bool mitm, bool sc);
9292
static void setSecurityAuth(uint8_t auth_req);

0 commit comments

Comments
 (0)