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

Fix for BLE restart after a BLE end #37

Open
wants to merge 1 commit into
base: extrapatches-6.17.0
Choose a base branch
from

Conversation

fabik111
Copy link

@fabik111 fabik111 commented Oct 29, 2024

Summary of changes

If the user sketch starts and stops BLE several time, after the first time, the BLE stack advertises the wrong BLE mac address ( "AA:AA:AA:AA:AA:AA" ). This is caused because the BLE.end() function of ArduinoBLE doesn't turn off correctly the BLE mbed-os library.

Here a cketch for reproducing the bug.

#include <ArduinoBLE.h>
REDIRECT_STDOUT_TO(Serial);
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // Bluetooth® Low Energy LED Service

// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
bool turnedOn = false;
const int ledPin = LED_BUILTIN; // pin to use for the LED
uint32_t lastChange = 0;
void turnBLEon(){
 // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting Bluetooth® Low Energy module failed!");

    while (1);
  }

  // set advertised local name and service UUID:
  BLE.setLocalName("LED");
  BLE.setAdvertisedService(ledService);

  // add the characteristic to the service
  ledService.addCharacteristic(switchCharacteristic);

  // add service
  BLE.addService(ledService);

  // set the initial value for the characeristic:
  switchCharacteristic.writeValue(0);

  // start advertising
  BLE.advertise();

  Serial.println("BLE LED Peripheral");
  turnedOn = true;
}

void turnBLEoff(){
  BLEDevice central = BLE.central();
  if(central && central.connected()){
    BLE.disconnect();
  }
  BLE.end();
  turnedOn=false;
}

void setup() {
  Serial.begin(9600);
  while (!Serial);
  //BLE.debug(Serial);
  // set LED pin to output mode
  pinMode(ledPin, OUTPUT);

  turnBLEon();
  lastChange = millis();
}

void loop() {

  if(lastChange + 20000 < millis()){
    if(turnedOn){
      Serial.println("change turn off");
      turnBLEoff();

    }else{
      Serial.println("change turn on");
      turnBLEon();
    }
    lastChange = millis();
  }

  if(turnedOn){
    // listen for Bluetooth® Low Energy peripherals to connect:
    BLEDevice central = BLE.central();

    // if a central is connected to peripheral:
    if (central) {
      Serial.print("Connected to central: ");
      // print the central's MAC address:
      Serial.println(central.address());

      // while the central is still connected to peripheral:
      while (central.connected()) {
        // if the remote device wrote to the characteristic,
        // use the value to control the LED:
        if (switchCharacteristic.written()) {
          if (switchCharacteristic.value()) {   // any value other than 0
            Serial.println("LED on");
            digitalWrite(ledPin, HIGH);         // will turn the LED on
          } else {                              // a 0 value
            Serial.println(F("LED off"));
            digitalWrite(ledPin, LOW);          // will turn the LED off
          }
        }
      }

      // when the central disconnects, print it out:
      Serial.print(F("Disconnected from central: "));
      Serial.println(central.address());
    }
  }
}

Here the fix on ArduinoBLE

This PR is needed for solving the problem since that after the second wake up the radio chip is not reset and sends an erroneous HCI response that is not related to the Reset request performed by the BLE stack during the init phase. With this change this packet will be ignored, instead of hanging the board. This solution works since the radio chip sends as second HCI response the expected reset response.

Impact of changes

Migration actions required

Documentation


Pull request type

[] Patch update (Bug fix / Target update / Docs update / Test update / Refactor)
[] Feature update (New feature / Functionality change / New API)
[] Major update (Breaking change E.g. Return code change / API behaviour change)

Test results

[] No Tests required for this change (E.g docs only update)
[] Covered by existing mbed-os tests (Greentea or Unittest)
[] Tests / results supplied as part of this PR

Reviewers


@fabik111 fabik111 force-pushed the fix-shutdown-ble-potenta branch from f18645d to 30cf041 Compare November 25, 2024 09:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant