NimBLE-Arduino
NimBLE-Arduino copied to clipboard
NimBLEServerCallbacks won't call onConnect(BLEServer*, NimBLEConnInfo&)
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 connInfo
parameter, this bug is a bit annoying.
Any idea ?
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.
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)); } }