summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorNedim Hadzic <nhadzic@blackberry.com>2014-03-09 20:56:12 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-03-11 13:26:09 +0100
commit085db46d31bb5010a8486b73543d01f183b684e1 (patch)
treecbdd84273cf29d379a9026d3c9d9e89e1b376736 /src
parent2f133076fb91ecadadef4a8e28cd6c5d9b431f71 (diff)
BLE characteristic notification moved to controller class
Enable and disable notification communication moved to the QLowEnergyController class. Change-Id: I34ceb10a5dec067133399257d544dd56db7e626e Reviewed-by: Alex Blasche <alexander.blasche@digia.com>
Diffstat (limited to 'src')
-rw-r--r--src/bluetooth/qlowenergycharacteristicinfo_bluez.cpp108
-rw-r--r--src/bluetooth/qlowenergycharacteristicinfo_p.h5
-rw-r--r--src/bluetooth/qlowenergycontroller.cpp22
-rw-r--r--src/bluetooth/qlowenergycontroller_bluez.cpp89
-rw-r--r--src/bluetooth/qlowenergycontroller_p.cpp11
-rw-r--r--src/bluetooth/qlowenergycontroller_p.h3
6 files changed, 115 insertions, 123 deletions
diff --git a/src/bluetooth/qlowenergycharacteristicinfo_bluez.cpp b/src/bluetooth/qlowenergycharacteristicinfo_bluez.cpp
index bcfd898e..a9daa445 100644
--- a/src/bluetooth/qlowenergycharacteristicinfo_bluez.cpp
+++ b/src/bluetooth/qlowenergycharacteristicinfo_bluez.cpp
@@ -67,67 +67,6 @@ QLowEnergyCharacteristicInfoPrivate::~QLowEnergyCharacteristicInfoPrivate()
delete characteristic;
}
-void QLowEnergyCharacteristicInfoPrivate::replyReceived(const QString &reply)
-{
- QStringList update, row;
- update = QStringList();
- row = QStringList();
- if (reply.contains(QStringLiteral("[CON]"))) {
-#ifdef QT_LOWENERGYCHARACTERISTIC_DEBUG
- qDebug() << "Char connected" << t;
-#endif
- }
- if (reply.contains(QStringLiteral("[CON]")) && t == 1) {
- QString command = QStringLiteral("char-read-hnd ") + handle;
- process->executeCommand(command);
- process->executeCommand(QStringLiteral("\n"));
- t++;
- }
- if (reply.contains(QStringLiteral("Notification handle"))) {
- update = reply.split(QStringLiteral("\n"));
-#ifdef QT_LOWENERGYCHARACTERISTIC_DEBUG
- qDebug() << update.size();
-#endif
- for (int i = 0; i< update.size(); i ++) {
- if (update.at(i).contains(QStringLiteral("Notification handle"))) {
- row = update.at(i).split(QRegularExpression(QStringLiteral("\\W+")), QString::SkipEmptyParts);
-#ifdef QT_LOWENERGYCHARACTERISTIC_DEBUG
- qDebug() << "Handle : "<< handle << row;
-#endif
- if (row.at(2) == handle) {
- QString notificationValue = QStringLiteral("");
- for (int j = 4 ; j< row.size(); j++)
- notificationValue += row.at(j);
-#ifdef QT_LOWENERGYCHARACTERISTIC_DEBUG
- qDebug() << notificationValue;
-#endif
- value = notificationValue.toUtf8();
- properties[QStringLiteral("value")] = value;
- emit notifyValue(uuid);
- }
- }
-
- }
- }
-
- if (reply.contains(QStringLiteral("Characteristic value/descriptor:"))) {
- update = reply.split(QStringLiteral("\n"));
- for (int i = 0; i< update.size(); i ++) {
- if (update.at(i).contains(QStringLiteral("Characteristic value/descriptor:"))) {
- row = update.at(i).split(QRegularExpression(QStringLiteral("\\W+")), QString::SkipEmptyParts);
- QString val = QStringLiteral("");
- for ( int j = 3; j<row.size(); j++)
- val += row.at(j);
-#ifdef QT_LOWENERGYCHARACTERISTIC_DEBUG
- qDebug() << "HANDLE: " << uuid << val;
-#endif
- value = val.toUtf8();
- properties[QStringLiteral("value")] = value;
- }
- }
- }
-}
-
void QLowEnergyCharacteristicInfoPrivate::setValue(const QByteArray &wantedValue)
{
if (permission & QLowEnergyCharacteristicInfo::Write || notification) {
@@ -157,43 +96,6 @@ void QLowEnergyCharacteristicInfoPrivate::setValue(const QByteArray &wantedValue
}
}
-bool QLowEnergyCharacteristicInfoPrivate::enableNotification()
-{
- if (!notification)
- return false;
- /*
- * Wanted value to enable notifications is 0100
- */
- if ( (permission & QLowEnergyCharacteristicInfo::Notify) == 0) {
-#ifdef QT_LOWENERGYCHARACTERISTIC_DEBUG
- qDebug() << "Notification changes not allowed";
-#endif
- errorString = QStringLiteral("This characteristic does not support notifications.");
- emit error(uuid);
- return false;
- }
- QByteArray val;
- val.append(48);
- val.append(49);
- val.append(48);
- val.append(48);
- setValue(val);
- return true;
-}
-
-void QLowEnergyCharacteristicInfoPrivate::disableNotification()
-{
- /*
- * Wanted value to disable notifications is 0000
- */
- QByteArray val;
- val.append(48);
- val.append(48);
- val.append(48);
- val.append(48);
- setValue(val);
-}
-
void QLowEnergyCharacteristicInfoPrivate::readDescriptors()
{
@@ -211,4 +113,14 @@ bool QLowEnergyCharacteristicInfoPrivate::valid()
return true;
}
+bool QLowEnergyCharacteristicInfoPrivate::enableNotification()
+{
+ return false;
+}
+
+void QLowEnergyCharacteristicInfoPrivate::disableNotification()
+{
+
+}
+
QT_END_NAMESPACE
diff --git a/src/bluetooth/qlowenergycharacteristicinfo_p.h b/src/bluetooth/qlowenergycharacteristicinfo_p.h
index 8fb23ad4..59353d5f 100644
--- a/src/bluetooth/qlowenergycharacteristicinfo_p.h
+++ b/src/bluetooth/qlowenergycharacteristicinfo_p.h
@@ -73,9 +73,9 @@ public:
void setValue(const QByteArray &wantedValue);
void readValue();
bool valid();
+ void readDescriptors();
bool enableNotification();
void disableNotification();
- void readDescriptors();
QString name;
QBluetoothUuid uuid;
@@ -98,9 +98,6 @@ public:
QString path;
int t;
QString startingHandle;
-public Q_SLOTS:
-
- void replyReceived(const QString &reply);
#endif
Q_SIGNALS:
diff --git a/src/bluetooth/qlowenergycontroller.cpp b/src/bluetooth/qlowenergycontroller.cpp
index 21daa650..07b45766 100644
--- a/src/bluetooth/qlowenergycontroller.cpp
+++ b/src/bluetooth/qlowenergycontroller.cpp
@@ -162,17 +162,7 @@ void QLowEnergyController::disconnectFromService(const QLowEnergyServiceInfo &le
*/
bool QLowEnergyController::enableNotifications(const QLowEnergyCharacteristicInfo &characteristic)
{
- bool enable = false;
- for (int i = 0; i < d_ptr->m_leServices.size(); i++) {
- for (int j = 0; j < d_ptr->m_leServices.at(i).characteristics().size(); j++) {
- if (d_ptr->m_leServices.at(i).characteristics().at(j).uuid() == characteristic.uuid()) {
- connect(d_ptr->m_leServices.at(i).characteristics().at(j).d_ptr.data(), SIGNAL(notifyValue(QBluetoothUuid)), this, SLOT(_q_valueReceived(QBluetoothUuid)));
- connect(d_ptr->m_leServices.at(i).characteristics().at(j).d_ptr.data(), SIGNAL(error(QBluetoothUuid)), this, SLOT(_q_characteristicError(QBluetoothUuid)));
- enable = d_ptr->m_leServices.at(i).characteristics().at(j).d_ptr->enableNotification();
- }
- }
- }
- return enable;
+ return d_ptr->enableNotification(characteristic);
}
/*!
@@ -183,15 +173,7 @@ bool QLowEnergyController::enableNotifications(const QLowEnergyCharacteristicInf
*/
void QLowEnergyController::disableNotifications(const QLowEnergyCharacteristicInfo &characteristic)
{
- for (int i = 0; i < d_ptr->m_leServices.size(); i++) {
- for (int j = 0; j < d_ptr->m_leServices.at(i).characteristics().size(); j++) {
- if (d_ptr->m_leServices.at(i).characteristics().at(j).uuid() == characteristic.uuid()){
- disconnect(d_ptr->m_leServices.at(i).characteristics().at(j).d_ptr.data(), SIGNAL(notifyValue(QBluetoothUuid)), this, SLOT(_q_valueReceived(QBluetoothUuid)));
- disconnect(d_ptr->m_leServices.at(i).characteristics().at(j).d_ptr.data(), SIGNAL(error(QBluetoothUuid)), this, SLOT(_q_characteristicError(QBluetoothUuid)));
- d_ptr->m_leServices.at(i).characteristics().at(j).d_ptr->disableNotification();
- }
- }
- }
+ d_ptr->disableNotification(characteristic);
}
/*!
diff --git a/src/bluetooth/qlowenergycontroller_bluez.cpp b/src/bluetooth/qlowenergycontroller_bluez.cpp
index 7a6aeece..6b4c091e 100644
--- a/src/bluetooth/qlowenergycontroller_bluez.cpp
+++ b/src/bluetooth/qlowenergycontroller_bluez.cpp
@@ -177,7 +177,7 @@ void QLowEnergyControllerPrivate::_q_replyReceived(const QString &reply)
{
qCDebug(QT_BT_BLUEZ) << "reply: " << reply;
// STEP 0
- if (m_step == 0)
+ if (m_step == 0 && !m_leServices.isEmpty())
connectToTerminal();
if (reply.contains(QStringLiteral("Connection refused (111)"))) {
errorString = QStringLiteral("Connection refused (111)");
@@ -338,6 +338,31 @@ void QLowEnergyControllerPrivate::_q_replyReceived(const QString &reply)
}
}
}
+
+ // READING ADVERTISEMENT FROM THE BLE DEVICE
+ if (reply.contains(QStringLiteral("Notification handle"))) {
+ QStringList update, row;
+ update = reply.split(QStringLiteral("\n"));
+ for (int i = 0; i< update.size(); i ++) {
+ if (update.at(i).contains(QStringLiteral("Notification handle"))) {
+ row = update.at(i).split(QRegularExpression(QStringLiteral("\\W+")), QString::SkipEmptyParts);
+ for (int j = 0; j < m_leServices.size(); j++) {
+ for (int k = 0; k < m_leServices.at(j).characteristics().size(); k++) {
+ if (m_leServices.at(j).characteristics().at(k).handle() == row.at(2)) {
+
+ QString notificationValue = QStringLiteral("");
+ for (int s = 4 ; s< row.size(); s++)
+ notificationValue += row.at(s);
+ m_leServices.at(j).characteristics().at(k).d_ptr->value = notificationValue.toUtf8();
+ m_leServices.at(j).characteristics().at(k).d_ptr->properties[QStringLiteral("value")] = notificationValue.toUtf8();
+ emit q_ptr->valueChanged(m_leServices.at(j).characteristics().at(k));
+ }
+ }
+ }
+ }
+
+ }
+ }
}
}
@@ -402,4 +427,66 @@ void QLowEnergyControllerPrivate::readCharacteristicValue(int index)
m_leServices.at(index).d_ptr->m_step++;
}
+void QLowEnergyControllerPrivate::writeValue(const QString &handle, const QByteArray &value)
+{
+ process = process->instance();
+ QString command = QStringLiteral("char-write-req ") + handle + QStringLiteral(" ") + QString::fromLocal8Bit(value.constData());
+ process->executeCommand(command);
+ process->executeCommand(QStringLiteral("\n"));
+
+}
+
+bool QLowEnergyControllerPrivate::enableNotification(const QLowEnergyCharacteristicInfo &characteristic)
+{
+ bool foundCharacteristic = false;
+ bool foundDescriptor = false;
+ const QBluetoothUuid descUuid((ushort)0x2902);
+ for (int i = 0; i < m_leServices.size(); i++) {
+ for (int j = 0; j < m_leServices.at(i).characteristics().size(); j++) {
+ if (m_leServices.at(i).characteristics().at(j).uuid() == characteristic.uuid()) {
+ foundCharacteristic = true;
+ for (int k = 0; k < m_leServices.at(i).characteristics().at(j).descriptors().size(); k++) {
+ if (m_leServices.at(i).characteristics().at(j).descriptors().at(k).uuid() == descUuid){
+ foundDescriptor = true;
+ QByteArray val;
+ val.append(48);
+ val.append(49);
+ val.append(48);
+ val.append(48);
+ writeValue(m_leServices.at(i).characteristics().at(j).descriptors().at(k).handle(), val);
+ return true;
+ }
+ }
+ }
+ }
+ }
+ if (!foundDescriptor || !foundCharacteristic) {
+ errorString = QStringLiteral("Characteristic or notification descriptor not found.");
+ emit q_ptr->error(characteristic);
+ return false;
+ }
+
+}
+
+void QLowEnergyControllerPrivate::disableNotification(const QLowEnergyCharacteristicInfo &characteristic)
+{
+ const QBluetoothUuid descUuid((ushort)0x2902);
+ for (int i = 0; i < m_leServices.size(); i++) {
+ for (int j = 0; j < m_leServices.at(i).characteristics().size(); j++) {
+ if (m_leServices.at(i).characteristics().at(j).uuid() == characteristic.uuid()) {
+ for (int k = 0; k < m_leServices.at(i).characteristics().at(j).descriptors().size(); k++) {
+ if (m_leServices.at(i).characteristics().at(j).descriptors().at(k).uuid() == descUuid){
+ QByteArray val;
+ val.append(48);
+ val.append(48);
+ val.append(48);
+ val.append(48);
+ writeValue(m_leServices.at(i).characteristics().at(j).descriptors().at(k).handle(), val);
+ }
+ }
+ }
+ }
+ }
+}
+
QT_END_NAMESPACE
diff --git a/src/bluetooth/qlowenergycontroller_p.cpp b/src/bluetooth/qlowenergycontroller_p.cpp
index 0595a059..da3b6bc9 100644
--- a/src/bluetooth/qlowenergycontroller_p.cpp
+++ b/src/bluetooth/qlowenergycontroller_p.cpp
@@ -94,4 +94,15 @@ void QLowEnergyControllerPrivate::_q_valueReceived(const QBluetoothUuid &uuid)
Q_UNUSED(uuid);
}
+bool QLowEnergyControllerPrivate::enableNotification(const QLowEnergyCharacteristicInfo &characteristic)
+{
+ Q_UNUSED(characteristic);
+ return false;
+}
+
+void QLowEnergyControllerPrivate::disableNotification(const QLowEnergyCharacteristicInfo &characteristic)
+{
+ Q_UNUSED(characteristic);
+}
+
QT_END_NAMESPACE
diff --git a/src/bluetooth/qlowenergycontroller_p.h b/src/bluetooth/qlowenergycontroller_p.h
index b0e07587..8ca1534b 100644
--- a/src/bluetooth/qlowenergycontroller_p.h
+++ b/src/bluetooth/qlowenergycontroller_p.h
@@ -55,6 +55,8 @@ public:
~QLowEnergyControllerPrivate();
void connectService(const QLowEnergyServiceInfo &service);
void disconnectService(const QLowEnergyServiceInfo &leService = QLowEnergyServiceInfo());
+ bool enableNotification(const QLowEnergyCharacteristicInfo &characteristic);
+ void disableNotification(const QLowEnergyCharacteristicInfo &characteristic);
void _q_serviceConnected(const QBluetoothUuid &uuid);
void _q_serviceError(const QBluetoothUuid &uuid);
@@ -72,6 +74,7 @@ public:
void setCharacteristics(int);
void setNotifications();
void readCharacteristicValue(int);
+ void writeValue(const QString &, const QByteArray &);
public slots:
void _q_replyReceived(const QString &reply);
#endif