From 8b8863b6407f4c80bfb3f4a51ea8eeeedf7f0ab5 Mon Sep 17 00:00:00 2001 From: Jan Mulder Date: Wed, 11 Oct 2017 19:31:45 +0200 Subject: [PATCH 1/3] Add function to clear connectionModel data Preparation primarily for mobile. When we want to switch in one session from BT to cable connection and vise versa, we need a way to clear the model data containing the possible connections in use. Signed-off-by: Jan Mulder --- core/connectionlistmodel.cpp | 7 +++++++ core/connectionlistmodel.h | 1 + 2 files changed, 8 insertions(+) diff --git a/core/connectionlistmodel.cpp b/core/connectionlistmodel.cpp index 3e1e0d71c..7f8b9894b 100644 --- a/core/connectionlistmodel.cpp +++ b/core/connectionlistmodel.cpp @@ -42,3 +42,10 @@ void ConnectionListModel::addAddress(const QString address) m_addresses.append(address); endInsertRows(); } + +void ConnectionListModel::removeAllAddresses() +{ + beginRemoveRows(QModelIndex(), 0, rowCount()); + m_addresses.clear(); + endRemoveRows(); +} diff --git a/core/connectionlistmodel.h b/core/connectionlistmodel.h index ec3a785fa..b7b1db5c9 100644 --- a/core/connectionlistmodel.h +++ b/core/connectionlistmodel.h @@ -15,6 +15,7 @@ public: QString address(int idx) const; int rowCount(const QModelIndex &parent = QModelIndex()) const; void addAddress(const QString address); + void removeAllAddresses(); private: QStringList m_addresses; }; From fa5e685279b66b14d8069b5214890ca7148487ef Mon Sep 17 00:00:00 2001 From: Jan Mulder Date: Wed, 11 Oct 2017 19:38:24 +0200 Subject: [PATCH 2/3] Add function for non-BT connection addresses Simple rewrite of a piece of code separated to its own function so that is can be used in other places as well. To avoid code duplication for dynamic BT on/off switching on mobile. Signed-off-by: Jan Mulder --- core/qt-gui.h | 1 + subsurface-mobile-helper.cpp | 25 +++++++++++++++---------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/core/qt-gui.h b/core/qt-gui.h index 59e79c268..1fc7d161e 100644 --- a/core/qt-gui.h +++ b/core/qt-gui.h @@ -7,6 +7,7 @@ void init_ui(); void run_ui(); void exit_ui(); +void set_non_bt_addresses(); #if defined(SUBSURFACE_MOBILE) #include diff --git a/subsurface-mobile-helper.cpp b/subsurface-mobile-helper.cpp index 56ad029e1..ae7143404 100644 --- a/subsurface-mobile-helper.cpp +++ b/subsurface-mobile-helper.cpp @@ -30,6 +30,19 @@ QObject *qqWindowObject = NULL; +void set_non_bt_addresses() { +#if defined(Q_OS_ANDROID) + connectionListModel.addAddress("FTDI"); +#elif defined(Q_OS_LINUX) // since this is in the else, it does NOT include Android + connectionListModel.addAddress("/dev/ttyS0"); + connectionListModel.addAddress("/dev/ttyS1"); + connectionListModel.addAddress("/dev/ttyS2"); + connectionListModel.addAddress("/dev/ttyS3"); + // this makes debugging so much easier - use the simulator + connectionListModel.addAddress("/tmp/ttyS1"); +#endif +} + void init_ui() { init_qt_late(); @@ -76,16 +89,8 @@ void run_ui() ctxt->setContextProperty("diveModel", sortModel); ctxt->setContextProperty("gpsModel", gpsSortModel); ctxt->setContextProperty("vendorList", vendorList); -#if defined(Q_OS_ANDROID) - connectionListModel.addAddress("FTDI"); -#elif defined(Q_OS_LINUX) // since this is in the else, it does NOT include Android - connectionListModel.addAddress("/dev/ttyS0"); - connectionListModel.addAddress("/dev/ttyS1"); - connectionListModel.addAddress("/dev/ttyS2"); - connectionListModel.addAddress("/dev/ttyS3"); - // this makes debugging so much easier - use the simulator - connectionListModel.addAddress("/tmp/ttyS1"); -#endif + set_non_bt_addresses(); + ctxt->setContextProperty("connectionListModel", &connectionListModel); ctxt->setContextProperty("logModel", MessageHandlerModel::self()); From ba4058667a21a278a394054fea70595358ac41f2 Mon Sep 17 00:00:00 2001 From: Jan Mulder Date: Thu, 12 Oct 2017 09:43:40 +0200 Subject: [PATCH 3/3] mobile: enable switching BT on/off during session. This commit implements possible switching BT on and off during a session, so not needing a restart of the app when the user forgot to switch it on when starting the app. For this, the following needed to be done: 1) create a handler that reacts on local BT device status changes. 2) repopulate the connection list in the download screen when a BT status change is detected. Notice the subtile change of the Q_INVOKABLE btEnabled() function to a Q_PROPERTY. This gives a nice dynamic behaviour when switching BT on/off with the app open. Fixes: #556 Signed-off-by: Jan Mulder --- core/btdiscovery.cpp | 7 +++++- core/btdiscovery.h | 3 ++- .../qml/DownloadFromDiveComputer.qml | 4 +-- mobile-widgets/qmlmanager.cpp | 25 +++++++++++++++++++ mobile-widgets/qmlmanager.h | 7 +++++- 5 files changed, 41 insertions(+), 5 deletions(-) diff --git a/core/btdiscovery.cpp b/core/btdiscovery.cpp index f9c6e8522..281e3b314 100644 --- a/core/btdiscovery.cpp +++ b/core/btdiscovery.cpp @@ -60,6 +60,12 @@ BTDiscovery::BTDiscovery(QObject *parent) } m_instance = this; #if defined(BT_SUPPORT) + BTDiscoveryReDiscover(); +#endif +} + +void BTDiscovery::BTDiscoveryReDiscover() +{ #if !defined(Q_OS_IOS) if (localBtDevice.isValid() && localBtDevice.hostMode() == QBluetoothLocalDevice::HostConnectable) { @@ -100,7 +106,6 @@ BTDiscovery::BTDiscovery(QObject *parent) m_btValid = false; } #endif -#endif } BTDiscovery::~BTDiscovery() diff --git a/core/btdiscovery.h b/core/btdiscovery.h index 61d03f49b..7a9e9c655 100644 --- a/core/btdiscovery.h +++ b/core/btdiscovery.h @@ -46,6 +46,8 @@ public: void getBluetoothDevices(); #endif QList getBtDcs(); + QBluetoothLocalDevice localBtDevice; + void BTDiscoveryReDiscover(); private: static BTDiscovery *m_instance; @@ -59,7 +61,6 @@ private: #endif QList btPairedDevices; - QBluetoothLocalDevice localBtDevice; QBluetoothDeviceDiscoveryAgent *discoveryAgent; signals: diff --git a/mobile-widgets/qml/DownloadFromDiveComputer.qml b/mobile-widgets/qml/DownloadFromDiveComputer.qml index 9ff0494c0..b7857f871 100644 --- a/mobile-widgets/qml/DownloadFromDiveComputer.qml +++ b/mobile-widgets/qml/DownloadFromDiveComputer.qml @@ -17,8 +17,8 @@ Kirigami.Page { property alias dcImportModel: importModel property bool divesDownloaded: false - property bool btEnabled: manager.btEnabled() - property string btMessage: manager.btEnabled() ? "" : qsTr("Bluetooth is not enabled") + property bool btEnabled: manager.btEnabled + property string btMessage: manager.btEnabled ? "" : qsTr("Bluetooth is not enabled") DCDownloadThread { id: downloadThread diff --git a/mobile-widgets/qmlmanager.cpp b/mobile-widgets/qmlmanager.cpp index dc37fd4b0..5fbe61614 100644 --- a/mobile-widgets/qmlmanager.cpp +++ b/mobile-widgets/qmlmanager.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include "qt-models/divelistmodel.h" #include "qt-models/gpslistmodel.h" @@ -77,6 +78,23 @@ extern "C" int gitProgressCB(const char *text) return 0; } +void QMLManager::btHostModeChange(QBluetoothLocalDevice::HostMode state) +{ + BTDiscovery *btDiscovery = BTDiscovery::instance(); + + qDebug() << "btHostModeChange to " << state; + if (state != QBluetoothLocalDevice::HostPoweredOff) { + connectionListModel.removeAllAddresses(); + btDiscovery->BTDiscoveryReDiscover(); + m_btEnabled = btDiscovery->btAvailable(); + } else { + connectionListModel.removeAllAddresses(); + set_non_bt_addresses(); + m_btEnabled = false; + } + emit btEnabledChanged(); +} + QMLManager::QMLManager() : m_locationServiceEnabled(false), m_verboseEnabled(false), reply(0), @@ -117,6 +135,8 @@ QMLManager::QMLManager() : m_locationServiceEnabled(false), // to QML, but that doesn't seem to always work BTDiscovery *btDiscovery = BTDiscovery::instance(); m_btEnabled = btDiscovery->btAvailable(); + connect(&btDiscovery->localBtDevice, &QBluetoothLocalDevice::hostModeStateChanged, + this, &QMLManager::btHostModeChange); #else m_btEnabled = false; #endif @@ -1659,6 +1679,11 @@ bool QMLManager::btEnabled() const return m_btEnabled; } +void QMLManager::setBtEnabled(bool value) +{ + m_btEnabled = value; +} + #if defined (Q_OS_ANDROID) void writeToAppLogFile(QString logText) diff --git a/mobile-widgets/qmlmanager.h b/mobile-widgets/qmlmanager.h index 1da147c9f..45851dfdf 100644 --- a/mobile-widgets/qmlmanager.h +++ b/mobile-widgets/qmlmanager.h @@ -45,6 +45,7 @@ class QMLManager : public QObject { Q_PROPERTY(QString progressMessage READ progressMessage WRITE setProgressMessage NOTIFY progressMessageChanged) Q_PROPERTY(bool libdcLog READ libdcLog WRITE setLibdcLog NOTIFY libdcLogChanged) Q_PROPERTY(bool developer READ developer WRITE setDeveloper NOTIFY developerChanged) + Q_PROPERTY(bool btEnabled READ btEnabled WRITE setBtEnabled NOTIFY btEnabledChanged) public: QMLManager(); @@ -124,6 +125,9 @@ public: bool developer() const; void setDeveloper(bool value); + bool btEnabled() const; + void setBtEnabled(bool value); + typedef void (QMLManager::*execute_function_type)(); DiveListSortModel *dlSortModel; @@ -134,7 +138,7 @@ public: bool showPin() const; void setShowPin(bool enable); Q_INVOKABLE void setStatusbarColor(QColor color); - Q_INVOKABLE bool btEnabled() const; + void btHostModeChange(QBluetoothLocalDevice::HostMode state); #if defined(Q_OS_ANDROID) void writeToAppLogFile(QString logText); @@ -258,6 +262,7 @@ signals: void progressMessageChanged(); void libdcLogChanged(); void developerChanged(); + void btEnabledChanged(); }; #endif