summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorAlex Blasche <alexander.blasche@digia.com>2014-02-24 16:58:30 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-03-06 08:38:27 +0100
commit9e1fa32246f2524d342ca5d0d53c0c8bcd79bd4c (patch)
tree2b4496b577d434b3725d82f5387b555cd2d0258b /examples
parent55e1d153fab9ceb198ab73f2e4d58fc8bc720bd7 (diff)
Fix 16 bit mode for heart rate measurement
At the same time the code has been streamlined to enhace its visual clarity (the section is directly used in the documentation) Change-Id: I9a126c6f7b412fc0918d45d308540651cb105f0e Reviewed-by: Nedim Hadzic <nedimhadzija@gmail.com> Reviewed-by: Alex Blasche <alexander.blasche@digia.com>
Diffstat (limited to 'examples')
-rw-r--r--examples/bluetooth/heartlistener/heartrate.cpp46
-rw-r--r--examples/bluetooth/heartlistener/heartrate.h2
2 files changed, 23 insertions, 25 deletions
diff --git a/examples/bluetooth/heartlistener/heartrate.cpp b/examples/bluetooth/heartlistener/heartrate.cpp
index 1a4e3d47..a1f80121 100644
--- a/examples/bluetooth/heartlistener/heartrate.cpp
+++ b/examples/bluetooth/heartlistener/heartrate.cpp
@@ -206,34 +206,33 @@ void HeartRate::receiveMeasurement(const QLowEnergyCharacteristicInfo &character
qint16 energy_expended = 0;
int flags = 0;
int index = 0;
+
+ //8 bit flags
val[0] = m_heartRateCharacteristic.value().at(index++);
- // Each Heart Belt has its own settings and fetarues, besides heart rate measurement
+ val[1] = m_heartRateCharacteristic.value().at(index++);
+ flags = val.toUInt(0, 16);
+
+ // Each Heart Belt has its own settings and features, besides heart rate measurement
// By checking the flags we can determine whether it has energy feature. We will go through the array
// of the characters in the characteristic value.
- if (val.toUInt(0, 16) > 3)
- flags = val.toUInt(0, 16);
- else {
- val[1] = m_heartRateCharacteristic.value().at(index++);
- flags = val.toUInt(0, 16);
+ // The following flags are used to determine what kind of value is being sent from the device.
+ // see https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
+ quint8 heartRateValueIs16BitFlag = 0x1; //8 bit or 16 bit
+ quint8 energyExpendedFeatureFlag = 0x8;
+
+ val.clear();
+ val[0] = m_heartRateCharacteristic.value().at(index++); //1st 4 bit
+ val[1] = m_heartRateCharacteristic.value().at(index++); //2nd 4 bit
+ if (flags & heartRateValueIs16BitFlag) { //16 bit value
+ val[2] = m_heartRateCharacteristic.value().at(index++);
+ val[3] = m_heartRateCharacteristic.value().at(index++);
}
- QString value;
- value[0] = m_heartRateCharacteristic.value().at(index++);
- value[1] = m_heartRateCharacteristic.value().at(index++);
- m_HRMeasurement = value.toUInt(0, 16);
+
+ m_HRMeasurement = val.toUInt(0, 16);
//! [Reading value]
m_measurements.append(m_HRMeasurement);
- // The following flags are used to determine what kind of value is being sent from the device.
- // FLAGS field bit mask values
- qint8 heartRateValueFormat = 1;// 0
- qint8 energyExpendedFeature = 8;// 3
- bool hrDataFormat = false;
- bool energyExpendedFeatureSupported = false;
-
- hrDataFormat = ((flags & heartRateValueFormat) != heartRateValueFormat); // 0 means 8 bit, 1 means 16 bit
- energyExpendedFeatureSupported = ((flags & energyExpendedFeature) == 0);
- if (!hrDataFormat)
- qWarning() << "XXXX 16 bit heart rate measurement data encountered!";
- if (energyExpendedFeatureSupported) {
+
+ if (flags & energyExpendedFeatureFlag) {
QString energy;
int counter1 = 0;
for (int i = index; i < (m_heartRateCharacteristic.value().size() - 1); i++) {
@@ -243,7 +242,6 @@ void HeartRate::receiveMeasurement(const QLowEnergyCharacteristicInfo &character
counter1 ++;
}
energy_expended = energy.toUInt(0, 16);
- index = index + 2;
}
qWarning() << "Used energy: " << energy_expended;
Q_EMIT hrChanged();
@@ -329,7 +327,7 @@ float HeartRate::average()
int HeartRate::measurements(int index)
{
- if (index> m_measurements.size())
+ if (index > m_measurements.size())
return 0;
else
return (int)m_measurements.value(index);
diff --git a/examples/bluetooth/heartlistener/heartrate.h b/examples/bluetooth/heartlistener/heartrate.h
index e7d75666..33e3181c 100644
--- a/examples/bluetooth/heartlistener/heartrate.h
+++ b/examples/bluetooth/heartlistener/heartrate.h
@@ -130,7 +130,7 @@ private:
bool foundHeartRateService;
bool foundHeartRateCharacteristic;
int m_HRMeasurement;
- QVector<qint8> m_measurements;
+ QVector<quint8> m_measurements;
QDateTime m_start;
QDateTime m_stop;
int m_max;