From 10879109faf527d27ba19a83c5375560cc80a132 Mon Sep 17 00:00:00 2001 From: iranl Date: Tue, 18 Jun 2024 21:12:48 +0200 Subject: [PATCH] Improve pairing reliability and speed --- src/NukiBle.cpp | 13 ++++++++----- src/NukiBle.h | 3 ++- src/NukiBle.hpp | 8 ++++---- src/NukiLock.cpp | 2 +- src/NukiOpener.cpp | 2 +- 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/NukiBle.cpp b/src/NukiBle.cpp index 12f94e71..04560ea9 100644 --- a/src/NukiBle.cpp +++ b/src/NukiBle.cpp @@ -77,14 +77,17 @@ PairingResult NukiBle::pairNuki(AuthorizationIdType idType) { if (retrieveCredentials()) { #ifdef DEBUG_NUKI_CONNECT - log_d("Allready paired"); + log_d("Already paired"); #endif isPaired = true; return PairingResult::Success; } PairingResult result = PairingResult::Pairing; + + if (pairingLastSeen < millis() - 2000) pairingServiceAvailable = false; if (pairingServiceAvailable && bleAddress != BLEAddress("")) { + pairingServiceAvailable = false; #ifdef DEBUG_NUKI_CONNECT log_d("Nuki in pairing mode found"); #endif @@ -94,6 +97,7 @@ PairingResult NukiBle::pairNuki(AuthorizationIdType idType) { PairingState nukiPairingState = PairingState::InitPairing; do { nukiPairingState = pairStateMachine(nukiPairingState); + extendDisconnectTimeout(); delay(50); } while ((nukiPairingState != PairingState::Success) && (nukiPairingState != PairingState::Timeout)); @@ -104,7 +108,7 @@ PairingResult NukiBle::pairNuki(AuthorizationIdType idType) { } else { result = PairingResult::Timeout; } - extendDisonnectTimeout(); + extendDisconnectTimeout(); } } else { #ifdef DEBUG_NUKI_CONNECT @@ -196,7 +200,7 @@ void NukiBle::setConnectRetries(uint8_t retries) { connectRetries = retries; } -void NukiBle::extendDisonnectTimeout() { +void NukiBle::extendDisconnectTimeout() { lastStartTimeout = millis(); } @@ -259,8 +263,7 @@ void NukiBle::onResult(BLEAdvertisedDevice* advertisedDevice) { #endif bleAddress = advertisedDevice->getAddress(); pairingServiceAvailable = true; - } else { - pairingServiceAvailable = false; + pairingLastSeen = millis(); } } } diff --git a/src/NukiBle.h b/src/NukiBle.h index 3d1f7a70..121558c3 100644 --- a/src/NukiBle.h +++ b/src/NukiBle.h @@ -283,7 +283,7 @@ class NukiBle : public BLEClientCallbacks, public BleScanner::Subscriber { protected: bool connectBle(const BLEAddress bleAddress); - void extendDisonnectTimeout(); + void extendDisconnectTimeout(); template Nuki::CmdResult executeAction(const TDeviceAction action); @@ -319,6 +319,7 @@ class NukiBle : public BLEClientCallbacks, public BleScanner::Subscriber { uint16_t timeoutDuration = 1000; uint8_t connectTimeoutSec = 1; uint8_t connectRetries = 5; + unsigned long pairingLastSeen = 0; void onConnect(BLEClient*) override; void onDisconnect(BLEClient*) override; diff --git a/src/NukiBle.hpp b/src/NukiBle.hpp index 9491fa6e..6a65dce6 100644 --- a/src/NukiBle.hpp +++ b/src/NukiBle.hpp @@ -32,7 +32,7 @@ Nuki::CmdResult NukiBle::executeAction(const TDeviceAction action) { Nuki::CmdResult result = cmdStateMachine(action); if (result != Nuki::CmdResult::Working) { giveNukiBleSemaphore(); - extendDisonnectTimeout(); + extendDisconnectTimeout(); return result; } esp_task_wdt_reset(); @@ -43,7 +43,7 @@ Nuki::CmdResult NukiBle::executeAction(const TDeviceAction action) { Nuki::CmdResult result = cmdChallStateMachine(action); if (result != Nuki::CmdResult::Working) { giveNukiBleSemaphore(); - extendDisonnectTimeout(); + extendDisconnectTimeout(); return result; } esp_task_wdt_reset(); @@ -54,7 +54,7 @@ Nuki::CmdResult NukiBle::executeAction(const TDeviceAction action) { Nuki::CmdResult result = cmdChallAccStateMachine(action); if (result != Nuki::CmdResult::Working) { giveNukiBleSemaphore(); - extendDisonnectTimeout(); + extendDisconnectTimeout(); return result; } esp_task_wdt_reset(); @@ -65,7 +65,7 @@ Nuki::CmdResult NukiBle::executeAction(const TDeviceAction action) { Nuki::CmdResult result = cmdChallStateMachine(action, true); if (result != Nuki::CmdResult::Working) { giveNukiBleSemaphore(); - extendDisonnectTimeout(); + extendDisconnectTimeout(); return result; } esp_task_wdt_reset(); diff --git a/src/NukiLock.cpp b/src/NukiLock.cpp index 7243985c..57eb33a0 100644 --- a/src/NukiLock.cpp +++ b/src/NukiLock.cpp @@ -758,7 +758,7 @@ void NukiLock::createNewAdvancedConfig(const AdvancedConfig* oldConfig, NewAdvan } void NukiLock::handleReturnMessage(Command returnCode, unsigned char* data, uint16_t dataLen) { - extendDisonnectTimeout(); + extendDisconnectTimeout(); switch (returnCode) { case Command::KeyturnerStates : { diff --git a/src/NukiOpener.cpp b/src/NukiOpener.cpp index 79575be5..4aa74d0e 100644 --- a/src/NukiOpener.cpp +++ b/src/NukiOpener.cpp @@ -646,7 +646,7 @@ void NukiOpener::createNewAdvancedConfig(const AdvancedConfig* oldConfig, NewAdv void NukiOpener::handleReturnMessage(Command returnCode, unsigned char* data, uint16_t dataLen) { - extendDisonnectTimeout(); + extendDisconnectTimeout(); switch (returnCode) { case Command::KeyturnerStates : {