Android 修改盒子路由方式 Static / DHCP

简介: Android 修改盒子路由方式 Static / DHCP
 //操作 盒子WiFi 开关的方法 false 表示关闭  true 表示打开
    public static boolean wifiSwitch(Context context,boolean open){
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (wifiManager.isWifiEnabled() && !open) {
            wifiManager.setWifiEnabled(false);
        } else if (!wifiManager.isWifiEnabled() && open){
            wifiManager.setWifiEnabled(true);
        }
        return true;
    }
 
    //设置盒子 上网路由方式  static/DHCP
    public static void setStaticIP(Context context,String type,String ip) {
        EthernetManager mEthManager = (EthernetManager) context.getSystemService(Context.ETHERNET_SERVICE);
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        switch (type) {
            case "Static":
                StaticIpConfiguration mStaticIpConfiguration = new StaticIpConfiguration();
                String[] split = getDns(HwContext.getContext()).split(" / ");
                for (int i = 0; i < split.length && !TextUtils.isEmpty(split[i]); i++) {
                    InetAddress inetAddress=android.net.NetworkUtils.numericToInetAddress(split[i]);
                    mStaticIpConfiguration.dnsServers.add(inetAddress);
                }
                Inet4Address inetAddr = getIPv4Address(ip);
                int prefixLength = NetUtils.maskStr2InetMask(NetworkUtils.getLanMask(HwContext.getContext()));
                InetAddress gatewayAddr = getIPv4Address(NetworkUtils.getDefaultGateway(HwContext.getContext()));
                if (inetAddr.getAddress().toString().isEmpty() || prefixLength ==0 ) {
                    return ;
                }
 
                mStaticIpConfiguration.ipAddress = new LinkAddress(ip+"/"+prefixLength);
 
                mStaticIpConfiguration.gateway=gatewayAddr;
                switch (NetworkUtils.getActiveNetworkType(getContext())) {
                    case "Ethernet":
                        LogUtils.e("Ethernet");
                        IpConfiguration mIpConfiguration = new IpConfiguration(IpConfiguration.IpAssignment.STATIC, IpConfiguration.ProxySettings.NONE, mStaticIpConfiguration, null);
                        mEthManager.setConfiguration("eth0",mIpConfiguration);
                        break;
                    case "WiFi":
                        LogUtils.e("\"WiFi\"");
                        IpConfiguration wifiIpConfiguration = new IpConfiguration(IpConfiguration.IpAssignment.STATIC, IpConfiguration.ProxySettings.NONE, mStaticIpConfiguration, null);
                        WifiConfiguration wifiConfiguration = getCurrentConfig();
                        wifiConfiguration.setIpConfiguration(wifiIpConfiguration);
                        wifiManager.setWifiApConfiguration(wifiConfiguration);
                        wifiManager.updateNetwork(wifiConfiguration);
                        new Thread(){// 这里比较耗时 所以开启新线程进行处理
                            @Override
                            public void run() {
                                super.run();
                                int netId = wifiManager.addNetwork(wifiConfiguration);
                                wifiManager.disableNetwork(netId);
                                boolean b = wifiManager.enableNetwork(netId, true);
                            }
                        }.start();
                        break;
                }
                break;
            case "DHCP":
                LogUtils.e("DHCP");
                switch (NetworkUtils.getActiveNetworkType(getContext())){
 
                    case "Ethernet":
                        LogUtils.e("Ethernet");
                        IpConfiguration ipConfiguration = new IpConfiguration(IpConfiguration.IpAssignment.DHCP, IpConfiguration.ProxySettings.NONE, null, null);
                        mEthManager.setConfiguration("eth0",ipConfiguration);
                        break;
 
                    case "WiFi":
                        LogUtils.e("DHCP");
                        IpConfiguration wifiIpConfiguration = new IpConfiguration(IpConfiguration.IpAssignment.DHCP, IpConfiguration.ProxySettings.NONE, null, null);
                        WifiConfiguration wifiConfiguration = getCurrentConfig();
                        wifiConfiguration.setIpConfiguration(wifiIpConfiguration);
                        wifiManager.setWifiApConfiguration(wifiConfiguration);
                        wifiManager.updateNetwork(wifiConfiguration);
                        new Thread(){// 这里比较耗时 所以开启新线程进行处理
                            @Override
                            public void run() {
                                super.run();
                                int netId = wifiManager.addNetwork(wifiConfiguration);
                                wifiManager.disableNetwork(netId);
                                boolean b = wifiManager.enableNetwork(netId, true);
                            }
                        }.start();
                        break;
                }
                break;
        }
    }
public static int maskStr2InetMask(String maskStr) {
        StringBuffer sb ;
        String str;
        int inetmask = 0;
        int count = 0;
      /*
       * check the subMask format
       */
        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 (pattern.matcher(maskStr).matches() == false) {
            Log.e("maskStr2InetMask","subMask is error");
            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;
    }
 
 
    public static Inet4Address getIPv4Address(String text) {
        try {
            return (Inet4Address) NetworkUtils.numericToInetAddress(text);
        } catch (IllegalArgumentException|ClassCastException e) {
            return null;
        }
    }

AndroidMenifest文件里添加一下 配置

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.NETWORK_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CONNECTIVITY_INTERNAL"/>
    <uses-permission android:name="android.permission.CONNECTIVITY_USE_RESTRICTED_NETWORKS"/>
    <uses-permission android:name="android.permission.OVERRIDE_WIFI_CONFIG"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

前提条件  导入 classes.jar  参考


目录
相关文章
|
4天前
|
Android开发
Android 盒子开发过程中遇到的问题及解决方法
Android 盒子开发过程中遇到的问题及解决方法
11 2
|
9月前
|
监控 网络协议 算法
Telnet、DHCP、静态路由、等价路由、环回接口、浮动静态路由详解
Telnet是位于OSI模型的第7层—应用层上的一种协议,是一个通过创建虚拟终端提供连接到远程主机终端仿真的TCP/IP协议。这一协议需要通过用户名和口令进行认证,是Internet远程登陆服务的标准协议。应用Telnet协议能够把本地用户所使用的计算机变成远程主机系统的一个终端。它提供了三种基本服务:
158 0
|
11月前
|
网络协议 网络虚拟化 网络架构
【网络】· 路由器中配置单臂路由和DHCP,VTP原理
【网络】· 路由器中配置单臂路由和DHCP,VTP原理
136 0
|
安全 网络安全 数据中心
华为USG防火墙配置DHCP及单臂路由小实验
华为USG防火墙配置DHCP及单臂路由小实验
757 0
华为USG防火墙配置DHCP及单臂路由小实验
|
网络协议 网络虚拟化 数据安全/隐私保护
华为—DHCP(中继)、路由引入,ACL综合实验
华为—DHCP(中继)、路由引入,ACL综合实验
272 1
华为—DHCP(中继)、路由引入,ACL综合实验
|
缓存 网络协议 网络虚拟化
路由基础:三层交换机、单臂路由的特点以及配置特点、DHCP报文类型、DHCP工作原理、在路由器上配置DHCP、在交换机上配置DHCP、配置DNS服务器
三层交换机、单臂路由的特点以及配置特点、DHCP报文类型、DHCP工作原理、在路由器上配置DHCP、在交换机上配置DHCP、配置DNS服务器
路由基础:三层交换机、单臂路由的特点以及配置特点、DHCP报文类型、DHCP工作原理、在路由器上配置DHCP、在交换机上配置DHCP、配置DNS服务器
|
ARouter 安全 API
XRouter 一个轻量级的Android路由框架,基于ARouter上进行改良,优化Fragment的使用,可结合XPage使用
XRouter 一个轻量级的Android路由框架,基于ARouter上进行改良,优化Fragment的使用,可结合XPage使用
764 0
XRouter 一个轻量级的Android路由框架,基于ARouter上进行改良,优化Fragment的使用,可结合XPage使用
|
Java Android开发 图形学
Android修行手册之Kotlin-【Kotlin的static是什么】
众所周知,人生是一个漫长的流程,不断克服困难,不断反思前进的过程。在这个过程中会产生很多对于人生的质疑和思考,于是我决定将自己的思考,经验和故事全部分享出来,以此寻找共鸣!!!
259 0
|
Android开发
【Android 逆向】Android 权限 ( ro.product.cpu.abi 属性 | ro.zygote 属性 | dhcp.eth0 属性 | net.* 属性 )
【Android 逆向】Android 权限 ( ro.product.cpu.abi 属性 | ro.zygote 属性 | dhcp.eth0 属性 | net.* 属性 )
367 0
【Android 逆向】Android 权限 ( ro.product.cpu.abi 属性 | ro.zygote 属性 | dhcp.eth0 属性 | net.* 属性 )
|
1天前
|
JSON Android开发 数据格式
Android框架-Google官方Gson解析,android开发实验报告总结
Android框架-Google官方Gson解析,android开发实验报告总结