diff options
author | Kari Hautamäki <kari.hautamaki@qt.io> | 2017-02-16 08:33:49 +0200 |
---|---|---|
committer | Maurice Kalinowski <maurice.kalinowski@qt.io> | 2017-02-28 13:16:09 +0000 |
commit | 35c1c07cd676b9c9afb23ad7266a67d5b6aef03a (patch) | |
tree | d43445955be5f1799ca9bb9db2c6e2cfd7915370 /tradeshow/iot-sensortag/sensortagdataproviderpool.cpp | |
parent | dec3883a1d0d7d24c797cf517fa7fad85b24a1f2 (diff) |
iot-sensortag: Allow reconnect after a failed connection attempt
A disconnected sensor tag can be tried to be reconnected by clicking
on the chart area or starting rescan from the Sensor settings menu.
During a (re)connection process a progress indicator is shown to
the user.
The user has a possibility to disconnect (and reconnect) a
currently connect SensorDataProvider
A new state, 'NotFound' is added in SensorTagDataProvider to
distinguish between states when a Sensor Tag has not been discovered
at all, and when it has already been discovered but is has been
disconnected.
Change-Id: I54eea72d8c92a67a5ccbb3bc192ac8f33ada1c39
Reviewed-by: Maurice Kalinowski <maurice.kalinowski@qt.io>
Diffstat (limited to 'tradeshow/iot-sensortag/sensortagdataproviderpool.cpp')
-rw-r--r-- | tradeshow/iot-sensortag/sensortagdataproviderpool.cpp | 55 |
1 files changed, 38 insertions, 17 deletions
diff --git a/tradeshow/iot-sensortag/sensortagdataproviderpool.cpp b/tradeshow/iot-sensortag/sensortagdataproviderpool.cpp index f9edad0..987e8b3 100644 --- a/tradeshow/iot-sensortag/sensortagdataproviderpool.cpp +++ b/tradeshow/iot-sensortag/sensortagdataproviderpool.cpp @@ -74,16 +74,22 @@ SensorTagDataProviderPool::SensorTagDataProviderPool(QString poolName, QObject* void SensorTagDataProviderPool::startScanning() { - qDeleteAll(m_dataProviders); - m_dataProviders.clear(); - - m_discoveryAgent->start(); + m_discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod); if (m_discoveryAgent->isActive()) { - m_deviceScanState = true; + emit scanStarted(); } } +void SensorTagDataProviderPool::disconnectProvider(QString id) +{ + SensorTagDataProvider *p = findProvider(id); + if (BluetoothDataProvider *btp = qobject_cast<BluetoothDataProvider*>(findProvider(id))) + btp->unbindDevice(); + else if (p) + p->setState(SensorTagDataProvider::Disconnected); +} + void SensorTagDataProviderPool::setMacFilterList(const QStringList &addressList) { m_macFilters = addressList; @@ -118,7 +124,6 @@ void SensorTagDataProviderPool::updateProviderForCloud() void SensorTagDataProviderPool::deviceDiscoveryFinished() { - m_deviceScanState = false; finishScanning(); emit scanFinished(); } @@ -130,8 +135,6 @@ void SensorTagDataProviderPool::finishScanning() void SensorTagDataProviderPool::btDeviceFound(const QBluetoothDeviceInfo &info) { - qCDebug(boot2QtDemos) << "Found a Bluetooth device. Name:" << info.name() << ", addr:" << info.address().toString(); - if (info.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) { bool filtered = m_macFilters.length() || m_nameFilters.length(); bool found = filtered ? false : true; @@ -142,14 +145,24 @@ void SensorTagDataProviderPool::btDeviceFound(const QBluetoothDeviceInfo &info) found = true; if (found) { - qCDebug(boot2QtDemos) << " Adding to the available devices"; - BluetoothDataProvider* dataProvider = new BluetoothDataProvider(info.address().toString(), this); - BluetoothDevice *d = new BluetoothDevice(info); - dataProvider->bindToDevice(d); - m_dataProviders.append(dataProvider); - emit providerConnected(d->getAddress()); - emit dataProvidersChanged(); - connect(dataProvider, &SensorTagDataProvider::stateChanged, this, &SensorTagDataProviderPool::handleStateChange); + BluetoothDataProvider *dataProvider = static_cast<BluetoothDataProvider *>(findProvider(info.address().toString())); + if (!dataProvider) { + qCDebug(boot2QtDemos) << "Found a new Sensor tag. Name:" << info.name() << ", addr:" << info.address().toString() ; + dataProvider = new BluetoothDataProvider(info.address().toString(), this); + m_dataProviders.append(dataProvider); + emit dataProvidersChanged(); + } + if (!dataProvider->device()) { + qCDebug(boot2QtDemos) << "Attach BluetoothDevice info for an existing Sensor Tag:" << info.name(); + BluetoothDevice *d = new BluetoothDevice(info); + dataProvider->bindToDevice(d); + connect(dataProvider, &SensorTagDataProvider::stateChanged, this, &SensorTagDataProviderPool::handleStateChange); + dataProvider->startDataFetching(); + } + else if (dataProvider->state() != SensorTagDataProvider::Connected) { + qCDebug(boot2QtDemos) << "Start service scan for already attached Sensor Tag" << dataProvider->id(); + dataProvider->startServiceScan(); + } } } } @@ -184,6 +197,14 @@ void SensorTagDataProviderPool::deviceScanError(QBluetoothDeviceDiscoveryAgent:: else qCDebug(boot2QtDemos) << "An unknown error has occurred."; - m_deviceScanState = false; emit scanFinished(); } + +SensorTagDataProvider* SensorTagDataProviderPool::findProvider(QString id) const +{ + for (SensorTagDataProvider *p : m_dataProviders) { + if (id == p->id()) + return p; + } + return 0; +} |