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

Reset scan filters when stopScan is called [Fixes #350] #351

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

andrewchilds
Copy link

This PR addresses a bug outlined in #350, where calling stopScan does not reset the filters set by scanByName etc, so a call to scan after will not work as expected.

Here's a barebones sketch that demonstrates the bug:

#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();
  }
}

@CLAassistant
Copy link

CLAassistant commented Feb 5, 2024

CLA assistant check
All committers have signed the CLA.

@per1234 per1234 added type: imperfection Perceived defect in any part of project topic: code Related to content of the project itself labels Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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