From 9e3eff1c3a2b7105fecf6d0d7f052eb9943fbada Mon Sep 17 00:00:00 2001 From: "Javier S. Pedro" Date: Tue, 14 May 2013 01:57:34 +0200 Subject: perform only one bluetooth discovery for all watches --- libsowatchbt/bluetoothwatchscanner.cpp | 41 +++++++++++++++++++++++----------- libsowatchbt/bluetoothwatchscanner.h | 7 +++--- 2 files changed, 31 insertions(+), 17 deletions(-) (limited to 'libsowatchbt') diff --git a/libsowatchbt/bluetoothwatchscanner.cpp b/libsowatchbt/bluetoothwatchscanner.cpp index eeae5f7..192da0d 100644 --- a/libsowatchbt/bluetoothwatchscanner.cpp +++ b/libsowatchbt/bluetoothwatchscanner.cpp @@ -6,26 +6,41 @@ QTM_USE_NAMESPACE using namespace sowatch; + +int BluetoothWatchScanner::_instances = 0; +QBluetoothServiceDiscoveryAgent *BluetoothWatchScanner::_agent = 0; + BluetoothWatchScanner::BluetoothWatchScanner(QObject *parent) : - WatchScanner(parent), - _agent(new QBluetoothServiceDiscoveryAgent(this)) + WatchScanner(parent) { - connect(_agent, SIGNAL(finished()), this, SIGNAL(finished())); - connect(_agent, SIGNAL(serviceDiscovered(QBluetoothServiceInfo)), - this, SLOT(handleDiscoveredService(QBluetoothServiceInfo))); + if (_instances == 0) { + Q_ASSERT(!_agent); + _agent = new QBluetoothServiceDiscoveryAgent; + } + _instances++; + Q_ASSERT(_agent); } -void BluetoothWatchScanner::start() +BluetoothWatchScanner::~BluetoothWatchScanner() { - if (_agent->isActive()) { - _agent->stop(); + _instances--; + if (_instances == 0) { + delete _agent; + _agent = 0; } - _agent->start(); - qDebug() << "started bluetooth scan"; - emit started(); } -void BluetoothWatchScanner::setUuidFilter(const QBluetoothUuid & uuid) +void BluetoothWatchScanner::start() { - _agent->setUuidFilter(uuid); + Q_ASSERT(_agent); + connect(_agent, SIGNAL(finished()), this, SIGNAL(finished())); + connect(_agent, SIGNAL(serviceDiscovered(QBluetoothServiceInfo)), + this, SLOT(handleDiscoveredService(QBluetoothServiceInfo))); + + if (!_agent->isActive()) { + qDebug() << "started bluetooth scan"; + _agent->start(); + } + + emit started(); } diff --git a/libsowatchbt/bluetoothwatchscanner.h b/libsowatchbt/bluetoothwatchscanner.h index 1ab974a..e2d78ef 100644 --- a/libsowatchbt/bluetoothwatchscanner.h +++ b/libsowatchbt/bluetoothwatchscanner.h @@ -19,17 +19,16 @@ class SOWATCHBT_EXPORT BluetoothWatchScanner : public WatchScanner public: BluetoothWatchScanner(QObject *parent); + ~BluetoothWatchScanner(); void start(); -protected: - void setUuidFilter(const QBluetoothUuid & uuid); - protected slots: virtual void handleDiscoveredService(const QBluetoothServiceInfo& info) = 0; private: - QBluetoothServiceDiscoveryAgent *_agent; + static int _instances; + static QBluetoothServiceDiscoveryAgent *_agent; }; } -- cgit v1.2.3