Skip to content

BLEMidiServer.end() is it re-entrant? #42

Description

@dannyrubin

I have a use case where I need to start a BLEMidiServer, and some time later, end it, then start it again. I am trying to make use of the BLEMidiServer.end() to end the server and then reinitialize (included in the latest version of the library). I use Apple's AudioMidi Setup to connect to the server.

After I end(), then start again, NIMBle throws an error:
E NimBLEAdvertising: Error enabling advertising; rc=30,
and subsequent connect attempt using AudioMidi Setup, states:
W NimBLEAdvertising: Advertising already active
and connection cannot be made.

It seems like when end() is called, the service is still being advertised and therefore a restart runs into errors.
Is there a way to make this be fully re-entrant?

Here's a simple sketch to recreate this. Note you will have to use NIMBLE-Arduino library version 1.4.3 or lower.

#include <SPI.h>
#include <BLEMidi.h>
#define SerialMon Serial

const char* deviceName = "esp32-testBLEMidi";
bool serverStarted=0;
bool serverConnected=0;

void setupBLEMidiServer(){
BLEMidiServer.enableDebugging();

BLEMidiServer.setOnDisconnectCallback([]() {
    Serial.println(F("BLEMidi server Disconnected..."));
    serverConnected=0;
});

BLEMidiServer.setOnConnectCallback([]() {
    Serial.println(F("BLEMidi server Connected"));
    serverConnected=1;
});

BLEMidiServer.begin(deviceName);
Serial.println(F("BLEMidi server initialized"));
serverStarted=1;

}

void setup() {
Serial.begin(115200);
while (!Serial);
Serial.println(F("Serial port initialized"));

SPI.begin(); 

setupBLEMidiServer();

}

void loop() {

  if( serverStarted ){ 
      // wait 20 seconds then stop the midi server;
      delay(20000);

      BLEMidiServer.end();
      Serial.println( "BLEMidiServer ended" );
      serverStarted=0;

      // wait 20 seconds, then start it again
      delay(10000);
      setupBLEMidiServer();
      serverStarted = 1;

      Serial.printf("serverConnected = %d\n", serverConnected);
  }

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions