device-os icon indicating copy to clipboard operation
device-os copied to clipboard

Add API to set advertising scheme on disconnected

Open XuGuohui opened this issue 1 year ago • 1 comments

Problem

Currently when device acting as BLE peripheral is disconnected from peer device, it will re-start advertising automatically. In some use cases, user may wants to stop advertising on disconnected, unless otherwise user manually calls BLE.advertise() to start advertising.

Solution

Add wiring API to set advertising scheme for which after disconnected from peer deivce.

Example App

#include "application.h"

SYSTEM_MODE(MANUAL);

SerialLogHandler logHandler(LOG_LEVEL_ALL);

system_tick_t disconTime = 0;
bool restartAdv = false;

/* executes once at startup */
void setup() {
    // BLE.setAdvertisingScheme(BleAdvertisingScheme::RESTART_ADV_ON_DISCONNECTED);
    // BLE.setAdvertisingScheme(BleAdvertisingScheme::STOP_ADV_ON_DISCONNECTED);
    BLE.setAdvertisingScheme(BleAdvertisingScheme::STOP_ADV_ON_DISCONNECTED_ONCE);
    BLE.advertise();
    BLE.onDisconnected([&](const BlePeerDevice& peer) {
        disconTime = millis();
        restartAdv = true;
    });
}

/* executes continuously after setup() runs */
void loop() {
    if (restartAdv && (millis() - disconTime > 15000)) {
        restartAdv = false;
        if (!BLE.advertising()) {
            Log.info("Restarting advertising");
            BLE.advertise();
        }
    }
}

References

https://community.particle.io/t/ble-disconnect-works-but-then-reconnects/68469


Completeness

  • [x] User is totes amazing for contributing!
  • [x] Contributor has signed CLA (Info here)
  • [x] Problem and Solution clearly stated
  • [ ] Run unit/integration/application tests on device
  • [ ] Added documentation
  • [ ] Added to CHANGELOG.md after merging (add links to docs and issues)

XuGuohui avatar Oct 16 '24 16:10 XuGuohui