h2zero / NimBLE-Arduino

A fork of the NimBLE library structured for compilation with Arduino, for use with ESP32, nRF5x.
https://h2zero.github.io/NimBLE-Arduino/
Apache License 2.0
667 stars 138 forks source link

NimBLEServerCallbacks won't call onConnect(BLEServer*, NimBLEConnInfo&) #638

Open bmedici opened 5 months ago

bmedici commented 5 months ago

Hi,

I'm using this library in version 1.4.1 and identified an unexpected behaviour with server callbacks.

Here is the init code I'm using:

class ServerCallbacks: public NimBLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      deviceConnected = true;
    };

void loop() {
  log("deviceConnected: " + std::to_string(deviceConnected));
  }
}

BLEServer *pServer;
NimBLEDevice::init(BLE_NAME);
pServer = NimBLEDevice::createServer();
pServer->setCallbacks(new ServerCallbacks());

I implemented the server callbacks as shown and, in this situation, the deviceConnected variable is correctly set when my android device connects:

ble-master | deviceConnected: 0
ble-master | deviceConnected: 0
ble-master | deviceConnected: 0
// Android device connecting
ble-master | deviceConnected: 1
// The callback is called
ble-master | deviceConnected: 1
ble-master | deviceConnected: 1

On the other hand, while using the complete prototype for onConnect(BLEServer*, NimBLEConnInfo&), the callback is not called at all. With:

class ServerCallbacks: public NimBLEServerCallbacks {
    void onConnect(BLEServer* pServer, NimBLEConnInfo& connInfo) {
      deviceConnected = true;
    };
}

nothing happens

ble-master | deviceConnected: 0
ble-master | deviceConnected: 0
ble-master | deviceConnected: 0
// Android device connecting
ble-master | deviceConnected: 0
ble-master | deviceConnected: 0
ble-master | deviceConnected: 0
// Nothing happens

I double checked that the only difference between both tests is changing the function prototype from onConnect(BLEServer* pServer) to onConnect(BLEServer* pServer, NimBLEConnInfo& connInfo).

As I need the connInfoparameter, this bug is a bit annoying.

Any idea ?

h2zero commented 4 months ago

You are using v1.4.1, try switching to the master branch if you want to use the latest function signature, otherwise change to the old sig that takes the ble_conn_desc struct.

REEASD commented 2 months ago

you can add override. class ServerCallbacks: public NimBLEServerCallbacks { void onConnect(BLEServer* pServer,BLEConnInfo& connInfo) override { deviceConnected = true; };

void loop() { log("deviceConnected: " + std::to_string(deviceConnected)); } }