mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
Use QLowEnergyController without QEventLoop
We rather use wait in combination with spinning the event loop. Signed-off-by: Alex Blasche <alexander.blasche@qt.io> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
parent
57753321b0
commit
e79bede0aa
1 changed files with 43 additions and 21 deletions
|
@ -3,7 +3,10 @@
|
||||||
|
|
||||||
#include <QtBluetooth/QBluetoothAddress>
|
#include <QtBluetooth/QBluetoothAddress>
|
||||||
#include <QLowEnergyController>
|
#include <QLowEnergyController>
|
||||||
|
#include <QCoreApplication>
|
||||||
|
#include <QElapsedTimer>
|
||||||
#include <QEventLoop>
|
#include <QEventLoop>
|
||||||
|
#include <QThread>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
|
@ -18,6 +21,20 @@
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
||||||
|
void waitFor(int ms) {
|
||||||
|
Q_ASSERT(QCoreApplication::instance());
|
||||||
|
Q_ASSERT(QThread::currentThread());
|
||||||
|
|
||||||
|
QElapsedTimer timer;
|
||||||
|
timer.start();
|
||||||
|
|
||||||
|
do {
|
||||||
|
QCoreApplication::processEvents(QEventLoop::AllEvents, ms);
|
||||||
|
QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete);
|
||||||
|
QThread::msleep(10);
|
||||||
|
} while (timer.elapsed() < ms);
|
||||||
|
}
|
||||||
|
|
||||||
void BLEObject::serviceStateChanged(QLowEnergyService::ServiceState s)
|
void BLEObject::serviceStateChanged(QLowEnergyService::ServiceState s)
|
||||||
{
|
{
|
||||||
QList<QLowEnergyCharacteristic> list;
|
QList<QLowEnergyCharacteristic> list;
|
||||||
|
@ -144,21 +161,15 @@ dc_status_t qt_ble_open(dc_custom_io_t *io, dc_context_t *context, const char *d
|
||||||
|
|
||||||
qDebug() << "qt_ble_open(" << devaddr << ")";
|
qDebug() << "qt_ble_open(" << devaddr << ")";
|
||||||
|
|
||||||
// Wait until the connection succeeds or until an error occurs
|
|
||||||
QEventLoop loop;
|
|
||||||
loop.connect(controller, SIGNAL(connected()), SLOT(quit()));
|
|
||||||
loop.connect(controller, SIGNAL(error(QLowEnergyController::Error)), SLOT(quit()));
|
|
||||||
|
|
||||||
// Create a timer. If the connection doesn't succeed after five seconds or no error occurs then stop the opening step
|
|
||||||
QTimer timer;
|
|
||||||
int msec = 5000;
|
|
||||||
timer.setSingleShot(true);
|
|
||||||
loop.connect(&timer, SIGNAL(timeout()), SLOT(quit()));
|
|
||||||
|
|
||||||
// Try to connect to the device
|
// Try to connect to the device
|
||||||
controller->connectToDevice();
|
controller->connectToDevice();
|
||||||
timer.start(msec);
|
|
||||||
loop.exec();
|
// Create a timer. If the connection doesn't succeed after five seconds or no error occurs then stop the opening step
|
||||||
|
int msec = 5000;
|
||||||
|
while (msec > 0 && controller->state() == QLowEnergyController::ConnectingState) {
|
||||||
|
waitFor(100);
|
||||||
|
msec -= 100;
|
||||||
|
};
|
||||||
|
|
||||||
switch (controller->state()) {
|
switch (controller->state()) {
|
||||||
case QLowEnergyController::ConnectedState:
|
case QLowEnergyController::ConnectedState:
|
||||||
|
@ -174,24 +185,35 @@ dc_status_t qt_ble_open(dc_custom_io_t *io, dc_context_t *context, const char *d
|
||||||
|
|
||||||
/* We need to discover services etc here! */
|
/* We need to discover services etc here! */
|
||||||
BLEObject *ble = new BLEObject(controller);
|
BLEObject *ble = new BLEObject(controller);
|
||||||
loop.connect(controller, SIGNAL(discoveryFinished()), SLOT(quit()));
|
|
||||||
ble->connect(controller, SIGNAL(serviceDiscovered(QBluetoothUuid)), SLOT(addService(QBluetoothUuid)));
|
ble->connect(controller, SIGNAL(serviceDiscovered(QBluetoothUuid)), SLOT(addService(QBluetoothUuid)));
|
||||||
|
|
||||||
qDebug() << " .. discovering services";
|
qDebug() << " .. discovering services";
|
||||||
|
|
||||||
controller->discoverServices();
|
controller->discoverServices();
|
||||||
timer.start(msec);
|
|
||||||
loop.exec();
|
msec = 5000;
|
||||||
|
while (msec > 0 && controller->state() == QLowEnergyController::DiscoveringState) {
|
||||||
|
waitFor(100);
|
||||||
|
msec -= 100;
|
||||||
|
};
|
||||||
|
|
||||||
qDebug() << " .. done discovering services";
|
qDebug() << " .. done discovering services";
|
||||||
|
if (ble->preferredService() == nullptr) {
|
||||||
|
qDebug() << "failed to find suitable service on" << devaddr;
|
||||||
|
report_error("Failed to find suitable service on '%s'", devaddr);
|
||||||
|
controller->disconnectFromDevice();
|
||||||
|
delete controller;
|
||||||
|
return DC_STATUS_IO;
|
||||||
|
}
|
||||||
|
|
||||||
qDebug() << " .. discovering details";
|
qDebug() << " .. discovering details";
|
||||||
|
msec = 5000;
|
||||||
|
while (msec > 0 && ble->preferredService()->state() == QLowEnergyService::DiscoveringServices) {
|
||||||
|
waitFor(100);
|
||||||
|
msec -= 100;
|
||||||
|
};
|
||||||
|
|
||||||
timer.start(msec);
|
if (ble->preferredService()->state() != QLowEnergyService::ServiceDiscovered) {
|
||||||
loop.exec();
|
|
||||||
|
|
||||||
qDebug() << " .. done waiting";
|
|
||||||
if (ble->preferredService() == nullptr) {
|
|
||||||
qDebug() << "failed to find suitable service on" << devaddr;
|
qDebug() << "failed to find suitable service on" << devaddr;
|
||||||
report_error("Failed to find suitable service on '%s'", devaddr);
|
report_error("Failed to find suitable service on '%s'", devaddr);
|
||||||
controller->disconnectFromDevice();
|
controller->disconnectFromDevice();
|
||||||
|
|
Loading…
Add table
Reference in a new issue