需求
实现通过4G网络访问外网,以太网访问内网。但是android系统默认的情况是,4G网络存在的情况下,插入以太网会导致4G网卡(wwan0)会被系统down掉。
修改
在android源码中找到EthernetNetworkFactory.java文件并做出以下修改:
- private static final int NETWORK_SCORE = 100;
+ private static final int NETWORK_SCORE = 30;
重新编译、打包、烧写。4G(wwan0)与以太网(eth0)已经可以同时存在。
这时设置eth0的静态IP以及添加相关路由:
ifconfig eth0 192.168.10.1 netmask 255.255.255.0
ip route add 192.168.10.0/24 dev eth0 proto static scope link table wwan0
ip route add 192.168.10.0/24 dev eth0 proto static scope link table eth0
这是ping 8.8.8.8以及ping192.168.10.2都可以ping通了。
注意
上面的配置重启后就会清掉,可以添加进启动脚本init.rc中,或者在app中需要的时候执行。