diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiScan.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiScan.cpp index b9186e899b..9b04771a0b 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiScan.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiScan.cpp @@ -58,6 +58,8 @@ bool ESP8266WiFiScanClass::_scanComplete = false; size_t ESP8266WiFiScanClass::_scanCount = 0; void* ESP8266WiFiScanClass::_scanResult = 0; +std::function ESP8266WiFiScanClass::_onComplete; + /** * Start scan WiFi networks available * @param async run in async mode @@ -102,6 +104,15 @@ int8_t ESP8266WiFiScanClass::scanNetworks(bool async, bool show_hidden) { } +/** + * Starts scanning WiFi networks available in async mode + * @param onComplete the event handler executed when the scan is done + * @param show_hidden show hidden networks + */ +void ESP8266WiFiScanClass::scanNetworksAsync(std::function onComplete, bool show_hidden) { + _onComplete = onComplete; + scanNetworks(true, show_hidden); +} /** * called to get the scan state in Async mode @@ -305,6 +316,9 @@ void ESP8266WiFiScanClass::_scanDone(void* result, int status) { if(!ESP8266WiFiScanClass::_scanAsync) { esp_schedule(); + } else if (ESP8266WiFiScanClass::_onComplete) { + ESP8266WiFiScanClass::_onComplete(ESP8266WiFiScanClass::_scanCount); + ESP8266WiFiScanClass::_onComplete = nullptr; } } @@ -319,4 +333,3 @@ void * ESP8266WiFiScanClass::_getScanInfoByIndex(int i) { } return reinterpret_cast(ESP8266WiFiScanClass::_scanResult) + i; } - diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiScan.h b/libraries/ESP8266WiFi/src/ESP8266WiFiScan.h index 9d0c964a28..742f742190 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiScan.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiScan.h @@ -35,6 +35,7 @@ class ESP8266WiFiScanClass { public: int8_t scanNetworks(bool async = false, bool show_hidden = false); + void scanNetworksAsync(std::function onComplete, bool show_hidden = false); int8_t scanComplete(); void scanDelete(); @@ -59,6 +60,8 @@ class ESP8266WiFiScanClass { static size_t _scanCount; static void* _scanResult; + static std::function _onComplete; + static void _scanDone(void* result, int status); static void * _getScanInfoByIndex(int i);