设置WiFi静态ip、网关、子网掩码、dns
/*** 设置WiFi静态ip等配置** @param context* @param ip* @param gateWay* @param dns1* @param dns2*/ public boolean changeWifiConfiguration(Context context,String ssid, String ip, String netMask, String gateWay, String dns1, String dns2) {try {InetAddress inetAddr = InetAddress.getByName(ip);int prefixLength =submaskStr2PrefixLen(netMask);InetAddress gatewayAddr = InetAddress.getByName(gateWay);if (TextUtils.isEmpty(dns1)){dns1="0.0.0.0";}InetAddress dns1Addr = InetAddress.getByName(dns1);if (TextUtils.isEmpty(dns2)){dns2="0.0.0.0";}InetAddress dns2Addr = InetAddress.getByName(dns2);//取得所有构造函数Class[] cl = new Class[]{InetAddress.class, int.class};Class<?> clazz = Class.forName("android.net.LinkAddress");Constructor cons = clazz.getConstructor(cl);if (cons == null) {return false;}//给传入参数赋初值Object[] x = {inetAddr, prefixLength};//构造StaticIpConfiguration对象Class<?> staticIpConfigurationCls = Class.forName("android.net.StaticIpConfiguration");//实例化StaticIpConfigurationObject staticIpConfiguration = null;staticIpConfiguration = staticIpConfigurationCls.newInstance();Field ipAddress = staticIpConfigurationCls.getField("ipAddress");Field gateway = staticIpConfigurationCls.getField("gateway");Field dnsServers = staticIpConfigurationCls.getField("dnsServers");//设置ipAddressipAddress.set(staticIpConfiguration, (LinkAddress) cons.newInstance(x));//设置网关gateway.set(staticIpConfiguration, gatewayAddr);//设置dnsArrayList<InetAddress> dnsList = (ArrayList<InetAddress>) dnsServers.get(staticIpConfiguration);dnsList.add(dns1Addr);dnsList.add(dns2Addr);WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(WIFI_SERVICE);WifiConfiguration wifiConfig = null;//得到连接的wifi网络WifiInfo connectionInfo = wifiManager.getConnectionInfo();if (!connectionInfo.getSSID().equals("\"" + ssid + "\"")) {return false;}@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);if (null==wifiConfig.getClass()){return false;}Method setStaticIpConfigurationMethod = wifiConfig.getClass().getDeclaredMethod("setStaticIpConfiguration", staticIpConfiguration.getClass());//设置静态IP,将StaticIpConfiguration设置给WifiConfigurationsetStaticIpConfigurationMethod.invoke(wifiConfig, staticIpConfiguration);//WifiConfiguration重新添加到WifiManagerint netId = wifiManager.addNetwork(wifiConfig);wifiManager.disableNetwork(netId);boolean flag = wifiManager.enableNetwork(netId, true);return flag;} catch (Exception e) {e.printStackTrace();return false;} }/*** 将string类型的子网掩码转为prefixLength,代码如下*/ public static int submaskStr2PrefixLen(String maskStr) throws Exception{StringBuffer sb;String str;int inetmask = 0;int count = 0;//检查子网掩码的格式Pattern pattern = Pattern.compile("(^((\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])\\.){3}(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])$)|^(\\d|[1-2]\\d|3[0-2])$");if (null!=pattern){Matcher matcher = pattern.matcher(maskStr);if (null!=matcher){boolean matches = matcher.matches();if (matches==false){return 0;}}}String[] ipSegment = maskStr.split("\\.");for (int n = 0; n < ipSegment.length; n++) {sb = new StringBuffer(Integer.toBinaryString(Integer.parseInt(ipSegment[n])));str = sb.reverse().toString();count = 0;for (int i = 0; i < str.length(); i++) {i = str.indexOf("1", i);if (i == -1) {break;}count++;}inetmask += count;}return inetmask; }
如上,设置的子网掩码一直都是0
设置WiFi静态ip、网关、子网掩码、dns相关推荐
- Android设置WIFI静态IP
想要在代码中实现设置WIFI静态IP需要有系统权限,要在manifest文件添加android:sharedUserId="android.uid.system",还要有系统签名. ...
- 安卓9.0设置WIFI静态IP地址。
简单的一个函数实现对wifi设置静态或动态IP地址. 一.需要的权限 <uses-permission android:name="android.permission.ACCESS_ ...
- 集群01之xshell链接liux 更改ip 网关及dns 主机名称
1. 安装vmware并新建虚拟机指定镜像文件 (简单略过了) 2.设置 ip 网关 用于链接xshell (nat模式) 2.1 右键点击虚拟机设置,确认虚拟机网络为nat 模式 2.2 编辑- ...
- win10 设置为静态ip地址
问题 有时候明明连接了wifi,网络却不能用,这时候需要修改IP地址配置的方式. 解决方法 设置为自动获取IP地址 打开控制面板 → 网络和Internet → 网路共享中心(查看网络状态和任务)→ ...
- linux配置ip 网关 和dns(转)
原文地址:http://blog.csdn.net/ztz0223/article/details/5800665 Linux下面配置ip很容易的,并没有网上说的那么复杂,我的linux系统是rhel ...
- 如何设置局域网静态ip地址?
一.什么是静态ip地址? 静态IP地址即固定IP地址,是长期固定分配给一台计算机使用的IP地址,一般只是作为特殊服务器的电脑才拥有静态IP地址.静态IP地址是手动设置的. 二.设置静态ip地址有什么好 ...
- android设置静态i,关于Android4.0系统下如何设置wlan静态ip的设置(转)
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 估计有不少人都以为Android4.0系统下如何设置wlan静态ip地址吧,其实不是Android4.0的问题,只是设置上有点区别而已,下面重新教大家如何 ...
- 计算机网络DNS域名配置,如何设置计算机的IP地址和DNS域名服务器
除了路由器设置之外,在发生的网络问题中,IP地址和DNS设置也是最主要的. 以下是一些我为您编译的设置IP地址和DNS名称服务器的方法. 有帮助! 设置IP地址和DNS域名服务器的方法 与常用的Win ...
- 计算机域名DNS设置,电脑的IP地址和DNS域名服务器如何设置
电脑的IP地址和DNS域名服务器如何设置 在日常生活出现的网络问题中,除了路由器设置之外,IP地址和DNS设置是最突出的,今天yjbys小编给大家整理的一些有关设置IP地址和DNS域名服务器的方法,希 ...
最新文章
- 【组队学习】【30期】7. CV中的Transformer
- chrome 请求带上cookie_【编号0002】请求头的内容,及其相关知识铺垫
- 4、Python —— 函数
- 经典网页设计:15个使用网格系统的精美网站作品
- 关于web开发字符集问题解决方法
- java 图形库_OpenGL开放图形java库jogamp-all-platforms.7z
- 警惕Oracle DB操作高压线
- Win CE下编译选项介绍
- 本地跨域处理ajax,Node.js配合node-http-proxy解决本地开发ajax跨域问题
- WPF教程尝试(修正部分格式)
- 100句美文、150个哲理成语和9篇课本素材运用
- (一)AC6(System Workbench for STM32)简单介绍及创建一个简单的STM32的工程
- python语言表白语句简单_简短的表白语句280句
- P001【项目一】客户信息管理软件_Customer类(2)
- 分部积分题型总结笔记(分部积分超强拓展)
- pytorch深度学习基础(九)——深入浅析卷积核
- go 格式化占位符详解
- 微信小程序--超简单贪吃蛇
- 百度大脑UNIT升级,五步完成对话模型私有化部署
- springboot文献综述
热门文章
- 芭蕾、体操和乒乓球的神奇转换力量
- Google面试官:不给我留提问时间,怎么给你 hire?
- ReSharper C++ 2021.2,以帮助您实现代码的现代化
- 女程序员向同事发200多条信息表白, 对方一条信息没回, 精神失常
- 2021年11月中国乘用车品牌出口量排行榜:MGZS出口量突破2万辆,Top前五中有3个品牌归属上海股份(附月榜TOP150详单)
- uniapp 小程序map地图上显示多个酷炫动态的标点,头像后端传过来,真机测试有效
- 【iOS开发进阶】-AVPlayer视频播放
- miniprogram-to-uniapp使用指南(各种小程序项目转换为uni-app项目)
- 基于NodeJS的漫画之家网站
- 迈克尔·杰克逊简历流行音乐之王