OS crash within BLE.available()

I'm trying to use two Nano BLE boards -- one beacon, one scanner, to transfer info using only the advertisement. (Data buried in local name.) In my original efforts, the OS seemed to crash at random times, and only when my beacon was active. After a month of frustration, I finally narrowed the problem to a bug in BLE.available(). It had nothing to do with my beacon, except that detecting it caused a branch into a portion of code that included a delay.

The problem can be demonstrated with two apparently innocuous changes to the example "Scan": (1) Change BLE.scan() to BLE.scan(true); (2) Add a delay at the end of the if(peripheral) code.

Printouts before and after BLE.available show that the crash occurs there.

There is an additional bug that I have not yet localized:
Without the delay, the sketch continues to run, but at some point (possibly hours later) it no longer recognizes peripherals!

/*
  Scan

  This example scans for BLE peripherals and prints out their advertising details:
  address, local name, adverised service UUID's.

  The circuit:
  - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT,
    Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board.

  This example code is in the public domain.
*/

// ==> Two (minor?) changes consistently crash the OS 
// ==> The crash occurs within BLE.available()

#include <ArduinoBLE.h>

void setup() {
  Serial.begin(9600);
  while (!Serial);

  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");

    while (1);
  }

  Serial.println("BLE Central scan");

  // start scanning for peripheral
  BLE.scan([color=red]true[/color]);                               [color=red]// (1) Modify BLE.scan(); so the same ad is recognized multiple times[/color]
}

void loop() {
  // check if a peripheral has been discovered
 [color=orange] [color=brown]Serial.print("B4-");[/color][/color]
  BLEDevice peripheral = BLE.available();
 [color=orange] [color=maroon]Serial.println("FTR");                        // (3) Add before-after printing to show crash occurs within BLE.available()[/color][/color]

  if (peripheral) {
    // discovered a peripheral
    Serial.println("Discovered a peripheral");
    Serial.println("-----------------------");

    // print address
    Serial.print("Address: ");
    Serial.println(peripheral.address());

    // print the local name, if present
    if (peripheral.hasLocalName()) {
      Serial.print("Local Name: ");
      Serial.println(peripheral.localName());
    }

    // print the advertised service UUIDs, if present
    if (peripheral.hasAdvertisedServiceUuid()) {
      Serial.print("Service UUIDs: ");
      for (int i = 0; i < peripheral.advertisedServiceUuidCount(); i++) {
        Serial.print(peripheral.advertisedServiceUuid(i));
        Serial.print(" ");
      }
      Serial.println();
    }

    // print the RSSI
    Serial.print("RSSI: ");
    Serial.println(peripheral.rssi());

    Serial.println();
    [color=red]delay(2500);                               // (2) Add a delay at end of if(peripheral) -- causes OS crash![/color]
  }
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.