android wifi获取静态信息和动态信息.IP.子网掩码.getaway(二)

开发中遇到需要获取连接的wifi的一些信息,以下代码适合有自己编译的系统jar包使用,时间不多没整理有点乱,合起来看就可以。

  private void setwifiinfo(){
        String wifiinfoIp = "";//ip
        String wifiinfomac = "";//mac
        String wifiinfoDNS = "";//dns
        String wifiinfogetaway = "";//getaway
        String wifiinfomask = ""; //子网掩码
        Boolean isdhcp = true; //是否静态动态
        WifiConfiguration wifiConfiguration = wifiAdmin.getWifiConfiguration(wifiAdmin.getNetworkId());
        if(wifiConfiguration != null){
            if (wifiAdmin.getwificonfigstatic(wifiConfiguration)){
                StaticIpConfiguration staticIpConfiguration = wifiConfiguration.getIpConfiguration().staticIpConfiguration;
                isdhcp = false;
                wifiinfoIp = wifiAdmin.getIPAddress();
                wifiinfogetaway = staticIpConfiguration.gateway.getHostAddress();
                wifiinfomask = utils.getmaskutll(staticIpConfiguration.ipAddress.getNetworkPrefixLength());
                Iterator<InetAddress> dns = staticIpConfiguration.dnsServers.iterator();
                if(dns.hasNext()){
                    wifiinfoDNS = dns.next().getHostAddress();
                }

            }else{
                DhcpInfo dhcpInfo = wifiAdmin.getmWifiManager().getDhcpInfo();
                if(dhcpInfo != null){
                    isdhcp = true;
                    wifiinfoIp = utils.getfrmentIPinfo(dhcpInfo.ipAddress);
                    wifiinfogetaway = utils.getfrmentIPinfo(dhcpInfo.gateway);
                    wifiinfomask = utils.getfrmentIPinfo(dhcpInfo.netmask);
                    wifiinfoDNS = utils.getfrmentIPinfo(dhcpInfo.dns1);
                }
            }
        }
        wifiinfomac = wifiAdmin.getMacAddress();
       
    }

为了不影响结构上述代码中的方法提取如下

    public WifiAdmin(Context context) {
        // 取得WifiManager对象
        mWifiManager = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);
        // 取得WifiInfo对象
        mWifiInfo = mWifiManager.getConnectionInfo();
    }
   //获取连接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 Boolean getwificonfigstatic(WifiConfiguration wifiConfiguration){

        return wifiConfiguration.getIpAssignment() == IpConfiguration.IpAssignment.STATIC;
    }
   // 得到连接的ID
    public int getNetworkId() {
        return (mWifiInfo == null) ? 0 : mWifiInfo.getNetworkId();
    }
    // 得到IP地址
    public String getIPAddress() {

        return utils.getfrmentIPinfo((mWifiInfo == null) ? 0 : mWifiInfo.getIpAddress());
    }
    public WifiManager getmWifiManager(){
        return mWifiManager;
    }

util类的

    public String getfrmentIPinfo(int ipadds){
        return Formatter.formatIpAddress(ipadds);
    }
// 得到MAC地址
    public String getMacAddress() {

        return (mWifiInfo == null) ? "NULL" : mWifiInfo.getMacAddress();
    }

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值