Skip to content

Commit d32fa5c

Browse files
committed
Manually restart BLE advertising after disconnect to prevent stack freeze
Replaced use of `restartOnDisconnect(true)` with explicit (existing) manual re-advertising logic. This avoids Bluetooth stack instability caused by overlapping advertising state, Changes: - Added explicit `Bluefruit.Advertising.stop()` and data clears in `startAdv()` - Disabled automatic restart with `restartOnDisconnect(false)` - Re-advertising now fully handled in `checkRecvFrame()` loop Tested on: iPhone, Android, Windows, and Chrome – confirmed stable reconnects and name visibility.
1 parent ec98d5f commit d32fa5c

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/helpers/nrf52/SerialBLEInterface.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ void SerialBLEInterface::begin(const char* device_name, uint32_t pin_code) {
1818
}
1919

2020
void SerialBLEInterface::startAdv() {
21+
Bluefruit.Advertising.stop(); // always clean restart
22+
Bluefruit.Advertising.clearData(); // clear advertising data
23+
Bluefruit.ScanResponse.clearData(); // clear scan response data
24+
2125
// Advertising packet
2226
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
2327
Bluefruit.Advertising.addTxPower();
@@ -27,7 +31,7 @@ void SerialBLEInterface::startAdv() {
2731

2832
// Secondary Scan Response packet (optional)
2933
// Since there is no room for 'Name' in Advertising packet
30-
// Bluefruit.ScanResponse.addName();
34+
Bluefruit.ScanResponse.addName();
3135

3236
/* Start Advertising
3337
* - Enable auto advertising if disconnected
@@ -38,7 +42,7 @@ void SerialBLEInterface::startAdv() {
3842
* For recommended advertising interval
3943
* https://developer.apple.com/library/content/qa/qa1931/_index.html
4044
*/
41-
Bluefruit.Advertising.restartOnDisconnect(true);
45+
Bluefruit.Advertising.restartOnDisconnect(false); // don't restart automatically as already beeing done in checkRecvFrame()
4246
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
4347
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
4448
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds

0 commit comments

Comments
 (0)