summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Blasche <alexander.blasche@digia.com>2014-06-13 16:28:57 +0200
committerAlex Blasche <alexander.blasche@digia.com>2014-06-18 13:04:43 +0200
commit6014943db4b72962731a90c09a1b476af8385c29 (patch)
treec8cfb326ac900396cd29b45f1653fe3a849f81bc
parent19714b30f9dff135f4b831895ce2ac518554e2c6 (diff)
Shift local Bt adapter validation out of constructor
It can emit error signals which cannot be received while still running the ctor. Add documentation on how this affects the localAddress() function. Change-Id: I144ea7277470f7db498b49952a9525d29c43183a Reviewed-by: Fabian Bumberger <fbumberger@rim.com>
-rw-r--r--src/bluetooth/qlowenergycontrollernew.cpp45
-rw-r--r--src/bluetooth/qlowenergycontrollernew_p.h3
-rw-r--r--tests/auto/qlowenergycontroller/tst_qlowenergycontroller.cpp3
3 files changed, 27 insertions, 24 deletions
diff --git a/src/bluetooth/qlowenergycontrollernew.cpp b/src/bluetooth/qlowenergycontrollernew.cpp
index 62a09342..967b1a87 100644
--- a/src/bluetooth/qlowenergycontrollernew.cpp
+++ b/src/bluetooth/qlowenergycontrollernew.cpp
@@ -71,29 +71,22 @@ void QLowEnergyControllerNewPrivate::setError(
emit q->error(newError);
}
-void QLowEnergyControllerNewPrivate::validateLocalAdapter()
+bool QLowEnergyControllerNewPrivate::isValidLocalAdapter()
{
+ if (localAdapter.isNull())
+ return false;
+
const QList<QBluetoothHostInfo> foundAdapters = QBluetoothLocalDevice::allDevices();
bool adapterFound = false;
- if (!localAdapter.isNull()) {
- foreach (const QBluetoothHostInfo &info, foundAdapters) {
- if (info.address() == localAdapter) {
- adapterFound = true;
- break;
- }
+ foreach (const QBluetoothHostInfo &info, foundAdapters) {
+ if (info.address() == localAdapter) {
+ adapterFound = true;
+ break;
}
-
- if (!adapterFound)
- setError(QLowEnergyControllerNew::InvalidBluetoothAdapterError);
- } else {
- adapterFound = true;
- if (!foundAdapters.isEmpty())
- localAdapter = foundAdapters[0].address();
}
- if (localAdapter.isNull() || !adapterFound)
- setError(QLowEnergyControllerNew::InvalidBluetoothAdapterError);
+ return adapterFound;
}
void QLowEnergyControllerNewPrivate::setState(
@@ -115,8 +108,7 @@ QLowEnergyControllerNew::QLowEnergyControllerNew(
Q_D(QLowEnergyControllerNew);
d->q_ptr = this;
d->remoteDevice = remoteDevice;
-
- d->validateLocalAdapter();
+ d->localAdapter = QBluetoothLocalDevice().address();
}
QLowEnergyControllerNew::QLowEnergyControllerNew(
@@ -129,8 +121,6 @@ QLowEnergyControllerNew::QLowEnergyControllerNew(
d->q_ptr = this;
d->remoteDevice = remoteDevice;
d->localAdapter = localDevice;
-
- d->validateLocalAdapter();
}
QLowEnergyControllerNew::~QLowEnergyControllerNew()
@@ -138,6 +128,16 @@ QLowEnergyControllerNew::~QLowEnergyControllerNew()
delete d_ptr;
}
+/*!
+ Returns the address of the local Bluetooth adapter being used for the
+ communication.
+
+ If this class instance was requested to use the default adapter
+ but there was no default adapter when creating this
+ class instance, the returned \l QBluetoothAddress will be null.
+
+ \sa QBluetoothAddress::isNull()
+ */
QBluetoothAddress QLowEnergyControllerNew::localAddress() const
{
return d_ptr->localAdapter;
@@ -157,6 +157,11 @@ void QLowEnergyControllerNew::connectToDevice()
{
Q_D(QLowEnergyControllerNew);
+ if (!d->isValidLocalAdapter()) {
+ d->setError(QLowEnergyControllerNew::InvalidBluetoothAdapterError);
+ return;
+ }
+
if (state() != QLowEnergyControllerNew::UnconnectedState)
return;
diff --git a/src/bluetooth/qlowenergycontrollernew_p.h b/src/bluetooth/qlowenergycontrollernew_p.h
index d50e6dcc..41824239 100644
--- a/src/bluetooth/qlowenergycontrollernew_p.h
+++ b/src/bluetooth/qlowenergycontrollernew_p.h
@@ -74,7 +74,8 @@ public:
{}
void setError(QLowEnergyControllerNew::Error newError);
- void validateLocalAdapter();
+ bool isValidLocalAdapter();
+
void setState(QLowEnergyControllerNew::ControllerState newState);
void connectToDevice();
diff --git a/tests/auto/qlowenergycontroller/tst_qlowenergycontroller.cpp b/tests/auto/qlowenergycontroller/tst_qlowenergycontroller.cpp
index e0328c56..5792d6d2 100644
--- a/tests/auto/qlowenergycontroller/tst_qlowenergycontroller.cpp
+++ b/tests/auto/qlowenergycontroller/tst_qlowenergycontroller.cpp
@@ -842,9 +842,6 @@ void tst_QLowEnergyController::tst_defaultBehavior()
QCOMPARE(controlDefaultAdapter.remoteAddress(), randomAddress);
QCOMPARE(controlDefaultAdapter.state(), QLowEnergyControllerNew::UnconnectedState);
if (foundAddresses.isEmpty()) {
- QCOMPARE(controlDefaultAdapter.error(),
- QLowEnergyControllerNew::InvalidBluetoothAdapterError);
- QVERIFY(!controlDefaultAdapter.errorString().isEmpty());
QVERIFY(controlDefaultAdapter.localAddress().isNull());
} else {
QCOMPARE(controlDefaultAdapter.error(), QLowEnergyControllerNew::NoError);