QT5 Bluetooth - scanning at regular intervals freezes the program

自作多情 提交于 2021-02-08 07:32:40

问题


I am currently using QT5 (c++) for implementing a bluetooth scanner which searches for all available devices in detection range. The program is running on a Raspberry PI 3, without a GUI (command line only).

The routine seems to work fine at first (3+ hours without issues). Then, after a seemingly random amount of repetitions, the bluetooth search doesn't find any device, even if they are physically there. After this happens, the program doesn't respond to system signals anymore, the program is not able to recover and must be manualy aborted.

The code of the scan is roughly the following:

 // create local QT application to run the event loop
 QCoreApplication app(0, NULL);
 // setup the device
 QBluetoothLocalDevice *localDevice = new QBluetoothLocalDevice;
 // setup discovery agent
 QBluetoothDeviceDiscoveryAgent *discoveryAgent = new QBluetoothDeviceDiscoveryAgent;
 discoveryAgent->setInquiryType(QBluetoothDeviceDiscoveryAgent::GeneralUnlimitedInquiry);
 // power on sensor and set as discoverable
 localDevice->powerOn();
 localDevice->setHostMode(QBluetoothLocalDevice::HostDiscoverable);
 // ask for both low energy and standard bluetooth devices to be found
 QBluetoothDeviceDiscoveryAgent::DiscoveryMethods type =
            QBluetoothDeviceDiscoveryAgent::LowEnergyMethod |
            QBluetoothDeviceDiscoveryAgent::ClassicMethod;
 // start an infinite loop 
 while(1){
    // start scan
    discoveryAgent->start(type);
    // execute QT main event loop
    app.exec();
    for (const auto& entry : discoveryAgent->discoveredDevices() ){
    // ... do something here and save the mac address of the detected devices for later analysis
    }
 }

I have tried to put a sleep(arbitrary_number_of_seconds); command after every scan to see if this helped with the problem, but it just mitigated it instead of solving the issue.

Any idea on what could be going wrong?

P.S.: removing QT5 and directly using the bluez libraries is not an option for this project, as I cannot release the final application under a strong copyleft license (other LGPL libraries are fine, if any).

来源:https://stackoverflow.com/questions/61866209/qt5-bluetooth-scanning-at-regular-intervals-freezes-the-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!