user3423347
user3423347

Reputation: 33

WifiManager.getConfiguredNetworks always returns empty list

I am trying to connect to an open wifi network. When I open my app it should turn on wifi and connect to the network defined as below. The problem is that WifiManager.getConfiguredNetworks always returns me an empty list. I have tried using locks too without success.

WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";
conf.status = WifiConfiguration.Status.ENABLED;        
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.priority = Integer.MAX_VALUE;

WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE); 
wifiManager.setWifiEnabled(true);   
wifiManager.addNetwork(conf);


List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
if(list.isEmpty()) 
{
    Log.e("Connection Setup","Empty list returned");
}

for( WifiConfiguration i : list ) {
    if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
    Log.e("Connection Setup",i.SSID+" connrction attempted");
    wifiManager.disconnect();
    wifiManager.enableNetwork(i.networkId, true);
    wifiManager.reconnect();               
    break;
 }           
}

I have been trying more things - if I make this thread sleep for about 10 seconds or so - everything works fine - but is there a better alternative?

Upvotes: 3

Views: 6957

Answers (4)

barbudito
barbudito

Reputation: 578

Since new Android versions (I think since Android 12) you also need to add COARSE_LOCATION permission to your AndroidManifest.xml.

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

Then you can use (Android <= 28):

List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();

For Android >= 29:

wifiManager.getScanResults()

Upvotes: 0

Youngpan
Youngpan

Reputation: 11

You can not continue the

CMD wifiManager.getConfiguredNetworks()

until the status of WiFi is enabled completely. For enabling the WiFi it need some time. So you need to delay some time.

Upvotes: 1

Carson Liu
Carson Liu

Reputation: 11

I also met this question unfortunely.

After searching for some time, I think it is a bug.

This is the android implement of getConfiguredNetworks

public List<WifiConfiguration> getConfiguredNetworks() {
    try {
        return mService.getConfiguredNetworks();
    } catch (RemoteException e) {
        return null;
    }
}

It is clearly shown that the function will return null if RemoteException happened when running. Up to now, I am also distressed with this and could not get some points to address this issue.

For more information: https://code.google.com/p/android/issues/detail?id=19078

Upvotes: 0

Abhijeet
Abhijeet

Reputation: 8771

Try to comment these lines in your code.. do you see any change in result set?

wifiManager.setWifiEnabled(true);   
wifiManager.addNetwork(conf);

You might want to check the return for wifiManager.addNetwork(conf);, is it returning -1.

For me this lines return 31 objects only.. no idea why..still hunting for it.

WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
List<WifiConfiguration> arraylist = wifiManager.getConfiguredNetworks();
Log.wtf("WifiPreference","No of Networks"+arraylist.size());

Upvotes: 0

Related Questions