If you want to set WIFI static IP in the code, you need to have system permission, to add android:sharedUserId= “android.uid. System” in the manifest file, and also have the system signature. Set the WIFI STATIC IP and Ethernet similar before, is to construct StaticIpConfiguration object, and IpConfiguration. IpAssignment. STATIC is set to STATIC IP, DHCP is also available, but WifiConfiguration is available directly from WifiManager, which is easier than Ethernet, without reflection.

Add IP, NETMASK, GATEWAY, and DNS to the StaticIpConfiguration object.

try {
    Inet4Address inetAddr = getIPv4Address(STATIC_IP);
    int prefixLength = NetUtils.maskStr2InetMask(STATIC_NETMASK);
    InetAddress gatewayAddr = getIPv4Address(STATIC_GATEWAY);
    InetAddress dnsAddr = getIPv4Address(STATIC_DNS1);

    Class[] cl = new Class[]{InetAddress.class, int.class};
    Constructor cons = null; Class<? > clazz = Class.forName("android.net.LinkAddress");

    // Get all constructors
    try {
        cons = clazz.getConstructor(cl);
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }

    if (cons == null) {
        return;
    }
    // Assign an initial value to the passed argument
    Object[] x = {inetAddr, prefixLength};

    // Construct the StaticIpConfiguration objectClass<? > staticIpConfigurationCls = Class.forName("android.net.StaticIpConfiguration");
    // Instantiate StaticIpConfiguration
    Object staticIpConfiguration = null;

    staticIpConfiguration = staticIpConfigurationCls.newInstance();
    Field ipAddress = staticIpConfigurationCls.getField("ipAddress");
    Field gateway = staticIpConfigurationCls.getField("gateway");
    Field dnsServers = staticIpConfigurationCls.getField("dnsServers");
    / / set the ipAddress
    ipAddress.set(staticIpConfiguration, (LinkAddress) cons.newInstance(x));
    // Set the gateway
    gateway.set(staticIpConfiguration, gatewayAddr);
    / / set the DNS
    ArrayList<InetAddress> dnsList = (ArrayList<InetAddress>) dnsServers.get(staticIpConfiguration);
    dnsList.add(dnsAddr);
    if(! STATIC_DNS2.isEmpty()) { dnsList.add(getIPv4Address(STATIC_DNS2)); } WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE); WifiConfiguration wifiConfig =null;
    WifiInfo connectionInfo = wifiManager.getConnectionInfo();  // Get the connected wifi network

    @SuppressLint("MissingPermission")
    List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();
    for (WifiConfiguration conf : configuredNetworks) {
        if (conf.networkId == connectionInfo.getNetworkId()) {
            wifiConfig = conf;
            break; }}@SuppressLint("PrivateApi") Class ipAssignmentCls = Class.forName("android.net.IpConfiguration$IpAssignment");
    Object ipAssignment = Enum.valueOf(ipAssignmentCls, "STATIC");
    Method setIpAssignmentMethod = wifiConfig.getClass().getDeclaredMethod("setIpAssignment", ipAssignmentCls);
    setIpAssignmentMethod.invoke(wifiConfig, ipAssignment);
    Method setStaticIpConfigurationMethod = wifiConfig.getClass().getDeclaredMethod("setStaticIpConfiguration", staticIpConfiguration.getClass());
    // Set the StaticIpConfiguration to WifiConfiguration
    setStaticIpConfigurationMethod.invoke(wifiConfig, staticIpConfiguration);
    //WifiConfiguration is readded to WifiManager
    int netId = wifiManager.addNetwork(wifiConfig);
    wifiManager.disableNetwork(netId);
    boolean flag = wifiManager.enableNetwork(netId, true);
} catch (NoSuchFieldException | IllegalAccessException | InstantiationException | InvocationTargetException | ClassNotFoundException | NoSuchMethodException e) {
    e.printStackTrace();
}

Copy the code

SetStaticIpConfiguration in WifiConfiguration’s setStaticIpConfiguration method, setIpAssignment in setIpAssignment, Set the value to STATIC and restart the network according to netId.