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  参考


目录
相关文章
|
25天前
|
网络协议 网络安全 数据安全/隐私保护
计算机网络概念:网关,DHCP,IP寻址,ARP欺骗,路由,DDOS等
计算机网络概念:网关,DHCP,IP寻址,ARP欺骗,路由,DDOS等
44 4
|
1月前
|
网络协议 网络安全 数据安全/隐私保护
计算机网络概念:网关,DHCP,IP寻址,ARP欺骗,路由,DDOS等
【10月更文挑战第27天】计算机主机网关的作用类似于小区传达室的李大爷,负责将内部网络的请求转发到外部网络。当小区内的小不点想与外面的小明通话时,必须通过李大爷(网关)进行联系。网关不仅帮助内部设备与外部通信,还负责路由选择,确保数据包高效传输。此外,网关还参与路由表的维护和更新,确保网络路径的准确性。
48 2
|
2月前
|
网络协议 网络虚拟化 网络架构
【网络实验】/主机/路由器/交换机/网关/路由协议/RIP+OSPF/DHCP(上)
【网络实验】/主机/路由器/交换机/网关/路由协议/RIP+OSPF/DHCP(上)
75 1
|
2月前
|
网络协议 数据安全/隐私保护 网络虚拟化
【网络实验】/主机/路由器/交换机/网关/路由协议/RIP+OSPF/DHCP(下)
【网络实验】/主机/路由器/交换机/网关/路由协议/RIP+OSPF/DHCP(下)
64 0
|
4月前
|
Java Android开发
Invoke-customs are only supported starting with Android,Static interface methods are only supported
Invoke-customs are only supported starting with Android,Static interface methods are only supported
45 2
|
7月前
|
Android开发
Android 盒子开发过程中遇到的问题及解决方法
Android 盒子开发过程中遇到的问题及解决方法
46 2
|
监控 网络协议 算法
Telnet、DHCP、静态路由、等价路由、环回接口、浮动静态路由详解
Telnet是位于OSI模型的第7层—应用层上的一种协议,是一个通过创建虚拟终端提供连接到远程主机终端仿真的TCP/IP协议。这一协议需要通过用户名和口令进行认证,是Internet远程登陆服务的标准协议。应用Telnet协议能够把本地用户所使用的计算机变成远程主机系统的一个终端。它提供了三种基本服务:
242 0
|
网络协议 网络虚拟化 网络架构
【网络】· 路由器中配置单臂路由和DHCP,VTP原理
【网络】· 路由器中配置单臂路由和DHCP,VTP原理
201 0
|
2月前
|
安全 网络协议 网络安全
Cisco-DHCP配置
Cisco-DHCP配置
|
2月前
|
安全 小程序 网络安全
Cisco-DHCP中继配置
Cisco-DHCP中继配置