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

The following examples show how to use android.net.wifi.WifiManager#saveConfiguration() . 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 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 2
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 3
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 4
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 5
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 6
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 7
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 8
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 9
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 10
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 11
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 12
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 13
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 14
Source File: WiFi.java    From WifiConnecter with Apache License 2.0 5 votes vote down vote up
private static int shiftPriorityAndSave(final WifiManager wifiMgr) {
	final List<WifiConfiguration> configurations = wifiMgr.getConfiguredNetworks();
	sortByPriority(configurations);
	final int size = configurations.size();
	for(int i = 0; i < size; i++) {
		final WifiConfiguration config = configurations.get(i);
		config.priority = i;
		wifiMgr.updateNetwork(config);
	}
	wifiMgr.saveConfiguration();
	return size;
}
 
Example 15
Source File: ConnectorUtils.java    From WifiUtils with Apache License 2.0 5 votes vote down vote up
@RequiresPermission(allOf = {ACCESS_FINE_LOCATION, ACCESS_WIFI_STATE})
private static boolean connectToConfiguredNetwork(@Nullable WifiManager wifiManager, @Nullable WifiConfiguration config, boolean reassociate) {
    if (config == null || wifiManager == null) {
        return false;
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return disableAllButOne(wifiManager, config) && (reassociate ? wifiManager.reassociate() : wifiManager.reconnect());
    }

    // Make it the highest priority.
    int newPri = getMaxPriority(wifiManager) + 1;
    if (newPri > MAX_PRIORITY) {
        newPri = shiftPriorityAndSave(wifiManager);
        config = ConfigSecurities.getWifiConfiguration(wifiManager, config);
        if (config == null) {
            return false;
        }
    }

    // Set highest priority to this configured network
    config.priority = newPri;
    int networkId = wifiManager.updateNetwork(config);
    if (networkId == -1) {
        return false;
    }

    // Do not disable others
    if (!wifiManager.enableNetwork(networkId, false)) {
        return false;
    }

    if (!wifiManager.saveConfiguration()) {
        return false;
    }

    // We have to retrieve the WifiConfiguration after save.
    config = ConfigSecurities.getWifiConfiguration(wifiManager, config);
    return config != null && disableAllButOne(wifiManager, config) && (reassociate ? wifiManager.reassociate() : wifiManager.reconnect());
}
 
Example 16
Source File: ConnectorUtils.java    From WifiUtils with Apache License 2.0 5 votes vote down vote up
private static int shiftPriorityAndSave(@Nullable final WifiManager wifiMgr) {
    if (wifiMgr == null) {
        return 0;
    }
    final List<WifiConfiguration> configurations = wifiMgr.getConfiguredNetworks();
    sortByPriority(configurations);
    final int size = configurations.size();
    for (int i = 0; i < size; i++) {
        final WifiConfiguration config = configurations.get(i);
        config.priority = i;
        wifiMgr.updateNetwork(config);
    }
    wifiMgr.saveConfiguration();
    return size;
}
 
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: WifiConfigUtil.java    From android-testdpc with Apache License 2.0 4 votes vote down vote up
private static boolean saveUpdatedWifiConfiguration(WifiManager wifiManager) {
  return wifiManager.saveConfiguration();
}
 
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);

    }
}