Home / References / ESP32 Library / WiFi API / WiFiScan
Description
The WiFi.getNetworkInfo
method is part of the ESP32 WiFi Library and is used to retrieve detailed information about a specific WiFi network detected during a scan. This method is particularly useful for IoT projects where you need to analyze available networks, such as their SSID, encryption type, signal strength (RSSI), BSSID, and channel. It complements the WiFi scanning functionality by providing a way to access individual network details after a scan has been performed using WiFi.scanNetworks()
.
Syntax and Usage
The WiFi.getNetworkInfo
method is used after a WiFi scan to fetch details of a specific network by its index. Below is the syntax:
bool WiFi.getNetworkInfo(uint8_t networkItem, String &ssid, uint8_t &encryptionType, int32_t &RSSI, uint8_t* &BSSID, int32_t &channel);
There is only one way to use this method, as it requires all arguments to be provided:
- With Arguments: Pass the index of the network (
networkItem
) along with references to variables where the network details will be stored. This is the standard usage, allowing you to retrieve all available information about a scanned network.
Argument(s)
The WiFi.getNetworkInfo
method requires the following arguments:
uint8_t networkItem
: The index of the network from the scan results (0 toWiFi.scanNetworks()
return value minus 1).String &ssid
: A reference to a String variable where the network’s SSID (name) will be stored.uint8_t &encryptionType
: A reference to a variable where the encryption type (e.g., WPA2, WEP) will be stored as an integer.int32_t &RSSI
: A reference to a variable where the Received Signal Strength Indicator (RSSI) in dBm will be stored.uint8_t* &BSSID
: A reference to a pointer where the network’s BSSID (MAC address, 6 bytes) will be stored.int32_t &channel
: A reference to a variable where the WiFi channel number will be stored.
Return Value
The WiFi.getNetworkInfo
method returns a bool
:
- true: If the network information was successfully retrieved for the specified
networkItem
. - false: If the retrieval failed (e.g., the
networkItem
index is out of range or no scan has been performed).
Example Codes
Note: This example demonstrates how to use WiFi.getNetworkInfo
to list details of all detected WiFi networks after a scan. It’s ideal for debugging or selecting a network for connection in an IoT project.
/*
* Author: Avant Maker
* Date: February 21, 2025
* Version: 1.0
* Description: This example code demonstrates how to
* use WiFi.getNetworkInfo to list details of all detected WiFi networks
* after a scan.
*
* License: MIT
*
* Code Source: This example code is sourced from the Comprehensive
* Guide to the ESP32 Arduino Core Library, accessible on
* AvantMaker.com. For additional code examples and in-depth
* documentation related to the ESP32 Arduino Core Library,
* please visit:
*
* https://avantmaker.com/references/esp32-arduino-core-index/
*
* AvantMaker.com, your premier destination for all things
* DIY, AI, IoT, Smart Home, and STEM projects. We are dedicated
* to empowering makers, learners, and enthusiasts with the resources
* they need to bring their innovative ideas to life.
*/
#include <WiFi.h>
void setup() {
Serial.begin(115200);
// Set WiFi to station mode
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
// Scan for networks
Serial.println("Scanning WiFi networks...");
int networkCount = WiFi.scanNetworks();
if (networkCount == 0) {
Serial.println("No networks found.");
} else {
Serial.print(networkCount);
Serial.println(" networks found:");
// Loop through all detected networks
for (int i = 0; i < networkCount; i++) {
String ssid;
uint8_t encryptionType;
int32_t rssi;
uint8_t* bssid;
int32_t channel;
// Get network info for the current index
if (WiFi.getNetworkInfo(i, ssid, encryptionType, rssi, bssid, channel)) {
Serial.print(i + 1);
Serial.print(": ");
Serial.print(ssid);
Serial.print(" (RSSI: ");
Serial.print(rssi);
Serial.print(" dBm, Channel: ");
Serial.print(channel);
Serial.print(", Encryption: ");
Serial.print(encryptionType);
Serial.print(") BSSID: ");
for (int j = 0; j < 6; j++) {
Serial.print(bssid[j], HEX);
if (j < 5) Serial.print(":");
}
Serial.println();
} else {
Serial.print(i + 1);
Serial.println(": Failed to get network info.");
}
}
}
// Clean up scan results
WiFi.scanDelete();
}
void loop() {
// Do nothing in loop
}
AvantMaker Tutorial Suggestion
If you want to learn more about the ESP32’s WiFi capabilities, we’ve created a beginner-friendly guide that explains them in detail. Just click the link below, and you’ll be teleported to that page. It will clear up any confusion you may have while connecting your ESP32 to WiFi or setting it up as a WiFi access point (AP).
ESP32 Library Index
- ESP32 WiFiClient Library
- ESP32 HTTPClient Library
- ESP32 WiFiClientSecure Library
- ESP32 WebServer Library
- ESP32 WiFi Library
- Which ESP32 Boards are Recommended for Learners
- How to Copy Codes from AvantMaker.com
- What is SPIFFS and how to upload files to it?
- What is LIttleFS and how to upload files to it?
Ready to experiment and explore more about ESP32? Visit our website’s All About ESP32 Resources Hub, packed with tutorials, guides, and tools to inspire your maker journey. Experiment, explore, and elevate your skills with everything you need to master this powerful microcontroller platform!