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

The following examples show how to use android.net.wifi.WifiManager#removeNetwork() . 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: WiFi.java    From WifiConnecter with Apache License 2.0 6 votes vote down vote up
/**
 * Ensure no more than numOpenNetworksKept open networks in configuration list.
 * @param wifiMgr
 * @param numOpenNetworksKept
 * @return Operation succeed or not.
 */
private static boolean checkForExcessOpenNetworkAndSave(final WifiManager wifiMgr, final int numOpenNetworksKept) {
	final List<WifiConfiguration> configurations = wifiMgr.getConfiguredNetworks();
	sortByPriority(configurations);
	
	boolean modified = false;
	int tempCount = 0;
	for(int i = configurations.size() - 1; i >= 0; i--) {
		final WifiConfiguration config = configurations.get(i);
		if(getWifiConfigurationSecurity(config).equals(OPEN)) {
			tempCount++;
			if(tempCount >= numOpenNetworksKept) {
				modified = true;
				wifiMgr.removeNetwork(config.networkId);
			}
		}
	}
	if(modified) {
		return wifiMgr.saveConfiguration();
	}
	
	return true;
}
 
Example 2
Source File: WifiConfigManager.java    From android-apps with MIT License 6 votes vote down vote up
/**
 * Update the network: either create a new network or modify an existing network
 * @param config the new network configuration
 * @return network ID of the connected network.
 */
private static void updateNetwork(WifiManager wifiManager, WifiConfiguration config) {
  Integer foundNetworkID = findNetworkInExistingConfig(wifiManager, config.SSID);
  if (foundNetworkID != null) {
    Log.i(TAG, "Removing old configuration for network " + config.SSID);
    wifiManager.removeNetwork(foundNetworkID);
    wifiManager.saveConfiguration();
  }
  int networkId = wifiManager.addNetwork(config);
  if (networkId >= 0) {
    // Try to disable the current network and start a new one.
    if (wifiManager.enableNetwork(networkId, true)) {
      Log.i(TAG, "Associating to network " + config.SSID);
      wifiManager.saveConfiguration();
    } else {
      Log.w(TAG, "Failed to enable network " + config.SSID);
    }
  } else {
    Log.w(TAG, "Unable to add network " + config.SSID);
  }
}
 
Example 3
Source File: WifiConfigManager.java    From reacteu-app with MIT License 6 votes vote down vote up
/**
 * Update the network: either create a new network or modify an existing network
 * @param config the new network configuration
 * @return network ID of the connected network.
 */
private static void updateNetwork(WifiManager wifiManager, WifiConfiguration config) {
  Integer foundNetworkID = findNetworkInExistingConfig(wifiManager, config.SSID);
  if (foundNetworkID != null) {
    Log.i(TAG, "Removing old configuration for network " + config.SSID);
    wifiManager.removeNetwork(foundNetworkID);
    wifiManager.saveConfiguration();
  }
  int networkId = wifiManager.addNetwork(config);
  if (networkId >= 0) {
    // Try to disable the current network and start a new one.
    if (wifiManager.enableNetwork(networkId, true)) {
      Log.i(TAG, "Associating to network " + config.SSID);
      wifiManager.saveConfiguration();
    } else {
      Log.w(TAG, "Failed to enable network " + config.SSID);
    }
  } else {
    Log.w(TAG, "Unable to add network " + config.SSID);
  }
}
 
Example 4
Source File: WifiConfigManager.java    From barcodescanner-lib-aar with MIT License 6 votes vote down vote up
/**
 * Update the network: either create a new network or modify an existing network
 * @param config the new network configuration
 */
private static void updateNetwork(WifiManager wifiManager, WifiConfiguration config) {
  Integer foundNetworkID = findNetworkInExistingConfig(wifiManager, config.SSID);
  if (foundNetworkID != null) {
    Log.i(TAG, "Removing old configuration for network " + config.SSID);
    wifiManager.removeNetwork(foundNetworkID);
    wifiManager.saveConfiguration();
  }
  int networkId = wifiManager.addNetwork(config);
  if (networkId >= 0) {
    // Try to disable the current network and start a new one.
    if (wifiManager.enableNetwork(networkId, true)) {
      Log.i(TAG, "Associating to network " + config.SSID);
      wifiManager.saveConfiguration();
    } else {
      Log.w(TAG, "Failed to enable network " + config.SSID);
    }
  } else {
    Log.w(TAG, "Unable to add network " + config.SSID);
  }
}
 
Example 5
Source File: WifiConfigManager.java    From Study_Android_Demo with Apache License 2.0 6 votes vote down vote up
/**
 * Update the network: either create a new network or modify an existing network
 * @param config the new network configuration
 */
private static void updateNetwork(WifiManager wifiManager, WifiConfiguration config) {
  Integer foundNetworkID = findNetworkInExistingConfig(wifiManager, config.SSID);
  if (foundNetworkID != null) {
    Log.i(TAG, "Removing old configuration for network " + config.SSID);
    wifiManager.removeNetwork(foundNetworkID);
    wifiManager.saveConfiguration();
  }
  int networkId = wifiManager.addNetwork(config);
  if (networkId >= 0) {
    // Try to disable the current network and start a new one.
    if (wifiManager.enableNetwork(networkId, true)) {
      Log.i(TAG, "Associating to network " + config.SSID);
      wifiManager.saveConfiguration();
    } else {
      Log.w(TAG, "Failed to enable network " + config.SSID);
    }
  } else {
    Log.w(TAG, "Unable to add network " + config.SSID);
  }
}
 
Example 6
Source File: WifiConfigManager.java    From weex with Apache License 2.0 6 votes vote down vote up
/**
 * Update the network: either create a new network or modify an existing network
 * @param config the new network configuration
 */
private static void updateNetwork(WifiManager wifiManager, WifiConfiguration config) {
  Integer foundNetworkID = findNetworkInExistingConfig(wifiManager, config.SSID);
  if (foundNetworkID != null) {
    Log.i(TAG, "Removing old configuration for network " + config.SSID);
    wifiManager.removeNetwork(foundNetworkID);
    wifiManager.saveConfiguration();
  }
  int networkId = wifiManager.addNetwork(config);
  if (networkId >= 0) {
    // Try to disable the current network and start a new one.
    if (wifiManager.enableNetwork(networkId, true)) {
      Log.i(TAG, "Associating to network " + config.SSID);
      wifiManager.saveConfiguration();
    } else {
      Log.w(TAG, "Failed to enable network " + config.SSID);
    }
  } else {
    Log.w(TAG, "Unable to add network " + config.SSID);
  }
}
 
Example 7
Source File: WifiConfigManager.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 6 votes vote down vote up
/**
 * Update the network: either create a new network or modify an existing network
 * @param config the new network configuration
 */
private static void updateNetwork(WifiManager wifiManager, WifiConfiguration config) {
  Integer foundNetworkID = findNetworkInExistingConfig(wifiManager, config.SSID);
  if (foundNetworkID != null) {
    Log.i(TAG, "Removing old configuration for network " + config.SSID);
    wifiManager.removeNetwork(foundNetworkID);
    wifiManager.saveConfiguration();
  }
  int networkId = wifiManager.addNetwork(config);
  if (networkId >= 0) {
    // Try to disable the current network and start a new one.
    if (wifiManager.enableNetwork(networkId, true)) {
      Log.i(TAG, "Associating to network " + config.SSID);
      wifiManager.saveConfiguration();
    } else {
      Log.w(TAG, "Failed to enable network " + config.SSID);
    }
  } else {
    Log.w(TAG, "Unable to add network " + config.SSID);
  }
}
 
Example 8
Source File: WifiConfigManager.java    From ZXing-Standalone-library with Apache License 2.0 6 votes vote down vote up
/**
 * Update the network: either create a new network or modify an existing network
 * @param config the new network configuration
 */
private static void updateNetwork(WifiManager wifiManager, WifiConfiguration config) {
  Integer foundNetworkID = findNetworkInExistingConfig(wifiManager, config.SSID);
  if (foundNetworkID != null) {
    Log.i(TAG, "Removing old configuration for network " + config.SSID);
    wifiManager.removeNetwork(foundNetworkID);
    wifiManager.saveConfiguration();
  }
  int networkId = wifiManager.addNetwork(config);
  if (networkId >= 0) {
    // Try to disable the current network and start a new one.
    if (wifiManager.enableNetwork(networkId, true)) {
      Log.i(TAG, "Associating to network " + config.SSID);
      wifiManager.saveConfiguration();
    } else {
      Log.w(TAG, "Failed to enable network " + config.SSID);
    }
  } else {
    Log.w(TAG, "Unable to add network " + config.SSID);
  }
}
 
Example 9
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 10
Source File: ConnectorUtils.java    From WifiUtils with Apache License 2.0 6 votes vote down vote up
static boolean cleanPreviousConfiguration(@Nullable final WifiManager wifiManager, @Nullable final WifiConfiguration config) {
    //On Android 6.0 (API level 23) and above if my app did not create the configuration in the first place, it can not remove it either.
    if (wifiManager == null) {
        return false;
    }

    wifiLog("Attempting to remove previous network config...");
    if (config == null) {
        return true;
    }

    if (wifiManager.removeNetwork(config.networkId)) {
        wifiManager.saveConfiguration();
        return true;
    }
    return false;
}
 
Example 11
Source File: ConnectorUtils.java    From WifiUtils with Apache License 2.0 6 votes vote down vote up
@RequiresPermission(allOf = {ACCESS_FINE_LOCATION, ACCESS_WIFI_STATE})
static boolean cleanPreviousConfiguration(@Nullable final WifiManager wifiManager, @NonNull final ScanResult scanResult) {
    if (wifiManager == null) {
        return false;
    }
    //On Android 6.0 (API level 23) and above if my app did not create the configuration in the first place, it can not remove it either.
    final WifiConfiguration config = ConfigSecurities.getWifiConfiguration(wifiManager, scanResult);
    wifiLog("Attempting to remove previous network config...");
    if (config == null) {
        return true;
    }

    if (wifiManager.removeNetwork(config.networkId)) {
        wifiManager.saveConfiguration();
        return true;
    }
    return false;
}
 
Example 12
Source File: ConnectorUtils.java    From WifiUtils with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("UnusedReturnValue")
private static boolean checkForExcessOpenNetworkAndSave(@NonNull final ContentResolver resolver, @NonNull final WifiManager wifiMgr) {
    final List<WifiConfiguration> configurations = wifiMgr.getConfiguredNetworks();
    sortByPriority(configurations);

    boolean modified = false;
    int tempCount = 0;
    final int numOpenNetworksKept = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1
            ? Settings.Secure.getInt(resolver, Settings.Global.WIFI_NUM_OPEN_NETWORKS_KEPT, 10)
            : Settings.Secure.getInt(resolver, Settings.Secure.WIFI_NUM_OPEN_NETWORKS_KEPT, 10);

    for (int i = configurations.size() - 1; i >= 0; i--) {
        final WifiConfiguration config = configurations.get(i);
        if (Objects.equals(ConfigSecurities.SECURITY_NONE, ConfigSecurities.getSecurity(config))) {
            tempCount++;
            if (tempCount >= numOpenNetworksKept) {
                modified = true;
                wifiMgr.removeNetwork(config.networkId);
            }
        }
    }
    return !modified || wifiMgr.saveConfiguration();

}
 
Example 13
Source File: WifiConfigManager.java    From zxingfragmentlib with Apache License 2.0 6 votes vote down vote up
/**
 * Update the network: either create a new network or modify an existing network
 * @param config the new network configuration
 */
private static void updateNetwork(WifiManager wifiManager, WifiConfiguration config) {
  Integer foundNetworkID = findNetworkInExistingConfig(wifiManager, config.SSID);
  if (foundNetworkID != null) {
    Log.i(TAG, "Removing old configuration for network " + config.SSID);
    wifiManager.removeNetwork(foundNetworkID);
    wifiManager.saveConfiguration();
  }
  int networkId = wifiManager.addNetwork(config);
  if (networkId >= 0) {
    // Try to disable the current network and start a new one.
    if (wifiManager.enableNetwork(networkId, true)) {
      Log.i(TAG, "Associating to network " + config.SSID);
      wifiManager.saveConfiguration();
    } else {
      Log.w(TAG, "Failed to enable network " + config.SSID);
    }
  } else {
    Log.w(TAG, "Unable to add network " + config.SSID);
  }
}
 
Example 14
Source File: WifiConfigManager.java    From BarcodeEye with Apache License 2.0 6 votes vote down vote up
/**
 * Update the network: either create a new network or modify an existing network
 * @param config the new network configuration
 */
private static void updateNetwork(WifiManager wifiManager, WifiConfiguration config) {
  Integer foundNetworkID = findNetworkInExistingConfig(wifiManager, config.SSID);
  if (foundNetworkID != null) {
    Log.i(TAG, "Removing old configuration for network " + config.SSID);
    wifiManager.removeNetwork(foundNetworkID);
    wifiManager.saveConfiguration();
  }
  int networkId = wifiManager.addNetwork(config);
  if (networkId >= 0) {
    // Try to disable the current network and start a new one.
    if (wifiManager.enableNetwork(networkId, true)) {
      Log.i(TAG, "Associating to network " + config.SSID);
      wifiManager.saveConfiguration();
    } else {
      Log.w(TAG, "Failed to enable network " + config.SSID);
    }
  } else {
    Log.w(TAG, "Unable to add network " + config.SSID);
  }
}
 
Example 15
Source File: WiFiUtils.java    From FimiX8-RE with MIT License 6 votes vote down vote up
public static void removeNetId(Context context, String ssid) {
    if (context != null && ssid != null) {
        WifiManager wifiManager = (WifiManager) context.getSystemService("wifi");
        List<WifiConfiguration> configurationList = wifiManager.getConfiguredNetworks();
        if (configurationList != null) {
            for (WifiConfiguration config : configurationList) {
                if (isEqualWifi(config.SSID, ssid)) {
                    wifiManager.removeNetwork(config.networkId);
                    wifiManager.saveConfiguration();
                    break;
                }
            }
        }
        netId = -1;
    }
}
 
Example 16
Source File: ChangeWifiNetworkTask.java    From arcusandroid with Apache License 2.0 5 votes vote down vote up
/**
 * Attempts to connect to the network defined by the given {@link WifiConfiguration}. Callers
 * should use the {@link WifiConfigurationBuilder} helper class to create valid
 * {@link WifiConfiguration} objects.
 *
 * @param network
 * @return True if the given network could be connected to; false otherwise
 */
private boolean setCurrentWifiNetwork(WifiConfiguration network) {
    WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    if (wifiManager == null) {
        return false;
    }

    logger.debug("Changing wifi to network configuration SSID={} with password={}", network.SSID, network.preSharedKey);

    // If phone already knows how to connect to this network, then delete it!
    if (PhoneWifiHelper.isNetworkConfigured(getApplicationContext(), network.SSID)) {
        logger.debug("Network configuration already exists; deleting it.");

        if (!wifiManager.removeNetwork(network.networkId)) {
            logger.error("Attempt to delete existing network id={} failed. Ignoring.", network.networkId);
        }
    }

    // Try to add this network definition
    if (!PhoneWifiHelper.isNetworkConfigured(getApplicationContext(), network.SSID) && wifiManager.addNetwork(network) < 0) {
        logger.error("Failed to add WifiConfiguration: {}", network);
        return false;
    }

    // Provided we added the network successfully, try to connect to it
    return setCurrentWifiNetwork(network.SSID);
}
 
Example 17
Source File: WifiConfigUtil.java    From android-testdpc with Apache License 2.0 5 votes vote down vote up
private static boolean saveAddedWifiConfiguration(WifiManager wifiManager, int networkId) {
  boolean saveConfigurationSuccess = wifiManager.saveConfiguration();
  if (!saveConfigurationSuccess) {
    wifiManager.removeNetwork(networkId);
    return false;
  }
  return true;
}
 
Example 18
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 19
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);

    }
}
 
Example 20
Source File: PhoneWifiHelper.java    From arcusandroid with Apache License 2.0 4 votes vote down vote up
public static boolean removeConfiguredNetwork (Context context, int networkId) {
    final WifiManager wifiManager = getWiFiManager(context);
    return wifiManager.removeNetwork(networkId);
}