7.1 4G路由功能实现

实现功能:4G网络通过RJ45共享给其他设备

framework层实现:

RK7.1的代码中已经集成了双以太网功能,通过:

设置属性: persist.net.ethernet.mode=multi   开启双以太网

 

JAVA层实现:

public String doEthernetShareS() {
        
        String result = "Failure";  
        DataOutputStream dataOutputStream = null;  
        BufferedReader errorStream = null;  
        //String networkType = getNetworkType();
        try {  
            //Log.d(TAG,"doEthernetShareS.networkType=" + networkType);
            Process process = Runtime.getRuntime().exec("su"); 
            dataOutputStream = new DataOutputStream(process.getOutputStream());  
            String commandStr = 
                "ndc tether stop " + " \n" +
                //"sleep 1" + " \n" +
                "ip rule add from all lookup main pref 9999 " + " \n" +
                //"ifconfig eth0 down " + " \n" +
                //"sleep 1" + " \n" +
                "ifconfig eth0 up" + " \n" +
                //"sleep 1" + " \n" +
                "busybox ifconfig eth0 192.168.43.1 " + " \n" +                        
                "ndc netd 5003 tether start 192.168.43.2 192.168.43.254" + " \n"  +
                "ndc netd 7 nat enable eth0 ppp0 2 10.6.194.114/24" + " \n"  +    
                "echo 1 >/proc/sys/net/ipv4/ip_forward" + " \n"  +                    
                //"iptables -t nat -I PREROUTING -i eth0 -p udp --dport 53 -j DNAT --to-destination 8.8.8.8" + " \n" +
                //"ndc tether stop " + " \n" +
                //"busybox ifconfig eth0 192.168.43.1 " + " \n" +    
                //"ndc netd 5003 tether start 192.168.43.2 192.168.43.254" + " \n"  +
                //"ndc netd 7 nat enable eth0 ppp0 2 10.6.194.114/24" + " \n"  +    
                //"echo 1 >/proc/sys/net/ipv4/ip_forward" + " \n"  +        
                "iptables -t nat -I PREROUTING -i eth0 -p udp --dport 53 -j DNAT --to-destination 8.8.8.8" + " \n"; 
                                
            dataOutputStream.write(commandStr.getBytes(Charset.forName("utf-8")));  
            dataOutputStream.flush();  
            dataOutputStream.writeBytes("exit\n");  
            dataOutputStream.flush();  
            process.waitFor();  
            errorStream = new BufferedReader(new InputStreamReader(process.getErrorStream()));  
            String msg = "";  
            String line;  
            // 读取命令的执行结果  
            while ((line = errorStream.readLine()) != null) {  
                msg += line;  
            }  
            Log.d(TAG,"msg" +msg);
            result = msg;
        } catch (Exception e) {  
            Log.e(TAG, e.getMessage(), e);  
        } finally {  
            try {  
                if (dataOutputStream != null) {  
                    dataOutputStream.close();  
                }  
                if (errorStream != null) {  
                    errorStream.close();  
                }  
            } catch (IOException e) {  
                Log.e(TAG, e.getMessage(), e);  
            }  
        }  

        return result;  
        
    }


    

参考:

https://blog.csdn.net/layuetian2011/article/details/108444577

拓展:错误:400 5003 Tether operation failed

https://blog.csdn.net/u010559573/article/details/110132226

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值