Skip to content

begin() doesn't work after end() on Nano BLE 33, have to power cycle #65

Closed
@rmlearney-digicatapult

Description

@rmlearney-digicatapult

Hi,

The begin() function does not behave as expected following an end() and instead I have to power cycle. This is similar to #33

I've created two minimal examples for people to try below:

Example 1:

  1. Initialise BLE
  2. Call BLE.disconnect() 10 seconds after connected to central
  3. Re-advertise 15 seconds later to allow reconnection

Expected behaviour - disconnect from central and allow reconnection after re-advertising
Actual behaviour - works as expected

#include <ArduinoBLE.h>

BLEService TestService("DEAD");
BLECharacteristic TestData("BEEF", BLEIndicate | BLENotify, 2, true);
BLEDescriptor TestDescriptor("BEEF", "Test");

uint32_t genericTimer = 0;
bool wasConnected = false;

void setup(){
    initBLE();
}

void loop(){
    BLEDevice central = BLE.central(); // begin listening for centrals to connect

    if(central){
        genericTimer = millis();
        while(central.connected()){
            if(millis() - genericTimer >= 10000){ // Wait 10 seconds after connect
                BLE.disconnect(); // Disconnect BLE
                wasConnected = true;
            }
        }
    }

    if(wasConnected){
        genericTimer = millis();
        while(millis() - genericTimer <= 15000){} // Wait 15 seconds after disconnect
        wasConnected = false;
        BLE.advertise();
    }
}

void initBLE(void){
    BLE.begin();
    BLE.setEventHandler(BLEConnected, blePeripheralConnectHandler);
    BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
    BLE.setLocalName("TestName");
    BLE.setDeviceName("Test Device Name");
    TestService.addCharacteristic(TestData);
    TestData.addDescriptor(TestDescriptor);
    BLE.addService(TestService);
    BLE.setAdvertisedService(TestService);
    BLE.advertise();
}

Example 2:

  1. Initialise BLE
  2. Disconnect and end() after 10 seconds of connection to central
  3. Wait 15 seconds and try to re-start BLE system again

Expected behaviour - system disconnects, then ends BLE service, and restarts it correctly
Actual behaviour - unable to restart BLE with begin() after calling end()

#include <ArduinoBLE.h>

BLEService TestService("DEAD");
BLECharacteristic TestData("BEEF", BLEIndicate | BLENotify, 2, true);
BLEDescriptor TestDescriptor("BEEF", "Test");

uint32_t genericTimer = 0;
bool wasConnected = false;

void setup(){
    initBLE();
}

void loop(){
    BLEDevice central = BLE.central(); // begin listening for centrals to connect

    if(central){
        genericTimer = millis();
        while(central.connected()){
            if(millis() - genericTimer >= 10000){ // Wait 10 seconds after connect
                BLE.disconnect(); // Disconnect BLE
                BLE.end(); // End BLE service
                wasConnected = true;
            }
        }
    }

    if(wasConnected){
        genericTimer = millis();
        while(millis() - genericTimer <= 15000){} // Wait 15 seconds after disconnect
        wasConnected = false;
        initBLE();
    }
}

void initBLE(void){
    BLE.begin();
    BLE.setEventHandler(BLEConnected, blePeripheralConnectHandler);
    BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
    BLE.setLocalName("TestName");
    BLE.setDeviceName("Test Device Name");
    TestService.addCharacteristic(TestData);
    TestData.addDescriptor(TestDescriptor);
    BLE.addService(TestService);
    BLE.setAdvertisedService(TestService);
    BLE.advertise();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions