Uniot Core
0.8.1
Loading...
Searching...
No Matches
WiFiNetworkScan.h
Go to the documentation of this file.
1/*
2 * This is a part of the Uniot project.
3 * Copyright (C) 2016-2025 Uniot <contact@uniot.io>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#pragma once
20
21#if defined(ESP8266)
22#include "ESP8266WiFi.h"
23#elif defined(ESP32)
24#include "WiFi.h"
25#endif
26
27#include <Arduino.h>
28#include <TaskScheduler.h>
29
64
65namespace uniot {
66#if defined(ESP8266)
75 public:
81 static int isSecured(int encryptionType) {
82 return static_cast<int>(encryptionType != ENC_TYPE_NONE);
83 }
84
90 void scanNetworksAsync(std::function<void(int)> onComplete, bool showHidden = false) {
91 WiFi.scanNetworksAsync(onComplete, showHidden);
92 }
93};
94#endif
95
96#if defined(ESP32)
106 public:
113 ESP32WifiScan() : mOnComplete(nullptr) {
114 mTaskCompleteScan = TaskScheduler::make([this](SchedulerTask &self, short times) {
115 if (!mOnComplete) {
116 self.detach();
117 return;
118 }
119
120 auto scanCount = WiFi.scanComplete();
121 if (scanCount == WIFI_SCAN_RUNNING || scanCount == WIFI_SCAN_FAILED) {
122 return;
123 }
124
125 mOnComplete(scanCount);
126 mOnComplete = nullptr;
127 self.detach();
128 });
129 }
130
136 static int isSecured(int encryptionType) {
137 return static_cast<int>(encryptionType != WIFI_AUTH_OPEN);
138 }
139
146 return mTaskCompleteScan;
147 }
148
155 void scanNetworksAsync(std::function<void(int)> onComplete, bool showHidden = false) {
156 WiFi.scanNetworks(true, showHidden);
157 mOnComplete = onComplete;
158 mTaskCompleteScan->attach(500);
159 }
160
161 private:
162 std::function<void(int)> mOnComplete;
163 TaskScheduler::TaskPtr mTaskCompleteScan;
164};
165#endif
166} // namespace uniot
167
ESP32WifiScan()
Construct a new ESP32 WiFi scanner.
Definition WiFiNetworkScan.h:113
TaskScheduler::TaskPtr getTask()
Get the scan completion monitoring task.
Definition WiFiNetworkScan.h:145
void scanNetworksAsync(std::function< void(int)> onComplete, bool showHidden=false)
Start asynchronous WiFi network scan.
Definition WiFiNetworkScan.h:155
static int isSecured(int encryptionType)
Check if a network uses encryption.
Definition WiFiNetworkScan.h:136
void detach()
Stop and detach the timer.
Definition ESP8266Task.h:107
WiFi network scanner implementation for ESP8266.
Definition WiFiNetworkScan.h:74
static int isSecured(int encryptionType)
Check if a network uses encryption.
Definition WiFiNetworkScan.h:81
void scanNetworksAsync(std::function< void(int)> onComplete, bool showHidden=false)
Start asynchronous WiFi network scan.
Definition WiFiNetworkScan.h:90
Definition TaskScheduler.h:67
SharedPointer< SchedulerTask > TaskPtr
Shared pointer type for scheduler tasks.
Definition TaskScheduler.h:171
static TaskPtr make(SchedulerTask::SchedulerTaskCallback callback)
Static factory method to create a task with a callback.
Definition TaskScheduler.h:193
Contains all classes and functions related to the Uniot Core.