views:

310

answers:

0

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?