Add bluetooth available check and fix discovery issue for iOS

This commit is contained in:
Simon Stürz 2018-07-17 13:18:26 +02:00
parent 1b42ffd3e4
commit 57b1172a88
5 changed files with 33 additions and 8 deletions

View File

@ -49,7 +49,7 @@ public:
Q_INVOKABLE BluetoothDeviceInfo *get(int index) const; Q_INVOKABLE BluetoothDeviceInfo *get(int index) const;
void addBluetoothDeviceInfo(BluetoothDeviceInfo *deviceInfo); void addBluetoothDeviceInfo(BluetoothDeviceInfo *deviceInfo);
void clearModel(); Q_INVOKABLE void clearModel();
protected: protected:
QHash<int, QByteArray> roleNames() const; QHash<int, QByteArray> roleNames() const;

View File

@ -38,6 +38,12 @@ BluetoothDiscovery::BluetoothDiscovery(QObject *parent) :
return; return;
} }
if (localDevice.allDevices().isEmpty()) {
qWarning() << "BluetoothDiscovery: there is no bluetooth device available currently.";
setBluetoothAvailable(false);
return;
}
setBluetoothAvailable(true); setBluetoothAvailable(true);
if (localDevice.allDevices().count() > 1) { if (localDevice.allDevices().count() > 1) {
@ -124,7 +130,7 @@ void BluetoothDiscovery::deviceDiscovered(const QBluetoothDeviceInfo &deviceInfo
qDebug() << "BluetoothDiscovery: [+]" << deviceInformation->name() << "(" << deviceInformation->address() << ")" << (isLowEnergy ? "LE" : ""); qDebug() << "BluetoothDiscovery: [+]" << deviceInformation->name() << "(" << deviceInformation->address() << ")" << (isLowEnergy ? "LE" : "");
//
if (!isLowEnergy || deviceInformation->name().isEmpty()) { if (!isLowEnergy || deviceInformation->name().isEmpty()) {
delete deviceInformation; delete deviceInformation;
return; return;
@ -148,6 +154,8 @@ void BluetoothDiscovery::discoveryFinished()
{ {
qDebug() << "BluetoothDiscovery: Discovery finished"; qDebug() << "BluetoothDiscovery: Discovery finished";
setDiscovering(false); setDiscovering(false);
if (m_enabled)
start();
} }
void BluetoothDiscovery::onError(const QBluetoothDeviceDiscoveryAgent::Error &error) void BluetoothDiscovery::onError(const QBluetoothDeviceDiscoveryAgent::Error &error)
@ -158,11 +166,14 @@ void BluetoothDiscovery::onError(const QBluetoothDeviceDiscoveryAgent::Error &er
void BluetoothDiscovery::start() void BluetoothDiscovery::start()
{ {
m_enabled = true;
if (!m_discoveryAgent)
return;
if (m_discoveryAgent->isActive()) if (m_discoveryAgent->isActive())
m_discoveryAgent->stop(); m_discoveryAgent->stop();
m_deviceInfos->clearModel();
qDebug() << "BluetoothDiscovery: Start discovering."; qDebug() << "BluetoothDiscovery: Start discovering.";
m_discoveryAgent->start(); m_discoveryAgent->start();
setDiscovering(true); setDiscovering(true);
@ -170,6 +181,8 @@ void BluetoothDiscovery::start()
void BluetoothDiscovery::stop() void BluetoothDiscovery::stop()
{ {
m_enabled = false;
qDebug() << "BluetoothDiscovery: Stop discovering."; qDebug() << "BluetoothDiscovery: Stop discovering.";
m_discoveryAgent->stop(); m_discoveryAgent->stop();
setDiscovering(false); setDiscovering(false);

View File

@ -54,6 +54,7 @@ private:
QBluetoothDeviceDiscoveryAgent *m_discoveryAgent = nullptr; QBluetoothDeviceDiscoveryAgent *m_discoveryAgent = nullptr;
BluetoothDeviceInfos *m_deviceInfos; BluetoothDeviceInfos *m_deviceInfos;
bool m_enabled = false;
bool m_discovering = false; bool m_discovering = false;
bool m_bluetoothAvailable = false; bool m_bluetoothAvailable = false;
bool m_bluetoothEnabled = false; bool m_bluetoothEnabled = false;

View File

@ -80,8 +80,7 @@ Page {
title: qsTr("Connect %1").arg(app.systemName) title: qsTr("Connect %1").arg(app.systemName)
model: ListModel { model: ListModel {
ListElement { iconSource: "../images/network-vpn.svg"; text: qsTr("Manual connection"); page: "connection/ManualConnectPage.qml" } 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/private-browsing.svg"; text: qsTr("Demo mode"); page: "" }
ListElement { iconSource: "../images/stock_application.svg"; text: qsTr("App settings"); page: "AppSettingsPage.qml" } ListElement { iconSource: "../images/stock_application.svg"; text: qsTr("App settings"); page: "AppSettingsPage.qml" }
} }

View File

@ -13,7 +13,13 @@ Page {
HeaderButton { HeaderButton {
imageSource: Qt.resolvedUrl("../images/refresh.svg") 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 } ) pageStack.push(connectingPageComponent, { name: name, address: btAddress } )
} }
ColumnLayout { ColumnLayout {
anchors.fill: parent anchors.fill: parent
spacing: app.margins spacing: app.margins
@ -46,9 +54,10 @@ Page {
Layout.leftMargin: app.margins Layout.leftMargin: app.margins
Layout.topMargin: app.margins Layout.topMargin: app.margins
Layout.rightMargin: app.rightMargin Layout.rightMargin: app.rightMargin
Label { Label {
Layout.fillWidth: true 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 wrapMode: Text.WordWrap
} }
@ -88,6 +97,9 @@ Page {
spacing: app.margins spacing: app.margins
Label { Label {
Layout.fillWidth: true Layout.fillWidth: true
wrapMode: Text.WordWrap
maximumLineCount: 2
elide: Text.ElideRight
text: qsTr("Troubles finding your box? Try this!") text: qsTr("Troubles finding your box? Try this!")
} }
Button { Button {