summaryrefslogtreecommitdiffstats
path: root/core/qt-ble.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'core/qt-ble.cpp')
-rw-r--r--core/qt-ble.cpp62
1 files changed, 42 insertions, 20 deletions
diff --git a/core/qt-ble.cpp b/core/qt-ble.cpp
index 3b165aa94..85dd0b1d1 100644
--- a/core/qt-ble.cpp
+++ b/core/qt-ble.cpp
@@ -3,7 +3,10 @@
#include <QtBluetooth/QBluetoothAddress>
#include <QLowEnergyController>
+#include <QCoreApplication>
+#include <QElapsedTimer>
#include <QEventLoop>
+#include <QThread>
#include <QTimer>
#include <QDebug>
@@ -18,6 +21,20 @@
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)
{
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 << ")";
- // 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()));
+ // Try to connect to the device
+ controller->connectToDevice();
// 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
- controller->connectToDevice();
- timer.start(msec);
- loop.exec();
+ while (msec > 0 && controller->state() == QLowEnergyController::ConnectingState) {
+ waitFor(100);
+ msec -= 100;
+ };
switch (controller->state()) {
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! */
BLEObject *ble = new BLEObject(controller);
- loop.connect(controller, SIGNAL(discoveryFinished()), SLOT(quit()));
ble->connect(controller, SIGNAL(serviceDiscovered(QBluetoothUuid)), SLOT(addService(QBluetoothUuid)));
qDebug() << " .. discovering services";
controller->discoverServices();
- timer.start(msec);
- loop.exec();
+
+ msec = 5000;
+ while (msec > 0 && controller->state() == QLowEnergyController::DiscoveringState) {
+ waitFor(100);
+ msec -= 100;
+ };
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";
+ msec = 5000;
+ while (msec > 0 && ble->preferredService()->state() == QLowEnergyService::DiscoveringServices) {
+ waitFor(100);
+ msec -= 100;
+ };
- timer.start(msec);
- loop.exec();
-
- qDebug() << " .. done waiting";
- if (ble->preferredService() == nullptr) {
+ if (ble->preferredService()->state() != QLowEnergyService::ServiceDiscovered) {
qDebug() << "failed to find suitable service on" << devaddr;
report_error("Failed to find suitable service on '%s'", devaddr);
controller->disconnectFromDevice();