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<WifiConfiguration> 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();
    }

 

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值