Search code examples
androidwifiandroid-wifiwifimanageraccess-point

Connect to strongest access point (access point with the strongest signal) with the same SSID


I am writing a program to always connect to the strongest access point. I mean the access point with the strongest signal. First I scan all the available WiFi networks and then I restrict them to see just the ones with the same SSID.

So I can see all the APs of one network. When I connect to that network it doesn't connect to the strongest signal but I want it to switch to the strongest AP signal. By

WifiManager.compareSignalLevel(bestSignal.level, connectedAP.level) <0  

I understand the best signal.

if(WifiManager.compareSignalLevel(bestSignal.level, connectedAP.level) <0){

}

The question is what should I do in this if function to connect to the strongest AP(the AP with the strongest signal)?

Below is my code but there is no way to add the signal to it:

    WifiConfiguration conf = new WifiConfiguration();
                 conf.SSID = "\"" + bestSignal.SSID + "\"";
            // I want to connect to the access point with signal bestSignal.level
            //how is it possible?



                conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                wifiManager.addNetwork(conf);
                List<WifiConfiguration> list =wifiManager.getConfiguredNetworks();
                for( WifiConfiguration i : list ) {
                    if(i.SSID != null && 
                            i.SSID.equals("\"" + bestSignal.SSID + "\"")) {
                        wifiManager.disconnect();
                        wifiManager.enableNetwork(i.networkId, true);
                        wifiManager.reconnect();

                        break;
                    }
                }

Solution

  • You should also add BSSID with the conf. Even though different networks have same SSIDs, they will have different BSSID. Adding that line will do.

    WifiConfiguration conf = new WifiConfiguration();
                 conf.SSID = "\"" + bestSignal.SSID + "\"";
                 conf.BSSID= "\"" + bestSignal.BSSID+ "\"";