diff options
author | Maurice Kalinowski <maurice.kalinowski@qt.io> | 2017-09-20 09:35:56 +0200 |
---|---|---|
committer | Maurice Kalinowski <maurice.kalinowski@qt.io> | 2017-09-27 06:35:24 +0000 |
commit | cf2ec248f2c8d6fabc539f2acef4df5d3c0e8b6c (patch) | |
tree | 01c4feef45ddf310113a7f2f2d5b06e20b443fbe /tradeshow/iot-sensortag/sensortagdataproviderpool.cpp | |
parent | 55ce120e6ceccb40f19a941fbda05edefd22d64c (diff) |
iot-sensortag: Redesign of example
The updated version switches the focus from having multiple sensors
locally towards showing one sensor from a list. This list can display
local or remote sensors and switch on runtime.
Also incorporated many style fixes and cleanups.
Change-Id: If7b8acbb2a784ebba95d467176e76728721c82f7
Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
Diffstat (limited to 'tradeshow/iot-sensortag/sensortagdataproviderpool.cpp')
-rw-r--r-- | tradeshow/iot-sensortag/sensortagdataproviderpool.cpp | 77 |
1 files changed, 25 insertions, 52 deletions
diff --git a/tradeshow/iot-sensortag/sensortagdataproviderpool.cpp b/tradeshow/iot-sensortag/sensortagdataproviderpool.cpp index 987e8b3..3b66d90 100644 --- a/tradeshow/iot-sensortag/sensortagdataproviderpool.cpp +++ b/tradeshow/iot-sensortag/sensortagdataproviderpool.cpp @@ -58,22 +58,23 @@ Q_DECLARE_LOGGING_CATEGORY(boot2QtDemos) SensorTagDataProviderPool::SensorTagDataProviderPool(QObject *parent) : SensorTagDataProviderPool("SensorTag", parent) { - } -SensorTagDataProviderPool::SensorTagDataProviderPool(QString poolName, QObject* parent) +SensorTagDataProviderPool::SensorTagDataProviderPool(QString poolName, QObject *parent) : DataProviderPool(poolName, parent) { m_discoveryAgent = new QBluetoothDeviceDiscoveryAgent(); - connect(m_discoveryAgent, SIGNAL(deviceDiscovered(const QBluetoothDeviceInfo&)), - this, SLOT(btDeviceFound(const QBluetoothDeviceInfo&))); - connect(m_discoveryAgent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error)), - this, SLOT(deviceScanError(QBluetoothDeviceDiscoveryAgent::Error))); - connect(m_discoveryAgent, SIGNAL(finished()), this, SLOT(deviceDiscoveryFinished())); + connect(m_discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, + this, &SensorTagDataProviderPool::btDeviceFound); + connect(m_discoveryAgent, QOverload<QBluetoothDeviceDiscoveryAgent::Error>::of(&QBluetoothDeviceDiscoveryAgent::error), + this, &SensorTagDataProviderPool::deviceScanError); + connect(m_discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, + this, &SensorTagDataProviderPool::deviceDiscoveryFinished); } void SensorTagDataProviderPool::startScanning() { + m_discoveryAgent->setLowEnergyDiscoveryTimeout(0); m_discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod); if (m_discoveryAgent->isActive()) { @@ -81,7 +82,7 @@ void SensorTagDataProviderPool::startScanning() } } -void SensorTagDataProviderPool::disconnectProvider(QString id) +void SensorTagDataProviderPool::disconnectProvider(const QString &id) { SensorTagDataProvider *p = findProvider(id); if (BluetoothDataProvider *btp = qobject_cast<BluetoothDataProvider*>(findProvider(id))) @@ -90,21 +91,6 @@ void SensorTagDataProviderPool::disconnectProvider(QString id) p->setState(SensorTagDataProvider::Disconnected); } -void SensorTagDataProviderPool::setMacFilterList(const QStringList &addressList) -{ - m_macFilters = addressList; -} - -QStringList SensorTagDataProviderPool::macFilters() const -{ - return m_macFilters; -} - -void SensorTagDataProviderPool::setnameFilteList(const QStringList &nameList) -{ - m_nameFilters = nameList; -} - SensorTagDataProvider *SensorTagDataProviderPool::providerForCloud() const { return m_providerForCloud; @@ -136,40 +122,27 @@ void SensorTagDataProviderPool::finishScanning() void SensorTagDataProviderPool::btDeviceFound(const QBluetoothDeviceInfo &info) { if (info.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) { - bool filtered = m_macFilters.length() || m_nameFilters.length(); - bool found = filtered ? false : true; - - if (m_macFilters.length() && m_macFilters.contains(info.address().toString())) - found = true; - else if (m_nameFilters.length() && m_nameFilters.contains(info.name())) - found = true; - - if (found) { - 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(); - } + // all tags by TI have this naming convention + if (info.name().contains(QLatin1String("SensorTag")) && !m_knownDevices.contains(info)) { + qCDebug(boot2QtDemos) << "Found a new device:" << info.name(); + m_knownDevices.append(info); + BluetoothDataProvider *p = new BluetoothDataProvider(info, this); + m_dataProviders.push_back(p); + connect(p, &SensorTagDataProvider::stateChanged, + this, &SensorTagDataProviderPool::handleStateChange); + + emit dataProvidersChanged(); + + // Set initial state to Scanning for UI to be + // able to show "Connecting.." information + p->setState(SensorTagDataProvider::Scanning); } } } void SensorTagDataProviderPool::handleStateChange() { - SensorTagDataProvider *provider = static_cast<SensorTagDataProvider*>(sender()); + SensorTagDataProvider *provider = static_cast<SensorTagDataProvider *>(sender()); switch (provider->state()) { case SensorTagDataProvider::Disconnected: @@ -200,7 +173,7 @@ void SensorTagDataProviderPool::deviceScanError(QBluetoothDeviceDiscoveryAgent:: emit scanFinished(); } -SensorTagDataProvider* SensorTagDataProviderPool::findProvider(QString id) const +SensorTagDataProvider *SensorTagDataProviderPool::findProvider(QString id) const { for (SensorTagDataProvider *p : m_dataProviders) { if (id == p->id()) |