Wifi scan while connected to an AP

BrunoOtavio
Posts: 8
Joined: Wed Sep 25, 2019 8:31 pm

Wifi scan while connected to an AP

Postby BrunoOtavio » Mon Nov 28, 2022 5:45 pm

I'm dealing with an application where I have to scan to search for some devices while the esp is connected to an AP, but when I try to do this, the function esp_wifi_scan_get_ap_num return 0, and the function esp_wifi_scan_get_ap_records do not put devices in the structure. Isn't possible to realize a scan while I'm connected to an AP? I'm using idf 4.0.4

ESP_igrr
Posts: 2067
Joined: Tue Dec 01, 2015 8:37 am

Re: Wifi scan while connected to an AP

Postby ESP_igrr » Mon Nov 28, 2022 5:59 pm

Have you also called esp_wifi_scan_start before esp_wifi_scan_get_ap_num? If yes, could you check the return code of esp_wifi_scan_start?

BrunoOtavio
Posts: 8
Joined: Wed Sep 25, 2019 8:31 pm

Re: Wifi scan while connected to an AP

Postby BrunoOtavio » Mon Nov 28, 2022 6:05 pm

Yes, I called esp_wifi_scan_start before, it's returning 0.

diolupo
Posts: 11
Joined: Sat Nov 19, 2022 12:08 pm

Re: Wifi scan while connected to an AP

Postby diolupo » Thu Dec 01, 2022 3:07 pm

The same here.
If I try to perform AP scan while in STA mode and already connected to an AP, the esp_wifi_scan_get_ap_num() returns 0.

This is a snippet of my code

Code: Select all

    // blocking scan
    err_code = esp_wifi_scan_start(NULL, true);
    if (err_code != ESP_OK)
    {
        LOGE("esp_wifi_scan_start() failed, error: %d", err_code);
        goto _exit;
    }
    
    // scan done
    uint16_t ap_count = 0;            
    err_code = esp_wifi_scan_get_ap_num(&ap_count);
    if (err_code != ESP_OK)
    {
        LOGE("esp_wifi_scan_get_ap_num() failed, error: %d", err_code);
        goto _exit;
    }

    if (ap_count > 0)
    {
        ap_list = (wifi_ap_record_t *)malloc(ap_count * sizeof(wifi_ap_record_t));
        if (ap_list == NULL)
        {
            goto _exit;
        }
        memset(ap_list, 0, ap_count * sizeof(wifi_ap_record_t));
        err_code = esp_wifi_scan_get_ap_records(&ap_count, ap_list);
        if (err_code != ESP_OK)
        {
            LOGE("esp_wifi_scan_get_ap_records() failed, error: %d", err_code);
            goto _exit;
        }

        for (int i = 0; i < ap_count; i++) 
        {
            // force last character of country code
            ap_list[i].country.cc[2] = '\0';
            LOGI("%d - %s [%d] (%d) %s", i, ap_list[i].ssid, ap_list[i].primary, ap_list[i].rssi, ap_list[i].country.cc);           
        }
    }
When the device is in APSTA mode it works correctly.
I'm developing on eSP32-S3-DevKitC, using esp-idf v4.4.2 on Linux.

diolupo
Posts: 11
Joined: Sat Nov 19, 2022 12:08 pm

Re: Wifi scan while connected to an AP

Postby diolupo » Fri Dec 02, 2022 11:25 am

I understand which was my problem.
Reading the wifi documentation I have found this:
the found APs are stored in WiFi driver dynamic allocated memory and they will be
freed in esp_wifi_scan_get_ap_records, so generally, call esp_wifi_scan_get_ap_records
to cause the memory to be freed once the scan is done
Since I also have an event handler in which I was processing the ap_records, the internal ap_records list was freed when I read it inside the event handler, and when I try to read it after the blocking scan it was empty.

Who is online

Users browsing this forum: awegel, StanInexeon and 100 guests