arduinoesp32

How to define the callback for an esp32 arduino ble scan result


The definition to start a BLE scan is:

bool start(uint32_t duration, void (*scanCompleteCB)(BLEScanResults), bool is_continue = false);

The second parameter seems to be the callback when a scan is complete, being somewhat new to this Im unsure how to define it.

fwiw Ive tried this:

void OnScanResults(BLEScanResults scanResults)
{ }

and used it like this:

scanResults = scan->start(60, OnScanResults, true);

but obvious to others, that didnt work.

Please help me decypher that signature

void (*scanCompleteCB)(BLEScanResults)

Solution

  • you need to add & to OnScanResults because:

    void (*scanCompleteCB)(BLEScanResults)
    

    is a pointer to a function which takes a BLEScanResults, returns nothing and is called scanCompleteCB

    So your call should be:

    scanResults = scan->start(60, &OnScanResults, true);
    

    just as a pointer to a int points to the address of a int

    int pointedTo;
    int* ptr = &pointedTo;