summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorNedim Hadzic <nhadzic@blackberry.com>2014-03-11 14:11:37 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-03-13 11:56:29 +0100
commit4439541b05ae88a30159a5597d40f569328783ca (patch)
tree12db0e45d1987503efb79b7f2d0967ca5d302f78 /src
parent085db46d31bb5010a8486b73543d01f183b684e1 (diff)
Bluez BLE write characteristic and descriptor value moved to controller
In the QLowEnergyController class write options for characteristic and descriptors are added. Change-Id: I5cc5dab1d3d2ef75d99eae045959f70524e65c91 Reviewed-by: Alex Blasche <alexander.blasche@digia.com>
Diffstat (limited to 'src')
-rw-r--r--src/bluetooth/qlowenergycharacteristicinfo.cpp10
-rw-r--r--src/bluetooth/qlowenergycharacteristicinfo.h2
-rw-r--r--src/bluetooth/qlowenergycharacteristicinfo_bluez.cpp31
-rw-r--r--src/bluetooth/qlowenergycontroller.cpp24
-rw-r--r--src/bluetooth/qlowenergycontroller.h3
-rw-r--r--src/bluetooth/qlowenergycontroller_bluez.cpp26
-rw-r--r--src/bluetooth/qlowenergycontroller_p.cpp12
-rw-r--r--src/bluetooth/qlowenergycontroller_p.h2
-rw-r--r--src/bluetooth/qlowenergydescriptorinfo.cpp13
-rw-r--r--src/bluetooth/qlowenergydescriptorinfo.h3
10 files changed, 90 insertions, 36 deletions
diff --git a/src/bluetooth/qlowenergycharacteristicinfo.cpp b/src/bluetooth/qlowenergycharacteristicinfo.cpp
index c70e74b6..66994131 100644
--- a/src/bluetooth/qlowenergycharacteristicinfo.cpp
+++ b/src/bluetooth/qlowenergycharacteristicinfo.cpp
@@ -252,14 +252,14 @@ bool QLowEnergyCharacteristicInfo::isNotificationCharacteristic() const
}
/*!
- Writes the value \a value directly to LE device. If the value was not written successfully
- an error will be emitted with an error string.
+ Sets the value \a value of the characteristic. This only caches the value. To write
+ a value directly to the device QLowEnergyController class must be used.
- \sa errorString()
+ \sa QLowEnergyController::writeCharacteristic()
*/
-void QLowEnergyCharacteristicInfo::writeValue(const QByteArray &value)
+void QLowEnergyCharacteristicInfo::setValue(const QByteArray &value)
{
- d_ptr->setValue(value);
+ d_ptr->value = value;
}
/*!
diff --git a/src/bluetooth/qlowenergycharacteristicinfo.h b/src/bluetooth/qlowenergycharacteristicinfo.h
index b655f4bf..ceaac46e 100644
--- a/src/bluetooth/qlowenergycharacteristicinfo.h
+++ b/src/bluetooth/qlowenergycharacteristicinfo.h
@@ -82,7 +82,7 @@ public:
QBluetoothUuid uuid() const;
- void writeValue(const QByteArray &value);
+ void setValue(const QByteArray &value);
QByteArray value() const;
int permissions() const;
diff --git a/src/bluetooth/qlowenergycharacteristicinfo_bluez.cpp b/src/bluetooth/qlowenergycharacteristicinfo_bluez.cpp
index a9daa445..a6592186 100644
--- a/src/bluetooth/qlowenergycharacteristicinfo_bluez.cpp
+++ b/src/bluetooth/qlowenergycharacteristicinfo_bluez.cpp
@@ -69,31 +69,8 @@ QLowEnergyCharacteristicInfoPrivate::~QLowEnergyCharacteristicInfoPrivate()
void QLowEnergyCharacteristicInfoPrivate::setValue(const QByteArray &wantedValue)
{
- if (permission & QLowEnergyCharacteristicInfo::Write || notification) {
- process = process->instance();
- if (!m_signalConnected) {
- connect(process, SIGNAL(replySend(const QString &)), this, SLOT(replyReceived(const QString &)));
- m_signalConnected = true;
- }
- value = wantedValue;
- QString command;
- if (notification == true)
- command = QStringLiteral("char-write-req ") + notificationHandle + QStringLiteral(" ") + QString::fromLocal8Bit(value.constData());
- else
- command = QStringLiteral("char-write-req ") + handle + QStringLiteral(" ") + QString::fromLocal8Bit(value.constData());
-
-
- #ifdef QT_LOWENERGYCHARACTERISTIC_DEBUG
- qDebug() << command << t << process;
- #endif
- process->executeCommand(command);
- process->executeCommand(QStringLiteral("\n"));
- t++;
- }
- else {
- errorString = QStringLiteral("This characteristic does not support write operations.");
- emit error(uuid);
- }
+ Q_UNUSED(wantedValue);
+
}
void QLowEnergyCharacteristicInfoPrivate::readDescriptors()
@@ -103,9 +80,7 @@ void QLowEnergyCharacteristicInfoPrivate::readDescriptors()
void QLowEnergyCharacteristicInfoPrivate::readValue()
{
- QString command = QStringLiteral("char-read-hnd ") + handle;
- process->executeCommand(command);
- process->executeCommand(QStringLiteral("\n"));
+
}
bool QLowEnergyCharacteristicInfoPrivate::valid()
diff --git a/src/bluetooth/qlowenergycontroller.cpp b/src/bluetooth/qlowenergycontroller.cpp
index 07b45766..16571a18 100644
--- a/src/bluetooth/qlowenergycontroller.cpp
+++ b/src/bluetooth/qlowenergycontroller.cpp
@@ -195,4 +195,28 @@ void QLowEnergyController::setRandomAddress()
d_ptr->m_randomAddress = true;
}
+/*!
+ This method writes the wanted \a characteristic taking its value. This value is written directly
+ to the Bluetooth Low Energy device. In case wanted characteristic is not connected or does not
+ have write permission, it will return false with the corresponding error string.
+
+ \sa QLowEnergyCharacteristicInfo::setValue(), errorString(), error()
+ */
+bool QLowEnergyController::writeCharacteristic(const QLowEnergyCharacteristicInfo &characteristic)
+{
+ return d_ptr->write(characteristic);
+}
+
+/*!
+ This method writes the wanted \a descriptor taking its value. This value is written directly
+ to the Bluetooth Low Energy device. In case wanted descriptor is not connected it will return
+ false with the corresponding error string.
+
+ \sa QLowEnergyDescriptorInfo::setValue(), errorString(), error()
+ */
+bool QLowEnergyController::writeDescriptor(const QLowEnergyDescriptorInfo &descriptor)
+{
+ return d_ptr->write(descriptor);
+}
+
QT_END_NAMESPACE
diff --git a/src/bluetooth/qlowenergycontroller.h b/src/bluetooth/qlowenergycontroller.h
index 5b2ae1a6..dcef728e 100644
--- a/src/bluetooth/qlowenergycontroller.h
+++ b/src/bluetooth/qlowenergycontroller.h
@@ -47,6 +47,7 @@
#include <QtBluetooth/QBluetoothAddress>
#include <QtBluetooth/QLowEnergyCharacteristicInfo>
#include <QtBluetooth/QLowEnergyServiceInfo>
+#include <QtBluetooth/QLowEnergyDescriptorInfo>
QT_BEGIN_NAMESPACE
@@ -64,6 +65,8 @@ public:
void disconnectFromService(const QLowEnergyServiceInfo &leService = QLowEnergyServiceInfo());
bool enableNotifications(const QLowEnergyCharacteristicInfo &characteristic);
void disableNotifications(const QLowEnergyCharacteristicInfo &characteristic);
+ bool writeCharacteristic(const QLowEnergyCharacteristicInfo &characteristic);
+ bool writeDescriptor(const QLowEnergyDescriptorInfo &descriptor);
QString errorString() const;
void setRandomAddress();
diff --git a/src/bluetooth/qlowenergycontroller_bluez.cpp b/src/bluetooth/qlowenergycontroller_bluez.cpp
index 6b4c091e..042ef5f4 100644
--- a/src/bluetooth/qlowenergycontroller_bluez.cpp
+++ b/src/bluetooth/qlowenergycontroller_bluez.cpp
@@ -489,4 +489,30 @@ void QLowEnergyControllerPrivate::disableNotification(const QLowEnergyCharacteri
}
}
+bool QLowEnergyControllerPrivate::write(const QLowEnergyCharacteristicInfo &characteristic)
+{
+ if (process->isConnected() && characteristic.isValid()) {
+ if (QLowEnergyCharacteristicInfo::Write & characteristic.permissions()) {
+ writeValue(characteristic.handle(), characteristic.value());
+ return true;
+ } else {
+ errorString = QStringLiteral("This characteristic does not support write operations.");
+ emit q_ptr->error(characteristic);
+ return false;
+ }
+ } else {
+ errorString = QStringLiteral("The device is not connected or characteristic is not valid");
+ emit q_ptr->error(characteristic);
+ return false;
+ }
+}
+
+bool QLowEnergyControllerPrivate::write(const QLowEnergyDescriptorInfo &descriptor)
+{
+ if (process->isConnected()) {
+ writeValue(descriptor.handle(), descriptor.value());
+ return true;
+ }
+}
+
QT_END_NAMESPACE
diff --git a/src/bluetooth/qlowenergycontroller_p.cpp b/src/bluetooth/qlowenergycontroller_p.cpp
index da3b6bc9..66230585 100644
--- a/src/bluetooth/qlowenergycontroller_p.cpp
+++ b/src/bluetooth/qlowenergycontroller_p.cpp
@@ -105,4 +105,16 @@ void QLowEnergyControllerPrivate::disableNotification(const QLowEnergyCharacteri
Q_UNUSED(characteristic);
}
+bool QLowEnergyControllerPrivate::write(const QLowEnergyCharacteristicInfo &characteristic)
+{
+ Q_UNUSED(characteristic);
+ return false;
+}
+
+bool QLowEnergyControllerPrivate::write(const QLowEnergyDescriptorInfo &descriptor)
+{
+ Q_UNUSED(descriptor);
+ return false;
+}
+
QT_END_NAMESPACE
diff --git a/src/bluetooth/qlowenergycontroller_p.h b/src/bluetooth/qlowenergycontroller_p.h
index 8ca1534b..826f88c1 100644
--- a/src/bluetooth/qlowenergycontroller_p.h
+++ b/src/bluetooth/qlowenergycontroller_p.h
@@ -57,6 +57,8 @@ public:
void disconnectService(const QLowEnergyServiceInfo &leService = QLowEnergyServiceInfo());
bool enableNotification(const QLowEnergyCharacteristicInfo &characteristic);
void disableNotification(const QLowEnergyCharacteristicInfo &characteristic);
+ bool write(const QLowEnergyCharacteristicInfo &characteristic);
+ bool write(const QLowEnergyDescriptorInfo &descriptor);
void _q_serviceConnected(const QBluetoothUuid &uuid);
void _q_serviceError(const QBluetoothUuid &uuid);
diff --git a/src/bluetooth/qlowenergydescriptorinfo.cpp b/src/bluetooth/qlowenergydescriptorinfo.cpp
index 9e5e0ba3..f18276ba 100644
--- a/src/bluetooth/qlowenergydescriptorinfo.cpp
+++ b/src/bluetooth/qlowenergydescriptorinfo.cpp
@@ -140,7 +140,7 @@ QString QLowEnergyDescriptorInfo::handle() const
/*!
Returns the value of the descriptor.
*/
-QByteArray QLowEnergyDescriptorInfo::value()
+QByteArray QLowEnergyDescriptorInfo::value() const
{
return d_ptr->m_value;
}
@@ -161,4 +161,15 @@ QString QLowEnergyDescriptorInfo::name() const
return d_ptr->m_name;
}
+/*!
+ Sets the value \a value of the descriptor. This only caches the value. To write
+ a value directly to the device QLowEnergyController class must be used.
+
+ \sa QLowEnergyController::writeDescriptor()
+*/
+void QLowEnergyDescriptorInfo::setValue(const QByteArray &value)
+{
+ d_ptr->m_value = value;
+}
+
QT_END_NAMESPACE
diff --git a/src/bluetooth/qlowenergydescriptorinfo.h b/src/bluetooth/qlowenergydescriptorinfo.h
index 4ee6c983..925ea122 100644
--- a/src/bluetooth/qlowenergydescriptorinfo.h
+++ b/src/bluetooth/qlowenergydescriptorinfo.h
@@ -62,11 +62,12 @@ public:
~QLowEnergyDescriptorInfo();
QLowEnergyDescriptorInfo &operator=(const QLowEnergyDescriptorInfo &other);
- QByteArray value();
+ QByteArray value() const;
QBluetoothUuid uuid() const;
QString handle() const;
QVariantMap properties() const;
QString name() const;
+ void setValue(const QByteArray &value);
private:
QSharedPointer<QLowEnergyDescriptorInfoPrivate> d_ptr;