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 on portenta based devices #374

Merged

Conversation

fabik111
Copy link
Contributor

On portenta based board, 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 sketch 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());
    }
  }
}

The fix improves the HCICordioTransportClass::end() calling the ble.shutdown() of mbed-os BLE library and restores the default HCI data received handler to make a valid initialization of the BLE chip at the coming restart.

This PR depends on this PR on the mbed-os repository; do not merge separately.

This comment was marked as outdated.

@fabik111 fabik111 force-pushed the fix-shutdown-ble-portenta branch from eab5631 to 1b1db35 Compare November 25, 2024 10:34

This comment was marked as outdated.

@per1234 per1234 added type: imperfection Perceived defect in any part of project topic: code Related to content of the project itself labels Dec 6, 2024
@fabik111 fabik111 force-pushed the fix-shutdown-ble-portenta branch from 1b1db35 to 55c343a Compare March 10, 2025 14:25

This comment was marked as outdated.

@pennam
Copy link
Contributor

pennam commented Apr 7, 2025

@fabik111 isn't hciTrSerialRxIncoming available only for mbed palatform? if so I think we should wrap all the changes inside the #ifdefs

If i recall correctly arduino/mbed-os#37 is not really needed, can you confirm?

@fabik111
Copy link
Contributor Author

fabik111 commented Apr 7, 2025

If i recall correctly arduino/mbed-os#37 is not really needed, can you confirm?

Yeah I confirm that after more testing with mbed core v 4.2.4 and without that changes all is working . I would like to keep open (or in draft) that PR since I opened after I got strange errors during the begin after an end, so if the problem happens again we already have the fix.

@pennam pennam force-pushed the fix-shutdown-ble-portenta branch from 55c343a to bbc2baa Compare April 9, 2025 13:09
Copy link

codecov bot commented Apr 9, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 9.62%. Comparing base (9712cb1) to head (bbc2baa).
Report is 2 commits behind head on master.

Additional details and impacted files
@@          Coverage Diff           @@
##           master    #374   +/-   ##
======================================
  Coverage    9.62%   9.62%           
======================================
  Files          28      28           
  Lines        3656    3656           
======================================
  Hits          352     352           
  Misses       3304    3304           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@pennam pennam merged commit c91c02c into arduino-libraries:master Apr 9, 2025
14 checks passed
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.

3 participants