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

The following examples show how to use android.net.wifi.WifiManager#updateNetwork() . 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: WifiConfigUtil.java    From android-testdpc with Apache License 2.0 6 votes vote down vote up
/**
 * Saves an existing Wifi configuration, returning the configuration's networkId, or
 * {@link #INVALID_NETWORK_ID} if the operation fails.
 */
private static int updateWifiNetwork(
    WifiManager wifiManager, WifiConfiguration wifiConfiguration) {
  // WifiManager APIs are marked as deprecated but still explicitly supported for DPCs.
  int networkId = wifiManager.updateNetwork(wifiConfiguration);
  if (networkId == INVALID_NETWORK_ID) {
    return INVALID_NETWORK_ID;
  }
  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
    // Saving the configuration is required pre-O.
    if (!saveUpdatedWifiConfiguration(wifiManager)) {
      return INVALID_NETWORK_ID;
    }
  }
  return networkId;
}
 
Example 2
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 3
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 4
Source File: ModHwKeys.java    From GravityBox with Apache License 2.0 5 votes vote down vote up
private static void updateWifiConfig(ArrayList<WifiConfiguration> configList, ResultReceiver receiver) {
    try {
        WifiManager wm = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
        for (WifiConfiguration c : configList) {
            wm.updateNetwork(c);
        }
        wm.saveConfiguration();
        if (receiver != null) {
            receiver.send(0, null);
        }
    } catch (Throwable t) {
        XposedBridge.log(t);
    }
}
 
Example 5
Source File: WiFiProxyUtil.java    From WiFiProxySwitcher with Apache License 2.0 5 votes vote down vote up
/**
 * 低于 Android 5.0 系统使用
 */
private boolean unsetWiFiProxySettings4() {
    WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiConfiguration config = getCurrentWifiConfiguration(manager);
    if (null == config)
        return false;

    try {
        // 获得 linkProperties
        Object linkProperties = getField(config, "linkProperties");
        if (null == linkProperties)
            return false;

        // 获得 setHttpProxy 方法
        Class proxyPropertiesClass = Class.forName("android.net.ProxyProperties");
        Class[] setHttpProxyParams = new Class[1];
        setHttpProxyParams[0] = proxyPropertiesClass;
        Class<?> lpClass = Class.forName("android.net.LinkProperties");
        Method setHttpProxy = lpClass.getDeclaredMethod("setHttpProxy", setHttpProxyParams);
        setHttpProxy.setAccessible(true);

        // 参数传递空, 表示取消 proxy 设置
        Object[] params = new Object[1];
        params[0] = null;
        setHttpProxy.invoke(linkProperties, params);

        setProxySettings("NONE", config);

        // 保存设置
        manager.updateNetwork(config);
        manager.disconnect();
        manager.reconnect();

        ToastUtil.showToast(context, "取消Proxy设置成功");
        return true;
    } catch (Exception e) {
        ToastUtil.showToast(context, "取消Proxy设置失败");
        return false;
    }
}
 
Example 6
Source File: WiFiProxyUtil.java    From WiFiProxySwitcher with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private boolean unSetWiFiProxySettings5() {
    try {
        WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiConfiguration config = getCurrentWifiConfiguration(manager);
        Class proxySettings = Class.forName("android.net.IpConfiguration$ProxySettings");

        Class[] setProxyParams = new Class[2];
        setProxyParams[0] = proxySettings;
        setProxyParams[1] = ProxyInfo.class;

        Method setProxy = WifiConfiguration.class.getDeclaredMethod("setProxy", setProxyParams);
        setProxy.setAccessible(true);

        Object[] methodParams = new Object[2];
        methodParams[0] = Enum.valueOf(proxySettings, "NONE");
        methodParams[1] = null;

        setProxy.invoke(config, methodParams);

        manager.updateNetwork(config);
        manager.disconnect();
        manager.reconnect();

        ToastUtil.showToast(context, "取消Proxy设置成功");
        return true;
    } catch (Exception e) {
        ToastUtil.showToast(context, "取消Proxy设置失败");
        return false;
    }
}
 
Example 7
Source File: WiFi.java    From WifiConnecter with Apache License 2.0 5 votes vote down vote up
/**
 * Change the password of an existing configured network and connect to it
 * @param wifiMgr
 * @param config
 * @param newPassword
 * @return
 */
public static boolean changePasswordAndConnect( final WifiManager wifiMgr, final WifiConfiguration config, final String newPassword, final int numOpenNetworksKept) {
	setupSecurity(config, getWifiConfigurationSecurity(config), newPassword);
	final int networkId = wifiMgr.updateNetwork(config);
	if(networkId == -1) {
		// Update failed.
		return false;
	}
       //确定
	return connectToConfiguredNetwork(wifiMgr, config, true);
}
 
Example 8
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 9
Source File: WiFiProxyUtil.java    From WiFiProxySwitcher with Apache License 2.0 4 votes vote down vote up
/**
 * 低于 Android 5.0 系统
 */
private boolean setWiFiProxySettings4(String ip, int port) {
    // 获得 WifiConfiguration
    WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiConfiguration config = getCurrentWifiConfiguration(manager);
    if (null == config)
        return false;

    try {
        // 从 WifiConfiguration 中获得 linkProperties
        Object linkProperties = getField(config, "linkProperties");
        if (null == linkProperties)
            return false;

        // 获得 setHttpProxy 方法
        Class<?> proxyPropertiesClass = Class.forName("android.net.ProxyProperties");
        Class[] setHttpProxyParams = new Class[1];
        setHttpProxyParams[0] = proxyPropertiesClass;
        Class<?> lpClass = Class.forName("android.net.LinkProperties");
        Method setHttpProxy = lpClass.getDeclaredMethod("setHttpProxy", setHttpProxyParams);
        setHttpProxy.setAccessible(true);

        // 获得 ProxyProperties 构造方法
        Class[] proxyPropertiesCtorParamTypes = new Class[3];
        proxyPropertiesCtorParamTypes[0] = String.class;
        proxyPropertiesCtorParamTypes[1] = int.class;
        proxyPropertiesCtorParamTypes[2] = String.class;

        Constructor proxyPropertiesCtor = proxyPropertiesClass.getConstructor(proxyPropertiesCtorParamTypes);

        // 给构造方法创建参数
        Object[] proxyPropertiesCtorParams = new Object[3];
        proxyPropertiesCtorParams[0] = ip;
        proxyPropertiesCtorParams[1] = port;
        proxyPropertiesCtorParams[2] = null;

        // 创建 ProxyProperties 的对象
        Object proxySettings = proxyPropertiesCtor.newInstance(proxyPropertiesCtorParams);

        // 反射调用 linkProperties 的 setHttpProxy 方法, 参数为 ProxyProperties
        Object[] params = new Object[1];
        params[0] = proxySettings;
        setHttpProxy.invoke(linkProperties, params);

        setProxySettings("STATIC", config);

        // 保存设置
        manager.updateNetwork(config);
        manager.disconnect();
        manager.reconnect();

        ToastUtil.showToast(context, "保存Proxy设置成功");
        return true;
    } catch (Exception e) {
        ToastUtil.showToast(context, "保存Proxy设置失败");
        return false;
    }
}
 
Example 10
Source File: WiFiProxyUtil.java    From WiFiProxySwitcher with Apache License 2.0 4 votes vote down vote up
/**
 * 高于 Android 5.0 系统使用
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private boolean setWiFiProxySettings5(String ip, int port) {
    try {
        WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiConfiguration config = getCurrentWifiConfiguration(manager);
        Class proxySettings = Class.forName("android.net.IpConfiguration$ProxySettings");

        Class[] setProxyParams = new Class[2];
        setProxyParams[0] = proxySettings;
        setProxyParams[1] = ProxyInfo.class;

        Method setProxy = WifiConfiguration.class.getDeclaredMethod("setProxy", setProxyParams);
        setProxy.setAccessible(true);

        ProxyInfo proxyInfo = ProxyInfo.buildDirectProxy(ip, port);

        Object[] methodParams = new Object[2];
        methodParams[0] = Enum.valueOf(proxySettings, "STATIC");
        methodParams[1] = proxyInfo;

        setProxy.invoke(config, methodParams);

        manager.updateNetwork(config);

        String result = getCurrentWifiConfiguration(manager).toString();
        String key = "Proxy settings: ";
        int start = result.indexOf(key) + key.length();
        if (result.substring(start, start + 4).equals("NONE")) {
            throw new RuntimeException("Can't update the Network, you should have the right WifiConfiguration");
        }

        manager.disconnect();
        manager.reconnect();

        ToastUtil.showToast(context, "保存Proxy设置成功");
        return true;
    } catch (Exception e) {
        ToastUtil.showToast(context, "保存Proxy设置失败");
        return false;
    }
}
 
Example 11
Source File: WiFi.java    From WifiConnecter with Apache License 2.0 4 votes vote down vote up
/**
 * Connect to a configured network.
 * @param wifiManager
 * @param config
 * @param numOpenNetworksKept Settings.Secure.WIFI_NUM_OPEN_NETWORKS_KEPT
 * @return
 */
public static boolean connectToConfiguredNetwork( final WifiManager wifiMgr, WifiConfiguration config, boolean reassociate) {
	final String security = getWifiConfigurationSecurity(config);
	
	int oldPri = config.priority;
	// Make it the highest priority.
	int newPri = getMaxPriority(wifiMgr) + 1;
	if(newPri > MAX_PRIORITY) {
		newPri = shiftPriorityAndSave(wifiMgr);
		config = getWifiConfiguration(wifiMgr, config, security);
		if(config == null) {
			return false;
		}
	}
	
	// Set highest priority to this configured network
	config.priority = newPri;
	int networkId = wifiMgr.updateNetwork(config);
	if(networkId == -1) {
		return false;
	}
	
	// Do not disable others
	if(!wifiMgr.enableNetwork(networkId, false)) {
		config.priority = oldPri;
		return false;
	}
	
	if(!wifiMgr.saveConfiguration()) {
		config.priority = oldPri;
		return false;
	}
	
	// We have to retrieve the WifiConfiguration after save.
	config = getWifiConfiguration(wifiMgr, config, security);
	if(config == null) {
		return false;
	}
	
	// Disable others, but do not save.
	// Just to force the WifiManager to connect to it.
	if(!wifiMgr.enableNetwork(config.networkId, true)) {
		return false;
	}
	
	final boolean connect = reassociate ? wifiMgr.reassociate() : wifiMgr.reconnect();
	if(!connect) {
		return false;
	}
	
	return true;
}