Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to scan Access Points and select strongest signal?

I am currently trying to write a class in Android that will Scan for access points, calculate which access point has the best signal and then connect to that access point.

So the application will be able to scan on the move and attach to new access points on the go.

I have the scanning and calculation of the best signal working.

But when it comes to attaching to the best access point I am having trouble.

It appears that enableNetwork(netid, othersTrueFalse) is the only method for attaching to an Access point but this causes problems as from my Scan Results I am not able to get the id of the access point with the strongest signal.

This is my code:


public void doWifiScan(){

  scanTask = new TimerTask() {
  public void run() {
      handler.post(new Runnable() {
          public void run() {
               sResults = wifiManager.scan(getBaseContext()); 
               if(sResults!=null)
               Log.d("TIMER", "sResults count" + sResults.size());
               ScanResult scan = wifiManager.calculateBestAP(sResults);
               wifiManager.addNewAccessPoint(scan);
           }
       });
    }};

    t.schedule(scanTask, 3000, 30000); 
}

public ScanResult calculateBestAP(List<ScanResult> sResults){

     ScanResult bestSignal = null;
        for (ScanResult result : sResults) {
          if (bestSignal == null
              || WifiManager.compareSignalLevel(bestSignal.level, result.level) < 0)
            bestSignal = result;
        }

        String message = String.format("%s networks found. %s is the strongest. %s is the bsid",
                sResults.size(), bestSignal.SSID, bestSignal.BSSID);

        Log.d("sResult", message);
        return bestSignal;
}

public void addNewAccessPoint(ScanResult scanResult){

    WifiConfiguration wc = new WifiConfiguration();
    wc.SSID = '\"' + scanResult.SSID + '\"';
    //wc.preSharedKey  = "\"password\"";
    wc.hiddenSSID = true;
    wc.status = WifiConfiguration.Status.ENABLED;        
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
    wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    int res = mainWifi.addNetwork(wc);
    Log.d("WifiPreference", "add Network returned " + res );
    boolean b = mainWifi.enableNetwork(res, false);        
    Log.d("WifiPreference", "enableNetwork returned " + b );

}

When I try to use addNewAccessPoint(ScanResult scanResult) it just adds another AP to the list in the settings application with the same name as the one with the best signal, so I end up with loads of duplicates and not actually attaching to them.

Can anyone point me in the direction of a better solution?

like image 937
Donal Rafferty Avatar asked May 13 '10 14:05

Donal Rafferty


People also ask

How often does Android scan WiFi?

On devices running Android 9 or lower, when the device is disconnected from Wi-Fi and the screen is off, PNO scans occur at 20 second intervals for the first three scans, then slow down to one scan every 60 seconds for all subsequent scans.

What is WiFi manager in Android?

WiFi Manager is a tool to manage WiFi connections, thanks to which you will not only have the ability to find and connect to networks in your environment, but it also will improve the quality of those connections.


1 Answers

Simply change

boolean b = mainWifi.enableNetwork(res, false);

to

boolean b = mainWifi.enableNetwork(res, true); 
like image 141
Lasse Avatar answered Sep 18 '22 19:09

Lasse