/** * @file discovery.cpp * @author Pablo Urcola (pablo.urcola@bitbrain.com) * @brief Minimal example for LE discovery possible bug * @version 0.1 * @date 2023-02-14 * * @copyright Copyright (c) 2023 * */ #include #include #include #include /** * @brief This is a struct that will print the message when the function main is removed from the execution stack * */ struct MessageOnStackLiberation { ~MessageOnStackLiberation() { std::cout << "Stack free" << std::endl; } }; /** * @brief Runs a discovery agent for 10 seconds and terminates. * * If a single stop parameter is used, then the discovery will be canceled after 5 seconds and the application terminates right after the cancelation. (This is the failing condition) * Otherwise, no parameters or other parameters, a fixed 10 second LE device discovery is performed. * * @param argc * @param argv * @return int */ int main(int argc, char **argv) { // To check when the funciton main terminates MessageOnStackLiberation message; QCoreApplication app(argc, argv); QBluetoothDeviceDiscoveryAgent discoveryAgent; discoveryAgent.setLowEnergyDiscoveryTimeout(10000); // set a timeout // Quit the app if the discovery is finished QObject::connect(&discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, [&app](){ std::cout << "finished" << std::endl; app.quit(); }); // Quit the app if the discovery is canceled QObject::connect(&discoveryAgent, &QBluetoothDeviceDiscoveryAgent::canceled, [&app](){ std::cout << "canceled" << std::endl; app.quit(); }); // Start the discovery when the application starts QTimer::singleShot(0, [&discoveryAgent](){ std::cout << "start" << std::endl; discoveryAgent.start(); }); // If a stop is used as parameter in the call of the program, then the discovery is stopped at 5 seconds if (argc > 1 && std::strcmp(argv[1], "stop") == 0) { QTimer::singleShot(5000, [&discoveryAgent](){ std::cout << "stop" << std::endl; discoveryAgent.stop(); }); } return app.exec(); }