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?

samugi
  • 395
  • 5
  • 17

1 Answers1

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.

Sreedevi J
  • 673
  • 1
  • 9
  • 15
  • 2
    The problem is that the getConfiguredNetworks() returns null for bssid – Ben Oct 12 '15 at 01:17
  • @Ben 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. – Sreedevi J Nov 06 '15 at 05:06
  • +Sreedevi J What do you mean wifimanager is not ready yet? I am getting null from BSSID as well – x0a Sep 06 '17 at 20:14
  • @x0a 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. – Sreedevi J Sep 11 '17 at 09:19