Sensirion/arduino-ble-gadget

Handle OnConnect and onDisconnect

KASSIMSAMJI opened this issue · 1 comments

Hi there

It's shame that I am struggling to handle onConnect() and onDiscconect() with no luck

`

// start code

#ifdef USE_BLE

#include "Sensirion_Gadget_BLE.h"

NimBLELibraryWrapper lib;
DataProvider provider(lib, DataType::T_RH_CO2_ALT, false, true);

static int64_t lastMeasurementTimeMs = 0;
static int measurementIntervalMs = 1000;

static int64_t lastBatteryLevelUpdateMs = 0;
static int batteryLevelUpdateIntervalMs = 60000;

#endif

class my_call_backs: public IProviderCallbacks {

};

void ble_setup(), ble_loop();

void ble_setup() {

if ( !( rf_mode_selected == both_radio || rf_mode_selected == ble_only )) {
return;
}

if ( ! psramFound() ) {
return;
}

#ifdef USE_BLE
// Serial.begin(115200);
// delay(1000); // Wait for Serial monitor to start

Serial.println("BLE App Starting");

ble_ready = true;

// Initialize the GadgetBle Library
provider.begin();

lib.setProviderCallbacks(new my_call_backs());

Serial.print("Sensirion GadgetBle Lib initialized with deviceId = ");
Serial.println(provider.getDeviceIdString());

#endif
// Set initial battery level
// provider.setBatteryLevel(batteryLevel);
}

void ble_loop() {

if (! ble_ready ) {
return;
}

if ( !( rf_mode_selected == both_radio || rf_mode_selected == ble_only )) {
return;
}

#ifdef USE_BLE

if ( millis() - lastMeasurementTimeMs >= measurementIntervalMs ) {
Serial.println("BLE Measurement");
provider.writeValueToCurrentSample(currentTemp % 50, SignalType::TEMPERATURE_DEGREES_CELSIUS);
provider.writeValueToCurrentSample(currentHumid % 100, SignalType::RELATIVE_HUMIDITY_PERCENTAGE);
provider.writeValueToCurrentSample(currentCO2, SignalType::CO2_PARTS_PER_MILLION);
provider.setBatteryLevel(int(batt_perc));
provider.commitSample();
lastMeasurementTimeMs = millis();
}

// provider.handleDownload();

#endif
}

// end code`

The goal is to capture app connection and disconnection event

Thanks

Hi, overwriting the callbacks with the lib.setProviderCallbacks() breaks some internal logic, which is why your code doesn't run properly. Concretely, the DataProvider object in the library sets some internal variables (whether we are currently downloading for example) on connection events, for which the callbacks are defined in the arduino-ble-gadget/src/DataProvider.h and the corresponding .cpp files. There is currently no simple interface to add to these callbacks from your main Arduino sketch, however you could go into the library code and add some custom lines to these definitions (again, the callback definitions in the DataProvider.cpp file). Hope this helps!