Donal,

I think you are not matching the SSIDs of known and live networks correctly.

The SSIDs in the "known" list are enclosed in double quotes:

http://developer.android.com/reference/android/net/wifi/WifiConfiguration.html#SSID

The SSIDs in the live scan result list are "as is", without the quotes.

Your code needs to take this into consideration when matching the networks between the two lists.

BTW, feel free to download and try my WiFi management tool, I think it should make it easier to see what's going on. See the link in my signature below.

--
Kostya Vasilyev ~ WiFi Manager + pretty widget ~ 
http://kmansoft.wordpress.com/sw


14.05.2010 14:33, Donal Rafferty ?????:
Thanks for that Kostya

I have come across a bit of a problem though.

After a scan there are 11 AP's found and all 11 show up in the list in the Wifi settings on the device.

But when I use getConfiguredNetworks <http://developer.android.com/reference/android/net/wifi/WifiManager.html#getConfiguredNetworks>() it only returns 4 configured networks.

This happens despite the 11 AP's showing up in the settings after a scan.

So the majority of the time when I compared the AP with the best signal's SSID with the SSID's in the Configured Network list the majority of the time no matching SSID is found.

In that case I try to add a new network but this ends up creating duplicates of the AP in the settings list.

On Thu, May 13, 2010 at 5:24 PM, Kostya Vasilyev <[email protected] <mailto:[email protected]>> wrote:

    Hi,
    1. Regarding duplicates. Android maintains a list of "known"
    networks - for each, it stores the SSID, encryption configuration,
    along with the password, if necessary. Each known network also has
    a unique numeric ID.
    You can only connect to networks on this "known" list, because, as
    you correctly figured out, connecting to a network is done by its id.
    However, it's not necessary to add the network to this list if
    it's already been added (by Android or by your application).
    You can get the list of known networks from the WifiManager:
    
http://developer.android.com/reference/android/net/wifi/WifiManager.html#getConfiguredNetworks()
    You can match these networks to your live scan results using their
    SSIDs. If the network is already in the known list, just use its
    numeric id. If not, you get the numeric ID back from addNetwork().
    2. Once you have the numeric ID, call enableNetwork(networkId,
    true) to connect. The second parameter means "disconnect from
    others", not "forget all other network" as its name might seem to
    imply.
    -- Kostya Vasilyev

    2010/5/13 [email protected] <mailto:[email protected]>
    <[email protected] <mailto:[email protected]>>

        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?

        --
        You received this message because you are subscribed to the Google
        Groups "Android Developers" group.
        To post to this group, send email to
        [email protected]
        <mailto:[email protected]>
        To unsubscribe from this group, send email to
        [email protected]
        <mailto:android-developers%[email protected]>
        For more options, visit this group at
        http://groups.google.com/group/android-developers?hl=en


-- You received this message because you are subscribed to the Google
    Groups "Android Developers" group.
    To post to this group, send email to
    [email protected]
    <mailto:[email protected]>
    To unsubscribe from this group, send email to
    [email protected]
    <mailto:android-developers%[email protected]>
    For more options, visit this group at
    http://groups.google.com/group/android-developers?hl=en


--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


--
Kostya Vasilyev ~ WiFi Manager + pretty widget ~ 
http://kmansoft.wordpress.com/sw

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to