本帖最后由 roc2 于 2019-6-19 10:55 編輯
概述: 無(wú)線網(wǎng)絡(luò)提供的WiFi熱點(diǎn),大部分都開放了SSID廣播(記得之前博主講過(guò)WiFi熱點(diǎn)也可以隱藏的),Scan WiFi的功能就是掃描出所有附近的WiFi熱點(diǎn)的SSID信息,這樣一來(lái),客戶端就可以根據(jù)需要選擇不同的SSID連入對(duì)應(yīng)的無(wú)線網(wǎng)絡(luò)中。 Scan WiFi庫(kù)提供了兩種方式實(shí)現(xiàn)掃描過(guò)程: 同步掃描:通過(guò)單個(gè)函數(shù)在一次運(yùn)行中完成,需要等待完成所有操作才能繼續(xù)運(yùn)行下面的操作。 異步掃描:把上面的過(guò)程分成幾個(gè)步驟,每個(gè)步驟由一個(gè)單獨(dú)函數(shù)完成,我們可以在掃描過(guò)程中執(zhí)行其他任務(wù)。 ESP8266WiFiScan庫(kù)ESP8266WiFiScan庫(kù),大家使用的時(shí)候只需要引入 掃描操作方法1.scanNetworks —— 同步掃描周邊有效wifi網(wǎng)絡(luò)函數(shù)說(shuō)明:- /**
- * Start scan WiFi networks available
- * @param async run in async mode(是否啟動(dòng)異步掃描)
- * @param show_hidden show hidden networks(是否掃描隱藏網(wǎng)絡(luò))
- * @param channel scan only this channel (0 for all channels)(是否掃描特定通道)
- * @param ssid* scan for only this ssid (NULL for all ssid's)(是否掃描特定的SSID)
- * @return Number of discovered networks
- */int8_t scanNetworks(bool async = false, bool show_hidden = false, uint8 channel = 0, uint8* ssid = NULL);
復(fù)制代碼 2.scanNetworks(async ) —— 異步掃描周邊有效wifi網(wǎng)絡(luò)函數(shù)說(shuō)明: - /**
- * Start scan WiFi networks available
- * @param async run in async mode(是否啟動(dòng)異步掃描)
- * @param show_hidden show hidden networks(是否掃描隱藏網(wǎng)絡(luò))
- * @param channel scan only this channel (0 for all channels)(是否掃描特定通道)
- * @param ssid* scan for only this ssid (NULL for all ssid's)(是否掃描特定的SSID)
- * @return Number of discovered networks
- */int8_t scanNetworks(bool async = false, bool show_hidden = false, uint8 channel = 0, uint8* ssid = NULL);
復(fù)制代碼3.scanNetworksAsync —— 異步掃描周邊wifi網(wǎng)絡(luò),并回調(diào)結(jié)果
函數(shù)說(shuō)明: - /**
- * 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 scanNetworksAsync(std::function<void(int)> onComplete, bool show_hidden = false);
復(fù)制代碼 4. scanComplete —— 檢測(cè)異步掃描的結(jié)果函數(shù)說(shuō)明: - /**
- * called to get the scan state in Async mode(異步掃描的結(jié)果函數(shù))
- * @return scan result or status
- * -1 if scan not find
- * -2 if scan not triggered
- */
- int8_t scanComplete();
復(fù)制代碼 5.scanDelete —— 從內(nèi)存中刪掉最近掃描結(jié)果函數(shù)說(shuō)明: - /**
- * delete last scan result from RAM(從內(nèi)存中刪除最近的掃描結(jié)果)
- */void scanDelete();
復(fù)制代碼 掃描結(jié)果方法1. SSID —— 獲取wifi網(wǎng)絡(luò)名字函數(shù)說(shuō)明: - /**
- * Return the SSID discovered during the network scan.
- * @param i specify from which network item want to get the information
- * @return ssid string of the specified item on the networks scanned list
- */String SSID(uint8_t networkItem);
復(fù)制代碼 2.RSSI —— 獲取wifi網(wǎng)絡(luò)信號(hào)強(qiáng)度函數(shù)說(shuō)明: - /**
- * Return the RSSI of the networks discovered during the scanNetworks(信號(hào)強(qiáng)度)
- * @param i specify from which network item want to get the information
- * @return signed value of RSSI of the specified item on the networks scanned list
- */int32_t RSSI(uint8_t networkItem);
復(fù)制代碼 3. encryptionType —— 獲取wifi網(wǎng)絡(luò)加密方式函數(shù)說(shuō)明: - /**
- * Return the encryption type of the networks discovered during the scanNetworks(加密方式)
- * @param i specify from which network item want to get the information
- * @return encryption type (enum wl_enc_type) of the specified item on the networks scanned list
- * ............ Values map to 802.11 encryption suites.....................
- * AUTH_OPEN ----> ENC_TYPE_WEP = 5,
- * AUTH_WEP ----> ENC_TYPE_TKIP = 2,
- * AUTH_WPA_PSK ----> ENC_TYPE_CCMP = 4,
- * ........... except these two, 7 and 8 are reserved in 802.11-2007.......
- * AUTH_WPA2_PSK ----> ENC_TYPE_NONE = 7,
- * AUTH_WPA_WPA2_PSK ----> ENC_TYPE_AUTO = 8
- */uint8_t encryptionType(uint8_t networkItem);
復(fù)制代碼 4. BSSID —— 獲取wifi網(wǎng)絡(luò)mac地址函數(shù)說(shuō)明: - /**
- * return MAC / BSSID of scanned wifi (物理地址)
- * @param i specify from which network item want to get the information
- * @return uint8_t * MAC / BSSID of scanned wifi
- */uint8_t * BSSID(uint8_t networkItem);
- /**
- * return MAC / BSSID of scanned wifi (物理地址)
- * @param i specify from which network item want to get the information
- * @return uint8_t * MAC / BSSID of scanned wifi
- */String BSSIDstr(uint8_t networkItem);
復(fù)制代碼 5.getNetworkInfo —— 獲取整體網(wǎng)絡(luò)信息,名字,信號(hào)強(qiáng)度等函數(shù)說(shuō)明: - /**
- * loads all infos from a scanned wifi in to the ptr parameters
- * @param networkItem uint8_t
- * @param ssid const char**
- * @param encryptionType uint8_t *
- * @param RSSI int32_t *
- * @param BSSID uint8_t **
- * @param channel int32_t *
- * @param isHidden bool *
- * @return (true if ok)
- */ bool getNetworkInfo(uint8_t networkItem, String &ssid, uint8_t &encryptionType, int32_t &RSSI, uint8_t* &BSSID, int32_t &channel, bool &isHidden);
復(fù)制代碼 6. channel —— 獲取wifi網(wǎng)絡(luò)通道號(hào)函數(shù)說(shuō)明: - /**
- * return channel of scanned wifi(通道號(hào))
- */int32_t channel(uint8_t networkItem);
復(fù)制代碼 7. isHidden —— 判斷wifi網(wǎng)絡(luò)是否是隱藏網(wǎng)絡(luò)函數(shù)說(shuō)明: - /**
- * return if the scanned wifi is Hidden (no SSID)(判斷掃描到的wifi是否是隱藏wifi)
- * @param networkItem specify from which network item want to get the information
- * @return bool (true == hidden)
- */bool isHidden(uint8_t networkItem);
復(fù)制代碼動(dòng)手操作 多說(shuō)不宜,實(shí)驗(yàn)是檢驗(yàn)真理的唯一標(biāo)準(zhǔn),下面我們就來(lái)實(shí)際操作一下吧。 打開零知開源開發(fā)工具,新建一個(gè)項(xiàng)目,輸入以下代碼,驗(yàn)證,上傳。
新建項(xiàng)目-1.png (68.83 KB, 下載次數(shù): 103)
下載附件
2019-6-10 16:34 上傳
測(cè)試demo: - /**
- * Demo:
- * STA模式下,演示同步掃描Scan wifi功能
- * @author 云上上云
- * @date 2019/06/01
- */#include <ESP8266WiFi.h>
- //以下三個(gè)定義為調(diào)試定義#define DebugBegin(baud_rate) Serial.begin(baud_rate)#define DebugPrintln(message) Serial.println(message)#define DebugPrint(message) Serial.print(message)
- void setup() { //設(shè)置串口波特率,以便打印信息
- DebugBegin(115200); //延時(shí)5s 為了演示效果
- delay(5000); // 我不想別人連接我,只想做個(gè)站點(diǎn)
- WiFi.mode(WIFI_STA); //斷開連接
- WiFi.disconnect();
- delay(100);
- DebugPrintln("Setup done");
- }
- void loop() {
- DebugPrintln("scan start"); // 同步掃描,等待返回結(jié)果
- int n = WiFi.scanNetworks();
- DebugPrintln("scan done"); if (n == 0){
- DebugPrintln("no networks found");
- }else{
- DebugPrint(n);
- DebugPrintln(" networks found"); for (int i = 0; i < n; ++i){
- DebugPrint(i + 1);
- DebugPrint(": "); //打印wifi賬號(hào)
- DebugPrint(WiFi.SSID(i));
- DebugPrint(",");
- DebugPrint(String("Ch:")+WiFi.channel(i));
- DebugPrint(",");
- DebugPrint(WiFi.isHidden(i)?"hide":"show");
- DebugPrint(" ("); //打印wifi信號(hào)強(qiáng)度
- DebugPrint(WiFi.RSSI(i));
- DebugPrint("dBm");
- DebugPrint(")"); //打印wifi加密方式
- DebugPrintln((WiFi.encryptionType(i) == ENC_TYPE_NONE)?"open":"*");
- delay(10);
- }
- }
- DebugPrintln(""); // 延時(shí)5s之后再次掃描
- delay(5000);
- }
復(fù)制代碼 測(cè)試結(jié)果(附近潛在的WiFi熱點(diǎn)):它可以掃描完附件所有WiFi。
結(jié)果.png (46.72 KB, 下載次數(shù): 113)
下載附件
2019-6-10 16:35 上傳
更多詳細(xì)資料可到零知實(shí)驗(yàn)室官網(wǎng)免費(fèi)獲取。
|