Java Code Examples for android.net.wifi.WifiManager#getConfiguredNetworks()

The following examples show how to use android.net.wifi.WifiManager#getConfiguredNetworks() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WiFiUtils.java    From FimiX8-RE with MIT License 6 votes vote down vote up
public static boolean connectWifi(WifiManager wifiManager, int netId) {
    if (netId == -1) {
        return false;
    }
    List<WifiConfiguration> wifiConfigList = wifiManager.getConfiguredNetworks();
    if (wifiConfigList == null || wifiConfigList.size() <= 0) {
        return false;
    }
    for (int i = 0; i < wifiConfigList.size(); i++) {
        if (((WifiConfiguration) wifiConfigList.get(i)).networkId == netId) {
            boolean connectOkay = wifiManager.enableNetwork(netId, true);
            wifiManager.saveConfiguration();
            return connectOkay;
        }
    }
    return false;
}
 
Example 2
Source File: WifiSsidPreference.java    From syncthing-android with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Load the configured WiFi networks, sort them by SSID.
 *
 * @return a sorted array of WifiConfiguration, or null, if data cannot be retrieved
 */
private WifiConfiguration[] loadConfiguredNetworksSorted() {
    WifiManager wifiManager = (WifiManager)
            getContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    if (wifiManager != null) {
        List<WifiConfiguration> configuredNetworks = null;
        try {
            configuredNetworks = wifiManager.getConfiguredNetworks();
        } catch (SecurityException e) {
            // See changes in Android Q, https://developer.android.com/reference/android/net/wifi/WifiManager.html#getConfiguredNetworks()
        }
        // if WiFi is turned off, getConfiguredNetworks returns null on many devices
        if (configuredNetworks != null) {
            WifiConfiguration[] result = configuredNetworks.toArray(new WifiConfiguration[configuredNetworks.size()]);
            Arrays.sort(result, (lhs, rhs) -> {
                // See #620: There may be null-SSIDs
                String l = lhs.SSID != null ? lhs.SSID : "";
                String r = rhs.SSID != null ? rhs.SSID : "";
                return l.compareToIgnoreCase(r);
            });
            return result;
        }
    }
    // WiFi is turned off or device doesn't have WiFi
    return null;
}
 
Example 3
Source File: OBConnectionManager.java    From GLEXP-Team-onebillion with Apache License 2.0 6 votes vote down vote up
public void forgetAllNetworks()
{
    WifiManager wifiManager = (WifiManager) MainActivity.mainActivity.getApplicationContext().getSystemService(MainActivity.WIFI_SERVICE);
    List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
    //
    for( WifiConfiguration i : list )
    {
        // Please Note: the app cannot make the system forget wifi configurations that were placed by the user or other apps.
        // Just the ones that this app has set.
        wifiManager.removeNetwork(i.networkId);
        wifiManager.saveConfiguration();
    }
}
 
Example 4
Source File: WifiScanWorker.java    From PhoneProfilesPlus with Apache License 2.0 5 votes vote down vote up
private static String getSSID(WifiManager wifiManager, WifiInfo wifiInfo, List<WifiSSIDData> wifiConfigurationList)
{
    String SSID = wifiInfo.getSSID();
    if (SSID == null)
        SSID = "";
    SSID = SSID.replace("\"", "");

    if (SSID.isEmpty())
    {
        if (wifiConfigurationList != null)
        {
            for (WifiSSIDData wifiConfiguration : wifiConfigurationList)
            {
                /*if ((wifiConfiguration.bssid != null) &&
                        (wifiConfiguration.bssid.equals(wifiInfo.getBSSID())))
                    return wifiConfiguration.ssid.replace("\"", "");*/
                if ((wifiConfiguration.ssid != null) &&
                        (wifiConfiguration.ssid.equals(wifiInfo.getSSID())))
                    return wifiConfiguration.ssid.replace("\"", "");
            }
        }
    }

    if (SSID.equals("<unknown ssid>")) {
        List<WifiConfiguration> listOfConfigurations = wifiManager.getConfiguredNetworks();

        if (listOfConfigurations != null) {
            for (int index = 0; index < listOfConfigurations.size(); index++) {
                WifiConfiguration configuration = listOfConfigurations.get(index);
                if (configuration.networkId == wifiInfo.getNetworkId()) {
                    return configuration.SSID;
                }
            }
        }
    }

    return SSID;
}
 
Example 5
Source File: WiFiProxyUtil.java    From WiFiProxySwitcher with Apache License 2.0 5 votes vote down vote up
private WifiConfiguration getCurrentWifiConfiguration(WifiManager manager) {
    if (!manager.isWifiEnabled())
        return null;

    List<WifiConfiguration> configurationList = manager.getConfiguredNetworks();
    WifiConfiguration configuration = null;
    int networkId = manager.getConnectionInfo().getNetworkId();
    for (int i = 0; i < configurationList.size(); ++i) {
        WifiConfiguration wifiConfiguration = configurationList.get(i);
        if (wifiConfiguration.networkId == networkId)
            configuration = wifiConfiguration;
    }

    return configuration;
}
 
Example 6
Source File: ConnectorUtils.java    From WifiUtils with Apache License 2.0 5 votes vote down vote up
static void reenableAllHotspots(@Nullable WifiManager wifi) {
    if (wifi == null) {
        return;
    }
    final List<WifiConfiguration> configurations = wifi.getConfiguredNetworks();
    if (configurations != null && !configurations.isEmpty()) {
        for (final WifiConfiguration config : configurations) {
            wifi.enableNetwork(config.networkId, false);
        }
    }
}
 
Example 7
Source File: WifiConfigManager.java    From reacteu-app with MIT License 5 votes vote down vote up
private static Integer findNetworkInExistingConfig(WifiManager wifiManager, String ssid) {
  List<WifiConfiguration> existingConfigs = wifiManager.getConfiguredNetworks();
  for (WifiConfiguration existingConfig : existingConfigs) {
    if (existingConfig.SSID.equals(ssid)) {
      return existingConfig.networkId;
    }
  }
  return null;
}
 
Example 8
Source File: WifiManagerModule.java    From react-native-wifi-manager with MIT License 5 votes vote down vote up
private static Integer findNetworkInExistingConfig(WifiManager wifiManager, String ssid) {
  List<WifiConfiguration> existingConfigs = wifiManager.getConfiguredNetworks();
  for (WifiConfiguration existingConfig : existingConfigs) {
    if (existingConfig.SSID.equals(ssid)) {
      return existingConfig.networkId;
    }
  }
  return null;
}
 
Example 9
Source File: WiFi.java    From WifiConnecter with Apache License 2.0 5 votes vote down vote up
public static WifiConfiguration getWifiConfiguration(final WifiManager wifiMgr,
                                                        final WifiConfiguration configToFind, String security) {
	final String ssid = configToFind.SSID;
	if(ssid.length() == 0) {
		return null;
	}
	
	final String bssid = configToFind.BSSID;

	
	if(security == null) {
		security = getWifiConfigurationSecurity(configToFind);
	}
	
	final List<WifiConfiguration> configurations = wifiMgr.getConfiguredNetworks();

	for(final WifiConfiguration config : configurations) {
		if(config.SSID == null || !ssid.equals(config.SSID)) {
			continue;
		}
		if(config.BSSID == null || bssid == null || bssid.equals(config.BSSID)) {
			final String configSecurity = getWifiConfigurationSecurity(config);
			if(security.equals(configSecurity)) {
				return config;
			}
		}
	}
	return null;
}
 
Example 10
Source File: ConnectorUtils.java    From WifiUtils with Apache License 2.0 5 votes vote down vote up
private static int getMaxPriority(@Nullable final WifiManager wifiManager) {
    if (wifiManager == null) {
        return 0;
    }
    final List<WifiConfiguration> configurations = wifiManager.getConfiguredNetworks();
    int pri = 0;
    for (final WifiConfiguration config : configurations) {
        if (config.priority > pri) {
            pri = config.priority;
        }
    }
    return pri;
}
 
Example 11
Source File: SsidUtil.java    From PresencePublisher with MIT License 5 votes vote down vote up
public static List<String> getKnownSsids(final Context context) {
    List<String> ssids = new ArrayList<>();
    WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    if (wifiManager == null) {
        HyperLog.w(TAG, "Unable to get WifiManager");
        return ssids;
    }
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
        //noinspection deprecation
        List<WifiConfiguration> configuredNetworks = null;
        try {
            //noinspection deprecation
            configuredNetworks = wifiManager.getConfiguredNetworks();
        } catch (SecurityException e) {
            HyperLog.w(TAG, "Not allowed to get configured networks. " +
                    "As ACCESS_FINE_LOCATION was only added as required in Android Q, this should never happen");
        }
        if (configuredNetworks == null) {
            HyperLog.w(TAG, "No wifi list found");
        } else {
            //noinspection deprecation
            for (WifiConfiguration configuration : configuredNetworks) {
                //noinspection deprecation
                String ssid = normalizeSsid(configuration.SSID);
                if (ssid != null) {
                    ssids.add(ssid);
                }
            }
        }
    }
    if (ssids.isEmpty()) {
        HyperLog.w(TAG, "No known networks found");
        String currentSsid = getCurrentSsid(context);
        if (currentSsid != null) {
            ssids.add(currentSsid);
        }
    }
    return ssids;
}
 
Example 12
Source File: WiFi.java    From WifiConnecter with Apache License 2.0 5 votes vote down vote up
public static WifiConfiguration getWifiConfiguration(final WifiManager wifiMgr,
                                                        final ScanResult hotsopt, String hotspotSecurity) {
	final String ssid = StringUtils.convertToQuotedString(hotsopt.SSID);
	if(ssid.length() == 0) {
		return null;
	}
	
	final String bssid = hotsopt.BSSID;
	if(bssid == null) {
		return null;
	}
	
	if(hotspotSecurity == null) {
		hotspotSecurity = getScanResultSecurity(hotsopt);
	}
	
	final List<WifiConfiguration> configurations = wifiMgr.getConfiguredNetworks();

	for(final WifiConfiguration config : configurations) {
		if(config.SSID == null || !ssid.equals(config.SSID)) {
			continue;
		}
		if(config.BSSID == null || bssid.equals(config.BSSID)) {
			final String configSecurity = getWifiConfigurationSecurity(config);
			if(hotspotSecurity.equals(configSecurity)) {
				return config;
			}
		}
	}
	return null;
}
 
Example 13
Source File: ConfigSecurities.java    From WifiUtils with Apache License 2.0 5 votes vote down vote up
@RequiresPermission(allOf = {ACCESS_FINE_LOCATION, ACCESS_WIFI_STATE})
@Nullable
static WifiConfiguration getWifiConfiguration(@NonNull final WifiManager wifiManager, @NonNull final String ssid) {
    final List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();
    final String findSSID = ('"' + ssid + '"');

    for (final WifiConfiguration wifiConfiguration : configuredNetworks) {
        if (wifiConfiguration.SSID.equals(findSSID)) {
            return wifiConfiguration;
        }
    }

    return null;
}
 
Example 14
Source File: WifiConfigManager.java    From zxingfragmentlib with Apache License 2.0 5 votes vote down vote up
private static Integer findNetworkInExistingConfig(WifiManager wifiManager, String ssid) {
  Iterable<WifiConfiguration> existingConfigs = wifiManager.getConfiguredNetworks();
  for (WifiConfiguration existingConfig : existingConfigs) {
    String existingSSID = existingConfig.SSID;
    if (existingSSID != null && existingSSID.equals(ssid)) {
      return existingConfig.networkId;
    }
  }
  return null;
}
 
Example 15
Source File: WifiConfigManager.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 5 votes vote down vote up
private static Integer findNetworkInExistingConfig(WifiManager wifiManager, String ssid) {
  Iterable<WifiConfiguration> existingConfigs = wifiManager.getConfiguredNetworks();
  if (existingConfigs != null) {
    for (WifiConfiguration existingConfig : existingConfigs) {
      String existingSSID = existingConfig.SSID;
      if (existingSSID != null && existingSSID.equals(ssid)) {
        return existingConfig.networkId;
      }
    }
  }
  return null;
}
 
Example 16
Source File: WifiConfigManager.java    From Study_Android_Demo with Apache License 2.0 5 votes vote down vote up
private static Integer findNetworkInExistingConfig(WifiManager wifiManager, String ssid) {
  Iterable<WifiConfiguration> existingConfigs = wifiManager.getConfiguredNetworks();
  if (existingConfigs != null) {
    for (WifiConfiguration existingConfig : existingConfigs) {
      String existingSSID = existingConfig.SSID;
      if (existingSSID != null && existingSSID.equals(ssid)) {
        return existingConfig.networkId;
      }
    }
  }
  return null;
}
 
Example 17
Source File: Wifi.java    From Multiwii-Remote with Apache License 2.0 5 votes vote down vote up
private WifiConfiguration getConfiguredNetwork(WifiManager wifiManager,	String ssid) {
	List<WifiConfiguration> myList = wifiManager.getConfiguredNetworks();
	for (WifiConfiguration current : myList) {
		String wifiConfigSSID = current.SSID.replace("\"", "");
		if (wifiConfigSSID.equalsIgnoreCase(ssid))
			return current;
	}
	return null;
}
 
Example 18
Source File: WifiConfigManager.java    From BarcodeEye with Apache License 2.0 5 votes vote down vote up
private static Integer findNetworkInExistingConfig(WifiManager wifiManager, String ssid) {
  Iterable<WifiConfiguration> existingConfigs = wifiManager.getConfiguredNetworks();
  for (WifiConfiguration existingConfig : existingConfigs) {
    if (existingConfig.SSID.equals(ssid)) {
      return existingConfig.networkId;
    }
  }
  return null;
}
 
Example 19
Source File: WifiManagerModule.java    From react-native-wifi-manager with MIT License 4 votes vote down vote up
public void connectTo(ScanResult result, String password, String ssid) {
  //Make new configuration
  WifiConfiguration conf = new WifiConfiguration();
  conf.SSID = "\"" + ssid + "\"";
  String Capabilities = result.capabilities;
  if (Capabilities.contains("WPA2")) {
    conf.preSharedKey = "\"" + password + "\"";
  } else if (Capabilities.contains("WPA")) {
    conf.preSharedKey = "\"" + password + "\"";
  } else if (Capabilities.contains("WEP")) {
    conf.wepKeys[0] = "\"" + password + "\"";
    conf.wepTxKeyIndex = 0;
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
    conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
  } else {
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
  }
  //Remove the existing configuration for this netwrok
  WifiManager mWifiManager = (WifiManager) getReactApplicationContext().getSystemService(Context.WIFI_SERVICE);
  List<WifiConfiguration> mWifiConfigList = mWifiManager.getConfiguredNetworks();
  String comparableSSID = ('"' + ssid + '"'); //Add quotes because wifiConfig.SSID has them
  for(WifiConfiguration wifiConfig : mWifiConfigList){
    if(wifiConfig.SSID.equals(comparableSSID)){
      int networkId = wifiConfig.networkId;
      mWifiManager.removeNetwork(networkId);
      mWifiManager.saveConfiguration();
    }
  }
  //Add configuration to Android wifi manager settings...
   WifiManager wifiManager = (WifiManager) getReactApplicationContext().getSystemService(Context.WIFI_SERVICE);
   mWifiManager.addNetwork(conf);
  //Enable it so that android can connect
  List < WifiConfiguration > list = mWifiManager.getConfiguredNetworks();
  for (WifiConfiguration i: list) {
    if (i.SSID != null && i.SSID.equals("\"" + ssid + "\"")) {
      wifiManager.disconnect();
      wifiManager.enableNetwork(i.networkId, true);
      wifiManager.reconnect();
      break;
    }
  }
}
 
Example 20
Source File: WifiBaseActivity.java    From android-wifi-activity with Creative Commons Zero v1.0 Universal 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
    List<ScanResult> scanResultList = wifi.getScanResults();
    boolean found = false;
    String security = null;
    for (ScanResult scanResult : scanResultList) {
        if (scanResult.SSID.equals(getWifiSSID())) {
            security = getScanResultSecurity(scanResult);
            found = true;
            break; // found don't need continue
        }
    }
    if (!found) {
        // if no wifi network with the specified ssid is not found exit
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
        new AlertDialog.Builder(WifiBaseActivity.this)
                .setCancelable(false)
                .setMessage(String.format(getString(R.string.wifi_not_found), getWifiSSID()))
                .setPositiveButton(getString(R.string.exit_app), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        unregisterReceiver(ScanReceiver.this);
                        finish();
                    }
                })
                .show();
    } else {
        // configure based on security
        final WifiConfiguration conf = new WifiConfiguration();
        conf.SSID = "\"" + getWifiSSID() + "\"";
        switch (security) {
            case WEP:
                conf.wepKeys[0] = "\"" + getWifiPass() + "\"";
                conf.wepTxKeyIndex = 0;
                conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
                break;
            case PSK:
                conf.preSharedKey = "\"" + getWifiPass() + "\"";
                break;
            case OPEN:
                conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                break;
        }
        connectionReceiver = new ConnectionReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
        intentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
        registerReceiver(connectionReceiver, intentFilter);
        List<WifiConfiguration> list = wifi.getConfiguredNetworks();
        for( WifiConfiguration i : list ) {
            wifi.removeNetwork(i.networkId);
            wifi.saveConfiguration();
        }
        int netId = wifi.addNetwork(conf);
        wifi.enableNetwork(netId, true);
        wifi.reconnect();
        unregisterReceiver(this);

    }
}