'How to get a list of wi-fi networks

Good afternoon. For a very long time I have been trying to get a list of wi-fi networks using WifiManager. When I try to get through a virtual device, I get only one fictional network with an SSID: Android Wifi. However, I do not receive networks from my real environment. When using a real device, it is not possible to get any network. What is the mistake?

Rights in AndroidManifest:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

The code I use to work with Wifi:

WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    wifiManager.startScan();
    List<ScanResult> availNetworks = wifiManager.getScanResults();
    Log.d("STRING",Integer.toString(availNetworks.size()));
    if (availNetworks.size() > 0) {
        for (int i=0; i< availNetworks.size();i++) {
            String buf = availNetworks.get(i).toString();
            String[] buflist = buf.split(",");
            Log.d("STRING","In");
            String elementWssid = buflist[0];
            String elementWsec = buflist[2];
            String elementWlevel = buflist[3];
            String SSID = elementWssid.split(": ")[1];
            String Sec = elementWsec.split(": ")[1];
            String level = elementWlevel.split(": ")[1];
            Log.d("STRING",SSID);
            Log.d("STRING",Sec);
            Log.d("STRING",level);
        }
    }

Permits have already been issued.



Solution 1:[1]

Solved a problem. In the application settings there is such a section as "other permissions". It contains permissions to work with wi-fi, which by default are in the "ask" status, after changing the status to "allow" the problem was solved. For some reason, android doesn't feel the need to ask for permissions like location.

P.s. Xiaomi has a separate slider to access the location of ALL apps, it needs to be turned on to give permissions.

AndroidWifi on a virtual device is a test network for checking the performance of applications, since it does not know how to work with real networks.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1