android.net.wifi.WifiManager#saveConfiguration ( )源码实例Demo

下面列出了android.net.wifi.WifiManager#saveConfiguration ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: FimiX8-RE   文件: WiFiUtils.java
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;
}
 
源代码2 项目: FimiX8-RE   文件: WiFiUtils.java
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;
    }
}
 
源代码3 项目: android-apps   文件: WifiConfigManager.java
/**
 * 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);
  }
}
 
源代码4 项目: WifiUtils   文件: ConnectorUtils.java
@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();

}
 
源代码5 项目: WifiUtils   文件: ConnectorUtils.java
@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;
}
 
源代码6 项目: WifiUtils   文件: ConnectorUtils.java
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;
}
 
源代码7 项目: BarcodeEye   文件: WifiConfigManager.java
/**
 * 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);
  }
}
 
/**
 * 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);
  }
}
 
源代码9 项目: WifiConnecter   文件: WiFi.java
/**
 * 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;
}
 
源代码10 项目: weex   文件: WifiConfigManager.java
/**
 * 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);
  }
}
 
源代码11 项目: Study_Android_Demo   文件: WifiConfigManager.java
/**
 * 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);
  }
}
 
源代码12 项目: barcodescanner-lib-aar   文件: WifiConfigManager.java
/**
 * 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);
  }
}
 
源代码13 项目: reacteu-app   文件: WifiConfigManager.java
/**
 * 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);
  }
}
 
源代码14 项目: android-testdpc   文件: WifiConfigUtil.java
private static boolean saveAddedWifiConfiguration(WifiManager wifiManager, int networkId) {
  boolean saveConfigurationSuccess = wifiManager.saveConfiguration();
  if (!saveConfigurationSuccess) {
    wifiManager.removeNetwork(networkId);
    return false;
  }
  return true;
}
 
源代码15 项目: WifiUtils   文件: ConnectorUtils.java
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;
}
 
源代码16 项目: WifiUtils   文件: ConnectorUtils.java
@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());
}
 
源代码17 项目: WifiConnecter   文件: WiFi.java
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;
}
 
源代码18 项目: android-testdpc   文件: WifiConfigUtil.java
private static boolean saveUpdatedWifiConfiguration(WifiManager wifiManager) {
  return wifiManager.saveConfiguration();
}
 
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;
    }
  }
}
 
源代码20 项目: android-wifi-activity   文件: WifiBaseActivity.java
@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);

    }
}