Check if ScanResult network is already configured (exists in getConfiguredNetworks() list)
Asked Answered
H

1

7

I need to check for every network returned by getScanResults() method if it is already configured in the device, that is, I need to check if it exist in the list returned by getConfiguredNetworks(). The problem is: how can I do this since the only parameter they have in common is SSID? I know this wouldn't be the good way to do it because there could be more networks with same SSID. As stated in the reference, networkId is The ID number that the supplicant uses to identify this network configuration entry, but I can't find something similar for the ScanResult object.

So if this is my receiver:

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
registerReceiver(new BroadcastReceiver()
       {
           @Override
           public void onReceive(Context c, Intent intent) 
           {
              results = wifi.getScanResults();
              size = results.size();
           }
       }, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); 

and this is how i get the configured networks:

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

Is there a way to check if list.get(i) corresponds to results.get(j) configuration, for whatever i or j?

Habitue answered 6/8, 2013 at 9:32 Comment(0)
S
4

You can check if the BSSIDs of both the networks match. ScanResult and WifiConfiguration both supply a BSSID, which is unique to a network.

Silverside answered 5/11, 2013 at 4:42 Comment(4)
The problem is that the getConfiguredNetworks() returns null for bssidChlorobenzene
@Chlorobenzene The only times you will get a null for BSSID are: 1. The wificonfiguration was manually saved and the bssid was incorrectly stored 2. The wifimanager is not ready yet (this happens only rarely and unlikely in non-custom builds.Silverside
+Sreedevi J What do you mean wifimanager is not ready yet? I am getting null from BSSID as wellAbie
@Abie in case of custom builds, you could have the order changed to get an early start on some components that use wifi manager. If that happens, then it is not guaranteed that the wifi service would be up yet, read and populated it's wifi configurations from the conf files. This is rare and will not happen if you are simply building an apk as third party. BSSID can also be null if you are saving a wifi configuration (such as "addNetwork") and not populating the BSSID when connection is established successfully.Silverside

© 2022 - 2024 — McMap. All rights reserved.