实现功能: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