summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Blasche <alexander.blasche@digia.com>2014-07-10 15:47:41 +0200
committerAlex Blasche <alexander.blasche@digia.com>2014-07-16 08:28:46 +0200
commitf399837b3a723af9add6d9ea2d2e988f64f76ae0 (patch)
tree2c2a17ff5c4cfd007af98c98c70a2bf8c5fe8984
parent8bf7e9986e359df46bb351dfb76e4140d9b04da7 (diff)
Add simpler API to retrieve descriptor/characteristic for certain uuid
This new API is mostly syntactic sugar and reduces the amount of code to be written by API users. Change-Id: I51ff1ea706ac97199646d211e39e79c8140ee74b Reviewed-by: Fabian Bumberger <fbumberger@rim.com>
-rw-r--r--examples/bluetooth/heartlistener/heartrate.cpp32
-rw-r--r--src/bluetooth/qlowenergycharacteristic.cpp29
-rw-r--r--src/bluetooth/qlowenergycharacteristic.h2
-rw-r--r--src/bluetooth/qlowenergyservice.cpp25
-rw-r--r--src/bluetooth/qlowenergyservice.h2
-rw-r--r--tests/auto/qlowenergycharacteristic/tst_qlowenergycharacteristic.cpp13
-rw-r--r--tests/auto/qlowenergycontroller/tst_qlowenergycontroller.cpp25
7 files changed, 86 insertions, 42 deletions
diff --git a/examples/bluetooth/heartlistener/heartrate.cpp b/examples/bluetooth/heartlistener/heartrate.cpp
index 996d8a79..f0f4230e 100644
--- a/examples/bluetooth/heartlistener/heartrate.cpp
+++ b/examples/bluetooth/heartlistener/heartrate.cpp
@@ -245,25 +245,21 @@ void HeartRate::serviceStateChanged(QLowEnergyService::ServiceState s)
switch (s) {
case QLowEnergyService::ServiceDiscovered:
{
- const QList<QLowEnergyCharacteristic> chars = m_service->characteristics();
- for (int i = 0; i < chars.count(); i++) {
- const QLowEnergyCharacteristic c = chars[i];
- if (c.uuid() == QBluetoothUuid(QBluetoothUuid::HeartRateMeasurement)) {
- const QList<QLowEnergyDescriptor> descriptors = c.descriptors();
- //enable notification
- for (int j = 0; j < descriptors.count(); j++) {
- const QLowEnergyDescriptor desc = descriptors[j];
- if (desc.type() == QBluetoothUuid::ClientCharacteristicConfiguration) {
- m_service->writeDescriptor(desc, QByteArray("0100"));
- setMessage("Measuring");
- m_notificationDesc = desc;
- m_start = QDateTime::currentDateTime();
- break;
- }
- }
- break;
- }
+ const QLowEnergyCharacteristic hrChar = m_service->characteristic(
+ QBluetoothUuid(QBluetoothUuid::HeartRateMeasurement));
+ if (!hrChar.isValid()) {
+ setMessage("HR Data not found.");
+ break;
+ }
+
+ const QLowEnergyDescriptor m_notificationDesc = hrChar.descriptor(
+ QBluetoothUuid::ClientCharacteristicConfiguration);
+ if (m_notificationDesc.isValid()) {
+ m_service->writeDescriptor(m_notificationDesc, QByteArray("0100"));
+ setMessage("Measuring");
+ m_start = QDateTime::currentDateTime();
}
+
break;
}
default:
diff --git a/src/bluetooth/qlowenergycharacteristic.cpp b/src/bluetooth/qlowenergycharacteristic.cpp
index d1c1a14c..bba5b7a0 100644
--- a/src/bluetooth/qlowenergycharacteristic.cpp
+++ b/src/bluetooth/qlowenergycharacteristic.cpp
@@ -275,8 +275,33 @@ QLowEnergyHandle QLowEnergyCharacteristic::attributeHandle() const
return data->handle;
}
+
+/*!
+ Returns the descriptor with \a uuid; otherwise an invalid \c QLowEnergyDescriptor
+ instance.
+
+ \sa descriptors()
+*/
+QLowEnergyDescriptor QLowEnergyCharacteristic::descriptor(const QBluetoothUuid &uuid) const
+{
+ if (d_ptr.isNull() || !data)
+ return QLowEnergyDescriptor();
+
+ QList<QLowEnergyHandle> descriptorKeys = d_ptr->characteristicList[data->handle].
+ descriptorList.keys();
+ foreach (const QLowEnergyHandle descHandle, descriptorKeys) {
+ if (uuid == d_ptr->characteristicList[data->handle].descriptorList[descHandle].uuid)
+ return QLowEnergyDescriptor(d_ptr, data->handle, descHandle);
+ }
+
+ return QLowEnergyDescriptor();
+}
+
/*!
- Returns the list of characteristic descriptors.
+ Returns the list of descriptors belonging to this characteristic; otherwise
+ an empty list.
+
+ \sa descriptor()
*/
QList<QLowEnergyDescriptor> QLowEnergyCharacteristic::descriptors() const
{
@@ -291,7 +316,7 @@ QList<QLowEnergyDescriptor> QLowEnergyCharacteristic::descriptors() const
std::sort(descriptorKeys.begin(), descriptorKeys.end());
- foreach (QLowEnergyHandle descHandle, descriptorKeys) {
+ foreach (const QLowEnergyHandle descHandle, descriptorKeys) {
QLowEnergyDescriptor descriptor(d_ptr, data->handle, descHandle);
result.append(descriptor);
}
diff --git a/src/bluetooth/qlowenergycharacteristic.h b/src/bluetooth/qlowenergycharacteristic.h
index 51cb70e6..5ed3dc55 100644
--- a/src/bluetooth/qlowenergycharacteristic.h
+++ b/src/bluetooth/qlowenergycharacteristic.h
@@ -87,7 +87,7 @@ public:
QLowEnergyCharacteristic::PropertyTypes properties() const;
QLowEnergyHandle handle() const;
- // TODO: Simplify obtaining descriptor (e.g. via uuid)
+ QLowEnergyDescriptor descriptor(const QBluetoothUuid &uuid) const;
QList<QLowEnergyDescriptor> descriptors() const;
bool isValid() const;
diff --git a/src/bluetooth/qlowenergyservice.cpp b/src/bluetooth/qlowenergyservice.cpp
index 30fe5733..401468f4 100644
--- a/src/bluetooth/qlowenergyservice.cpp
+++ b/src/bluetooth/qlowenergyservice.cpp
@@ -93,12 +93,35 @@ QLowEnergyService::ServiceState QLowEnergyService::state() const
return d_ptr->state;
}
-
QLowEnergyService::ServiceType QLowEnergyService::type() const
{
return d_ptr->type;
}
+/*!
+ Returns the matching characteristic for \a uuid; otherwise an invalid
+ characteristic.
+
+ \sa characteristics()
+*/
+QLowEnergyCharacteristic QLowEnergyService::characteristic(const QBluetoothUuid &uuid) const
+{
+ foreach (const QLowEnergyHandle handle, d_ptr->characteristicList.keys()) {
+ if (d_ptr->characteristicList[handle].uuid == uuid)
+ return QLowEnergyCharacteristic(d_ptr, handle);
+ }
+
+ return QLowEnergyCharacteristic();
+}
+
+/*!
+ Returns all characteristics associated with this \c QLowEnergyService instance.
+
+ The returned list will be empty if this service instance is invalid,
+ \l discoverDetails() was not yet called or there are no known characteristics.
+
+ \sa characteristic(), state(), discoverDetails
+*/
QList<QLowEnergyCharacteristic> QLowEnergyService::characteristics() const
{
diff --git a/src/bluetooth/qlowenergyservice.h b/src/bluetooth/qlowenergyservice.h
index 1250e3a7..fcaf9496 100644
--- a/src/bluetooth/qlowenergyservice.h
+++ b/src/bluetooth/qlowenergyservice.h
@@ -79,7 +79,7 @@ public:
QLowEnergyService::ServiceType type() const;
QLowEnergyService::ServiceState state() const;
- // TODO: Simplify obtaining characteristic (e.g. via uuid)
+ QLowEnergyCharacteristic characteristic(const QBluetoothUuid &uuid) const;
QList<QLowEnergyCharacteristic> characteristics() const;
QBluetoothUuid serviceUuid() const;
QString serviceName() const;
diff --git a/tests/auto/qlowenergycharacteristic/tst_qlowenergycharacteristic.cpp b/tests/auto/qlowenergycharacteristic/tst_qlowenergycharacteristic.cpp
index 27c32db9..c6c408b6 100644
--- a/tests/auto/qlowenergycharacteristic/tst_qlowenergycharacteristic.cpp
+++ b/tests/auto/qlowenergycharacteristic/tst_qlowenergycharacteristic.cpp
@@ -188,6 +188,12 @@ void tst_QLowEnergyCharacteristic::tst_constructionDefault()
QVERIFY(characteristic.handle() == 0);
QCOMPARE(characteristic.name(), QString());
QCOMPARE(characteristic.descriptors().count(), 0);
+ QCOMPARE(characteristic.descriptor(QBluetoothUuid()),
+ QLowEnergyDescriptor());
+ QCOMPARE(characteristic.descriptor(QBluetoothUuid(QBluetoothUuid::ClientCharacteristicConfiguration)),
+ QLowEnergyDescriptor());
+ QCOMPARE(characteristic.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration),
+ QLowEnergyDescriptor());
QCOMPARE(characteristic.properties(), QLowEnergyCharacteristic::Unknown);
QLowEnergyCharacteristic copyConstructed(characteristic);
@@ -244,6 +250,10 @@ void tst_QLowEnergyCharacteristic::tst_assignCompare()
const QList<QLowEnergyCharacteristic> chars = globalService->characteristics();
QVERIFY(!chars.isEmpty());
for (int i = 0; i < chars.count(); i++) {
+ const QLowEnergyCharacteristic specific =
+ globalService->characteristic(chars[i].uuid());
+ QVERIFY(specific.isValid());
+ QCOMPARE(specific, chars[i]);
if (chars[i].descriptors().count() > 0) {
indexWithDescriptor = i;
break;
@@ -290,6 +300,9 @@ void tst_QLowEnergyCharacteristic::tst_assignCompare()
QCOMPARE(target.descriptors()[i].handle(), ref.handle());
QCOMPARE(target.descriptors()[i].uuid(), ref.uuid());
QCOMPARE(target.descriptors()[i].value(), ref.value());
+
+ const QLowEnergyDescriptor ref2 = chars[indexWithDescriptor].descriptor(ref.uuid());
+ QCOMPARE(ref, ref2);
}
// test copy constructor
diff --git a/tests/auto/qlowenergycontroller/tst_qlowenergycontroller.cpp b/tests/auto/qlowenergycontroller/tst_qlowenergycontroller.cpp
index 1fa8b9bf..bb7bd720 100644
--- a/tests/auto/qlowenergycontroller/tst_qlowenergycontroller.cpp
+++ b/tests/auto/qlowenergycontroller/tst_qlowenergycontroller.cpp
@@ -1648,17 +1648,10 @@ void tst_QLowEnergyController::tst_writeDescriptor()
// http://processors.wiki.ti.com/index.php/SensorTag_User_Guide
// 1. Find temperature data characteristic
- const QList<QLowEnergyCharacteristic> chars = service->characteristics();
- QLowEnergyCharacteristic tempData, tempConfig;
- foreach (const QLowEnergyCharacteristic &c, chars) {
- if (c.uuid() ==
- QBluetoothUuid(QStringLiteral("f000aa01-0451-4000-b000-000000000000"))) {
- tempData = c;
- } else if (c.uuid() ==
- QBluetoothUuid(QStringLiteral("f000aa02-0451-4000-b000-000000000000"))) {
- tempConfig = c;
- }
- }
+ const QLowEnergyCharacteristic tempData = service->characteristic(
+ QBluetoothUuid(QStringLiteral("f000aa01-0451-4000-b000-000000000000")));
+ const QLowEnergyCharacteristic tempConfig = service->characteristic(
+ QBluetoothUuid(QStringLiteral("f000aa02-0451-4000-b000-000000000000")));
if (!tempData.isValid()) {
delete service;
@@ -1667,14 +1660,8 @@ void tst_QLowEnergyController::tst_writeDescriptor()
}
// 2. Find temperature data notification descriptor
- const QList<QLowEnergyDescriptor> descs = tempData.descriptors();
- QLowEnergyDescriptor notification;
- foreach (const QLowEnergyDescriptor &d, descs) {
- if (d.type() == QBluetoothUuid::ClientCharacteristicConfiguration) {
- notification = d;
- break;
- }
- }
+ const QLowEnergyDescriptor notification = tempData.descriptor(
+ QBluetoothUuid(QBluetoothUuid::ClientCharacteristicConfiguration));
if (!notification.isValid()) {
delete service;