如何在Android 3.x或4.x上以编程方式配置静态IP地址,网络掩码,网关

Yeu*_*ung 31 settings networking android ipv4

我已经检查了Stack Overflow问题API,以便在Android应用程序中配置静态IP地址.

它适用于Android 2.3.但是,在更高的API级别上没有运气.例如,我把设置

android.provider.Settings.System.putString(getContentResolver(), android.provider.Settings.System.WIFI_USE_STATIC_IP, "1");        
android.provider.Settings.System.putString(getContentResolver(), android.provider.Settings.System.WIFI_STATIC_IP, "192.168.0.100");
android.provider.Settings.System.putString(getContentResolver(), android.provider.Settings.System.WIFI_STATIC_NETMASK, "255.255.255.0");
android.provider.Settings.System.putString(getContentResolver(), android.provider.Settings.System.WIFI_STATIC_DNS1, "192.168.0.254");
android.provider.Settings.System.putString(getContentResolver(), android.provider.Settings.System.WIFI_STATIC_GATEWAY, "192.168.0.254");
Run Code Online (Sandbox Code Playgroud)

但我回去查看:

Setting --> Wi-Fi --> Long Press Access Point SSID --> Modify Network --> check Show advanced options
Run Code Online (Sandbox Code Playgroud)

IP Settings领域仍然陈述DHCP但不是Static.

确实,我可以android.provider.Settings.System.getString()用来取回我设定的东西.它证明设置保存在某处,但系统只是忽略它.

系统使用android.provider.Settings.SystemAndroid 3.x和4.x以外的设置,因为设置是根据接入点SSID设置的.我可以修改一个SSID上的设置,就像它在Android 2.3上的工作方式一样吗?

Yeu*_*ung 60

我意识到每个SSID的设置在3.x或4.x上没有API.因此,我查看了源代码,发现每个SSID的配置都存储在android.net.wifi.WifiConfiguration其中android.net.wifi.WifiManager.

在下面的代码中,IpAssignment是Enum STAIC,DHCP或者NONE.并且linkProperties是对象存储的IP地址,网关,DNS等...

linkAddress 是IP地址,其网络掩码为prefixLength(网络掩码中的位数为1).

mRoutesArrayListRouteInfo,可以指示网关.

mDnsesArrayListInetAddress对DNS.

首先,使用WifiConfigurationSSID 获取当前配置

WifiConfiguration wifiConf = null;
WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
WifiInfo connectionInfo = wifiManager.getConnectionInfo();
List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();        
for (WifiConfiguration conf : configuredNetworks){
    if (conf.networkId == connectionInfo.getNetworkId()){
        wifiConf = conf;
        break;              
    }
}
Run Code Online (Sandbox Code Playgroud)

当隐藏IpAssignmentlinkProperties隐藏时,对象可以从反射中获得.

以下方法可以在SSID WifiConfiguration上设置声明的IP地址设置:

    public static void setIpAssignment(String assign , WifiConfiguration wifiConf)
    throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException{
        setEnumField(wifiConf, assign, "ipAssignment");     
    }

    public static void setIpAddress(InetAddress addr, int prefixLength, WifiConfiguration wifiConf)
    throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException,
    NoSuchMethodException, ClassNotFoundException, InstantiationException, InvocationTargetException{
        Object linkProperties = getField(wifiConf, "linkProperties");
        if(linkProperties == null)return;
        Class laClass = Class.forName("android.net.LinkAddress");
        Constructor laConstructor = laClass.getConstructor(new Class[]{InetAddress.class, int.class});
        Object linkAddress = laConstructor.newInstance(addr, prefixLength);

        ArrayList mLinkAddresses = (ArrayList)getDeclaredField(linkProperties, "mLinkAddresses");
        mLinkAddresses.clear();
        mLinkAddresses.add(linkAddress);        
    }

    public static void setGateway(InetAddress gateway, WifiConfiguration wifiConf)
    throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException, 
    ClassNotFoundException, NoSuchMethodException, InstantiationException, InvocationTargetException{
        Object linkProperties = getField(wifiConf, "linkProperties");
        if(linkProperties == null)return;
        Class routeInfoClass = Class.forName("android.net.RouteInfo");
        Constructor routeInfoConstructor = routeInfoClass.getConstructor(new Class[]{InetAddress.class});
        Object routeInfo = routeInfoConstructor.newInstance(gateway);

        ArrayList mRoutes = (ArrayList)getDeclaredField(linkProperties, "mRoutes");
        mRoutes.clear();
        mRoutes.add(routeInfo);
    }

    public static void setDNS(InetAddress dns, WifiConfiguration wifiConf)
    throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException{
        Object linkProperties = getField(wifiConf, "linkProperties");
        if(linkProperties == null)return;

        ArrayList<InetAddress> mDnses = (ArrayList<InetAddress>)getDeclaredField(linkProperties, "mDnses");
        mDnses.clear(); //or add a new dns address , here I just want to replace DNS1
        mDnses.add(dns); 
    }

    public static Object getField(Object obj, String name)
    throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{
        Field f = obj.getClass().getField(name);
        Object out = f.get(obj);
        return out;
    }

    public static Object getDeclaredField(Object obj, String name)
    throws SecurityException, NoSuchFieldException,
    IllegalArgumentException, IllegalAccessException {
        Field f = obj.getClass().getDeclaredField(name);
        f.setAccessible(true);
        Object out = f.get(obj);
        return out;
    }  

    private static void setEnumField(Object obj, String value, String name)
    throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{
        Field f = obj.getClass().getField(name);
        f.set(obj, Enum.valueOf((Class<Enum>) f.getType(), value));
    }
Run Code Online (Sandbox Code Playgroud)

之后,我可以设置WifiConfiguration此SSID的设置和更新.

    try{
        setIpAssignment("STATIC", wifiConf); //or "DHCP" for dynamic setting
        setIpAddress(InetAddress.getByName("192.168.0.100"), 24, wifiConf);
        setGateway(InetAddress.getByName("4.4.4.4"), wifiConf);
        setDNS(InetAddress.getByName("4.4.4.4"), wifiConf);
        wifiManager.updateNetwork(wifiConf); //apply the setting
            wifiManager.saveConfiguration(); //Save it
    }catch(Exception e){
        e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)

编辑:对不起,我没有检查Android 3.x设备是否具有Android 4.x的silmilar UI.在安卓3.x中,网关在storted mGatewayslinkProperties. mGatewaysArraylist类型的InetAddress.因此,以下应该在Android 3.x中工作.

public static void setGateway(InetAddress gateway, WifiConfiguration wifiConf)
        throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException, 
        ClassNotFoundException, NoSuchMethodException, InstantiationException, InvocationTargetException{
            Object linkProperties = getField(wifiConf, "linkProperties");
            if(linkProperties == null)return;
            ArrayList mGateways = (ArrayList)getDeclaredField(linkProperties, "mGateways");
            mGateways.clear();
            mGateways.add(gateway);
        }
Run Code Online (Sandbox Code Playgroud)

EDIT2:所述方法setIpAddress,setGateway,setDNS应当被输入作为InetAddress类型.

  • 第一个代码块中的小错字.if(WifiConf.networkId == connectionInfo.getNetworkId()){should be:if(conf.networkId == connectionInfo.getNetworkId()){And:WifiConf = conf; 应该是:wifiConf = conf; (5认同)
  • 它真的有效吗?我试过3.x甚至4.x版本,没有显示效果.它总是DHCP,它不会更改为静态.因此,解决不会发生任何变化. (2认同)
  • 确保你调用wifiManager.saveConfiguration(); 在wifiManager.updateNetwork(wifiConf)之后; 正确应用设置.参考:http://stackoverflow.com/questions/18136000/assign-static-ip-address-for-wifi-network-on-android-3-x-and-4-x (2认同)