Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Gen4] fixes BLE scanning may break. #2747

Merged
merged 1 commit into from
Feb 29, 2024

Conversation

XuGuohui
Copy link
Member

Problem

BLE scans for BLE advertisements will quickly hit a failure and become unable to scan. The BLE.scan API will start to return immediately without performing the scan.

Solution

Reset the BT stack when it failed to start scanning.

Steps to Test

Run the following test app.

Example App

#include "Particle.h"

SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);

SerialLogHandler logHandler(LOG_LEVEL_ALL);

int advert_count;
void scan_result_cb(const BleScanResult *result, void *context) {
    advert_count++;
    // Log.info("\tgot advert");
}

void scan_thread_f() {
    unsigned int short_scan_count = 0;
    while (true) {
        unsigned int t0, t1;
        advert_count = 0;
        t0 = millis();
        BLE.scan(scan_result_cb, nullptr);
        t1 = millis();
        if (t1 - t0 < 50) {
            short_scan_count++;
        } else {
            short_scan_count = 0;
            if (t1 - t0 > 500) {
                Log.info("\tscan too long: %dms", t1 - t0);
            }
        }
        if (advert_count) {
            Log.info("BLE scan got %d adverts (duration %ums)", advert_count, t1 - t0);
        }
        if (short_scan_count && !(short_scan_count % 1000)) {
            Log.info("BLE scan too short last %d scans (ex. %dms)", short_scan_count, t1 - t0);
        }
        delay(1);
    }
}

void setup() {
    while (!Serial.isConnected()) {
        delay(100);
    }

    BLE.on();
    // quick scans should see the issue more quickly
    BLE.setScanTimeout(10);

    static Thread *thread = new Thread("sensor_ble_scan", scan_thread_f, OS_THREAD_PRIORITY_DEFAULT);
}

References

SC125720


Completeness

  • User is totes amazing for contributing!
  • Contributor has signed CLA (Info here)
  • 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)

Copy link
Member

@avtolstoy avtolstoy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@XuGuohui This works as a workaround. Do we have any understanding on why this happens especially if we are not doing any WiFi comms?

@XuGuohui XuGuohui merged commit 3443b4c into develop Feb 29, 2024
13 checks passed
@XuGuohui XuGuohui deleted the fix/SC125720/gen4-ble-scan-breaks branch February 29, 2024 14:49
@XuGuohui
Copy link
Member Author

I think it's that the BT stack state machine has race condition, causing that certain even is not progagated to high layer application.

@XuGuohui XuGuohui added bug ble Bluetooth Low Energy 5.x labels Feb 29, 2024
@eberseth
Copy link
Contributor

As Andrey mentioned this is a workaround. We'll need to continue looking for a root cause after this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
5.x ble Bluetooth Low Energy bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants