diff --git a/src/cscbike.cpp b/src/cscbike.cpp index 1602d60b2..55f3ed0ab 100644 --- a/src/cscbike.cpp +++ b/src/cscbike.cpp @@ -89,6 +89,15 @@ void cscbike::update() { /*initDone*/) { update_metrics(true, watts()); + if(lastGoodCadence.secsTo(QDateTime::currentDateTime()) > 5 && !charNotified) { + readMethod = true; + qDebug() << "no cadence for 5 secs, switching to reading method"; + } + + if(readMethod && cadenceService) { + cadenceService->readCharacteristic(cadenceChar); + } + // updating the treadmill console every second if (sec1Update++ == (500 / refresh->interval())) { sec1Update = 0; @@ -141,6 +150,8 @@ void cscbike::characteristicChanged(const QLowEnergyCharacteristic &characterist double _WheelRevs = 0; uint8_t battery = 0; + charNotified = true; + emit debug(QStringLiteral(" << ") + newValue.toHex(' ')); if (characteristic.uuid() == QBluetoothUuid((quint16)0x2A19)) { @@ -311,8 +322,16 @@ void cscbike::stateChanged(QLowEnergyService::ServiceState state) { qDebug() << QStringLiteral("all services discovered!"); + QBluetoothUuid CyclingSpeedAndCadence(QBluetoothUuid::CyclingSpeedAndCadence); + for (QLowEnergyService *s : qAsConst(gattCommunicationChannelService)) { if (s->state() == QLowEnergyService::ServiceDiscovered) { + + if(s->serviceUuid() == CyclingSpeedAndCadence) { + qDebug() << "CyclingSpeedAndCadence found"; + cadenceService = s; + } + // establish hook into notifications connect(s, &QLowEnergyService::characteristicChanged, this, &cscbike::characteristicChanged); connect(s, &QLowEnergyService::characteristicWritten, this, &cscbike::characteristicWritten); @@ -327,7 +346,11 @@ void cscbike::stateChanged(QLowEnergyService::ServiceState state) { auto characteristics_list = s->characteristics(); for (const QLowEnergyCharacteristic &c : qAsConst(characteristics_list)) { - qDebug() << QStringLiteral("char uuid") << c.uuid() << QStringLiteral("handle") << c.handle(); + if(c.uuid() == QBluetoothUuid((quint16)0x2A5B)) { + qDebug() << "CyclingSpeedAndCadence char found"; + cadenceChar = c; + } + qDebug() << QStringLiteral("char uuid") << c.uuid() << QStringLiteral("handle") << c.handle() << QStringLiteral("properties") << c.properties(); auto descriptors_list = c.descriptors(); for (const QLowEnergyDescriptor &d : qAsConst(descriptors_list)) { qDebug() << QStringLiteral("descriptor uuid") << d.uuid() << QStringLiteral("handle") << d.handle(); @@ -424,6 +447,8 @@ void cscbike::characteristicWritten(const QLowEnergyCharacteristic &characterist void cscbike::characteristicRead(const QLowEnergyCharacteristic &characteristic, const QByteArray &newValue) { qDebug() << QStringLiteral("characteristicRead ") << characteristic.uuid() << newValue.toHex(' '); + + characteristicChanged(characteristic, newValue); } void cscbike::serviceScanDone(void) { diff --git a/src/cscbike.h b/src/cscbike.h index 25f556969..825e81c60 100644 --- a/src/cscbike.h +++ b/src/cscbike.h @@ -47,13 +47,15 @@ class cscbike : public bike { QTimer *refresh; QList gattCommunicationChannelService; - // QLowEnergyCharacteristic gattNotify1Characteristic; + QLowEnergyService* cadenceService = nullptr; + QLowEnergyCharacteristic cadenceChar; uint8_t sec1Update = 0; QByteArray lastPacket; QDateTime lastRefreshCharacteristicChanged = QDateTime::currentDateTime(); QDateTime lastGoodCadence = QDateTime::currentDateTime(); uint8_t firstStateChanged = 0; + bool charNotified = false; bool initDone = false; bool initRequest = false; @@ -62,6 +64,8 @@ class cscbike : public bike { bool noHeartService = false; bool noVirtualDevice = false; + bool readMethod = false; + uint16_t oldLastCrankEventTime = 0; uint16_t oldCrankRevs = 0;