android wifi设置连接为静态,动态DHCP(三)

wifi设置为静态IP和动态IP方式 MSTAR 6.0验证

//获取要设置为静态IP的信息 
 public WifiConfiguration setwifistaticIpSettings(WifiConfiguration wifiConfiguration,String wifiip,String wifimasks,
                                      String wifigeteway,String wifidns) {

        wifiConfiguration.setIpAssignment(IpConfiguration.IpAssignment.STATIC);
        StaticIpConfiguration wifistaticConfig = new StaticIpConfiguration();
//设置IP
        Inet4Address inetAddr = null;
        try {
            inetAddr = (Inet4Address) NetworkUtils.numericToInetAddress(wifiip);
        } catch (IllegalArgumentException|ClassCastException e) {
            return null;
        }

//获取换算子网掩码
        int wasks= twoget(wifimasks);
        try {

            wifistaticConfig.ipAddress = new LinkAddress(inetAddr, wasks);
        } catch (NumberFormatException e) {
            return null;
        }
//设置网关
        try {
            wifistaticConfig.gateway =(Inet4Address) NetworkUtils.numericToInetAddress(wifigeteway);
        } catch (IllegalArgumentException|ClassCastException e) {
            return null;
        }
//设置DNS服务器
        try {
            wifistaticConfig.dnsServers.add((Inet4Address) NetworkUtils.numericToInetAddress(wifidns));
        } catch (IllegalArgumentException|ClassCastException e) {
            return null;
        }
//保存wifiConfiguration中
        wifiConfiguration.setStaticIpConfiguration(wifistaticConfig);
        return wifiConfiguration;
    }

//获取换算子网掩码
    public  int twoget( String netwak){
        int wak = 0;

        if(netwak==null){
            return 24;
        }
        StringBuffer netmask = new StringBuffer();
        String[] ipArray = netwak.split("\\.");

        for (int j = 0; j < ipArray.length; j++) {
            String s = ipArray[j];
            String a=Integer.toBinaryString(Integer.valueOf(s));
            String aString2[]=a.split("");
            for (int i = 0; i < aString2.length; i++) {
                if ("1".equals(aString2[i])) {
                    wak++;
                }
            }
        }
        if(wak<=0 && wak>32){
            return 24;
        }

        return wak;


    }
//设置为动态IP 
private void setwifidhcp(){
        WifiConfiguration wifiConfigurationhdcp = wifiAdmin.getWifiConfiguration(wifiAdmin.getNetworkId());
        if(wifiConfigurationhdcp != null){
            if(wifiAdmin.getwificonfigstatic(wifiConfigurationhdcp)) {
                wifiConfigurationhdcp.setIpAssignment(IpConfiguration.IpAssignment.DHCP);
                savewifiinif(wifiConfigurationhdcp);
            }
        }
    }

//保存并设置为动静态IP
private void savewifiinif(WifiConfiguration issetwifistatic ){
        wifiAdmin.getmWifiManager().updateNetwork(issetwifistatic);
        wifiAdmin.getmWifiManager().saveConfiguration();
        int netId =  wifiAdmin.getmWifiManager().addNetwork(issetwifistatic);
        wifiAdmin.getmWifiManager().disableNetwork(netId);//断开网络
        boolean  flag  = wifiAdmin.getmWifiManager().enableNetwork(netId, true);//重新连接
    }

//获取连接wifi的config
    public  WifiConfiguration getWifiConfiguration(int networkId) {
        List configuredNetworks = mWifiManager.getConfiguredNetworks();
        if (configuredNetworks != null) {
            for (WifiConfiguration configuredNetwork : configuredNetworks) {
                if (configuredNetwork.networkId == networkId) {
                    return configuredNetwork;
                }
            }
        }
        return null;
    }

  // 构造器
    public WifiAdmin(Context context) {
        // 取得WifiManager对象
        mWifiManager = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);
        // 取得WifiInfo对象
        mWifiInfo = mWifiManager.getConnectionInfo();
    }

    public WifiManager getmWifiManager(){
        return mWifiManager;
    }

    // 得到连接的ID
    public int getNetworkId() {
        return (mWifiInfo == null) ? 0 : mWifiInfo.getNetworkId();
    }

 

你可能感兴趣的:(安卓)