Skip to content

BLE.scan will not work after a BLE.scanFor[Uuid|Name|Address], even after a BLE.stopScan #350

Open

Description

@andrewchilds

Hello! Thank you for building and maintaining this library. Here is a simple test case with a buggy path, and a separate workaround using the existing library code:

#include <ArduinoBLE.h>

#define SERVICE_UUID "MY-SERVICE-UUID"

unsigned long scanStart;
bool fallbackToFullScan = false;

void setup() {
  Serial.begin(9600);
  delay(1000); // Wait for serial

  if (!BLE.begin()) {
    Serial.println("Starting BLE failed!");
    while (1);
  }

  Serial.println("Scanning by Service UUID...");
  BLE.scanForUuid(SERVICE_UUID);
  scanStart = millis();
}

void notWorkingScan() {
  BLE.stopScan();
  delay(1000);
  // This scan will not work - it will continue to behave like BLE.scanForUuid(SERVICE_UUID)
  BLE.scan();
}

void workingScan() {
  // This will reset _scanNameFilter, _scanUuidFilter, and _scanAddressFilter to ""
  BLE.scanForUuid("");
  BLE.stopScan();
  delay(1000);
  // This scan will now work
  BLE.scan();
}

void loop() {
  BLEDevice peripheral = BLE.available();

  if (peripheral) {
    Serial.println("Peripheral found!");
    BLE.stopScan();
  }

  // Wait 5 seconds to connect, then fall back to a regular scan.
  if (!fallbackToFullScan && millis() - scanStart > 5000) {
    scanStart = millis();
    Serial.println("Falling back to full scan...");
    fallbackToFullScan = true;
    // notWorkingScan();
    workingScan();
  }
}

From looking at GAP.cpp, it seems that _scanNameFilter, _scanUuidFilter, and _scanAddressFilter are not unset during a stopScan, so a BLE.scan will continue to use the previous filters (since BLE.available is where the filters are used). You can unset the filters by passing an empty string to scanForUuid(), scanForName(), or scanForAddress(), at which point the subsequent BLE.scan will start working.

Activity

  1. added
    type: imperfection
    topic: code
    on Feb 2, 2024
  2. added a commit that references this issue on Feb 5, 2024
    4a2096f
  3. added 4 commits that reference this issue on Mar 21, 2025
    1f7128c
    38a264c
    6e7edb2
    a35e40b
  4. added 2 commits that reference this issue on Apr 17, 2025
    1b9d1e8
    dd9d269
  5. added a commit that references this issue on Oct 22, 2025
    49ad82d
  6. added a commit that references this issue on Nov 28, 2025
    716c044
  7. added 2 commits that reference this issue on Aug 11, 2026
    d7d9013
    d2c9094
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: codetype: imperfection

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions