Home > Mobile >  Is it possible to get mac addresses when scanning networks? ESP32
Is it possible to get mac addresses when scanning networks? ESP32

Time:10-07

I need to get the RSSI of the networks and they MAC addresss to a IPS (indoor position system) program. I was able to get ssid, signal strength and security using the sample code, but not mac addresses. I tryed to use this, but itsn't working:

void loop() {
  int n = WiFi.scanNetworks();

  if(n == 0){
    Serial.println("no networks found");
  }
  else{
      for (int i = 0; i < n;   i) {
        Serial.print(i   1);
        Serial.print(": ");
        Serial.print(WiFi.SSID(i));
        Serial.print(" (");
        Serial.print(WiFi.RSSI(i));
        Serial.print(")");
        Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");
        Serial.println(WiFi.macAddress(i));
        delay(10);
      }
  }
  delay(10000);
}

CodePudding user response:

@Tarmo's answer is correct, but the Arduino core does provide a simpler interface to getting the AP's access point without having to directly call ESP-IDF functions.

Use the BSSID method to get the MAC address of the base station's wifi radio.

You can call either the BSSID() method to get a pointer to the six byte MAC address or BSSIDstr() to get the MAC address as a string.

So for instance:

        Serial.print(WiFi.BSSIDstr(i));

will print the MAC address as a string.

When you're stuck on this kind of things referring to the library's source code can help you find out more than documentation and tutorials may.

CodePudding user response:

Maybe the Arduino framework doesn't give this information up easily, but the underlying ESP IDF framework certainly does. The AP-s MAC is called BSSID. Adapting this example scan.c:

#include "esp_wifi.h"

#define DEFAULT_SCAN_LIST_SIZE 10

void wifi_scan(void) {
    uint16_t ap_count = 0;
    uint16_t number = DEFAULT_SCAN_LIST_SIZE;
    wifi_ap_record_t ap_info[DEFAULT_SCAN_LIST_SIZE];

    esp_wifi_scan_start(NULL, true);
    ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number, ap_info));
    ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count));
    printf("Total APs scanned = %u", ap_count);
    for (int i = 0; (i < DEFAULT_SCAN_LIST_SIZE) && (i < ap_count); i  ) {
        printf("SSID \t\t%s", ap_info[i].ssid);
        printf("RSSI \t\t%d", ap_info[i].rssi);
        printf("BSSID \t\tXXXXXX\n", 
            ap_info[i].bssid[0], ap_info[i].bssid[1], ap_info[i].bssid[2], 
            ap_info[i].bssid[3], ap_info[i].bssid[4], ap_info[i].bssid[5]);
    }
}

Have a look at the official docs for struct wifi_ap_record_t to see what other information is available.

  • Related