From 57b1172a88ad40594c0a1eb60f11bee305ecfcb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Tue, 17 Jul 2018 13:18:26 +0200 Subject: [PATCH] Add bluetooth available check and fix discovery issue for iOS --- .../wifisetup/bluetoothdeviceinfos.h | 2 +- .../wifisetup/bluetoothdiscovery.cpp | 19 ++++++++++++++++--- .../wifisetup/bluetoothdiscovery.h | 1 + nymea-app/ui/ConnectPage.qml | 3 +-- .../ui/connection/BluetoothDiscoveryPage.qml | 16 ++++++++++++++-- 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/libnymea-app-core/wifisetup/bluetoothdeviceinfos.h b/libnymea-app-core/wifisetup/bluetoothdeviceinfos.h index 8b87fdaa..9e47fbfb 100644 --- a/libnymea-app-core/wifisetup/bluetoothdeviceinfos.h +++ b/libnymea-app-core/wifisetup/bluetoothdeviceinfos.h @@ -49,7 +49,7 @@ public: Q_INVOKABLE BluetoothDeviceInfo *get(int index) const; void addBluetoothDeviceInfo(BluetoothDeviceInfo *deviceInfo); - void clearModel(); + Q_INVOKABLE void clearModel(); protected: QHash roleNames() const; diff --git a/libnymea-app-core/wifisetup/bluetoothdiscovery.cpp b/libnymea-app-core/wifisetup/bluetoothdiscovery.cpp index a9d573e9..e3bf1f26 100644 --- a/libnymea-app-core/wifisetup/bluetoothdiscovery.cpp +++ b/libnymea-app-core/wifisetup/bluetoothdiscovery.cpp @@ -38,6 +38,12 @@ BluetoothDiscovery::BluetoothDiscovery(QObject *parent) : return; } + if (localDevice.allDevices().isEmpty()) { + qWarning() << "BluetoothDiscovery: there is no bluetooth device available currently."; + setBluetoothAvailable(false); + return; + } + setBluetoothAvailable(true); if (localDevice.allDevices().count() > 1) { @@ -124,7 +130,7 @@ void BluetoothDiscovery::deviceDiscovered(const QBluetoothDeviceInfo &deviceInfo qDebug() << "BluetoothDiscovery: [+]" << deviceInformation->name() << "(" << deviceInformation->address() << ")" << (isLowEnergy ? "LE" : ""); - // + if (!isLowEnergy || deviceInformation->name().isEmpty()) { delete deviceInformation; return; @@ -148,6 +154,8 @@ void BluetoothDiscovery::discoveryFinished() { qDebug() << "BluetoothDiscovery: Discovery finished"; setDiscovering(false); + if (m_enabled) + start(); } void BluetoothDiscovery::onError(const QBluetoothDeviceDiscoveryAgent::Error &error) @@ -158,11 +166,14 @@ void BluetoothDiscovery::onError(const QBluetoothDeviceDiscoveryAgent::Error &er void BluetoothDiscovery::start() { + m_enabled = true; + + if (!m_discoveryAgent) + return; + if (m_discoveryAgent->isActive()) m_discoveryAgent->stop(); - m_deviceInfos->clearModel(); - qDebug() << "BluetoothDiscovery: Start discovering."; m_discoveryAgent->start(); setDiscovering(true); @@ -170,6 +181,8 @@ void BluetoothDiscovery::start() void BluetoothDiscovery::stop() { + m_enabled = false; + qDebug() << "BluetoothDiscovery: Stop discovering."; m_discoveryAgent->stop(); setDiscovering(false); diff --git a/libnymea-app-core/wifisetup/bluetoothdiscovery.h b/libnymea-app-core/wifisetup/bluetoothdiscovery.h index 05cb84e1..65401797 100644 --- a/libnymea-app-core/wifisetup/bluetoothdiscovery.h +++ b/libnymea-app-core/wifisetup/bluetoothdiscovery.h @@ -54,6 +54,7 @@ private: QBluetoothDeviceDiscoveryAgent *m_discoveryAgent = nullptr; BluetoothDeviceInfos *m_deviceInfos; + bool m_enabled = false; bool m_discovering = false; bool m_bluetoothAvailable = false; bool m_bluetoothEnabled = false; diff --git a/nymea-app/ui/ConnectPage.qml b/nymea-app/ui/ConnectPage.qml index e4860fb0..cdfc15d7 100644 --- a/nymea-app/ui/ConnectPage.qml +++ b/nymea-app/ui/ConnectPage.qml @@ -80,8 +80,7 @@ Page { title: qsTr("Connect %1").arg(app.systemName) model: ListModel { ListElement { iconSource: "../images/network-vpn.svg"; text: qsTr("Manual connection"); page: "connection/ManualConnectPage.qml" } - // Disabled for now as it's too buggy still -// ListElement { iconSource: "../images/bluetooth.svg"; text: qsTr("Wireless setup"); page: "connection/BluetoothDiscoveryPage.qml" } + ListElement { iconSource: "../images/bluetooth.svg"; text: qsTr("Wireless setup"); page: "connection/BluetoothDiscoveryPage.qml"; } ListElement { iconSource: "../images/private-browsing.svg"; text: qsTr("Demo mode"); page: "" } ListElement { iconSource: "../images/stock_application.svg"; text: qsTr("App settings"); page: "AppSettingsPage.qml" } } diff --git a/nymea-app/ui/connection/BluetoothDiscoveryPage.qml b/nymea-app/ui/connection/BluetoothDiscoveryPage.qml index ee57871d..e0b36f9d 100644 --- a/nymea-app/ui/connection/BluetoothDiscoveryPage.qml +++ b/nymea-app/ui/connection/BluetoothDiscoveryPage.qml @@ -13,7 +13,13 @@ Page { HeaderButton { imageSource: Qt.resolvedUrl("../images/refresh.svg") - onClicked: Engine.bluetoothDiscovery.start() + onClicked: { + if (Engine.bluetoothDiscovery.bluetoothAvailable) { + Engine.bluetoothDiscovery.deviceInfos.clearModel() + Engine.bluetoothDiscovery.start() + } + } + enabled: Engine.bluetoothDiscovery.bluetoothAvailable && !Engine.bluetoothDiscovery.discovering } } @@ -37,6 +43,8 @@ Page { pageStack.push(connectingPageComponent, { name: name, address: btAddress } ) } + + ColumnLayout { anchors.fill: parent spacing: app.margins @@ -46,9 +54,10 @@ Page { Layout.leftMargin: app.margins Layout.topMargin: app.margins Layout.rightMargin: app.rightMargin + Label { Layout.fillWidth: true - text: qsTr("Searching for %1 boxes via Bluetooth.").arg(app.systemName) + text: Engine.bluetoothDiscovery.bluetoothAvailable ? qsTr("Searching for %1 boxes via Bluetooth.").arg(app.systemName) : qsTr("Uh oh! Bluetooth is not available. Please make sure Bluetooth is enabled on this device.") wrapMode: Text.WordWrap } @@ -88,6 +97,9 @@ Page { spacing: app.margins Label { Layout.fillWidth: true + wrapMode: Text.WordWrap + maximumLineCount: 2 + elide: Text.ElideRight text: qsTr("Troubles finding your box? Try this!") } Button {