之前一直想着把后续写下的,事情太多给搞忘了,这几天有几个人加我 QQ 问我有没解决,我才想起来
以下是我这边经过修改可以达到预期效果的方法,仅供参考! 思路是:先保证系统多个网卡能同时获取到 IP ,然后设置路由表分配什么流量走什么网卡
=============================================
当以太网连接后 Wi-Fi 仍可连
frameworks/opt/net/wifi/service/java/com/android/server/wifi/WifiStateMachine.java
private void setScanResults() {
...
if (getCurrentState() == mRoamingState
|| getCurrentState() == mObtainingIpState
|| getCurrentState() == mScanModeState
|| getCurrentState() == mDisconnectingState
|| (getCurrentState() == mConnectedState
&& !mWifiConfigStore.enableAutoJoinWhenAssociated)
|| linkDebouncing
|| state == SupplicantState.ASSOCIATING
|| state == SupplicantState.AUTHENTICATING
|| state == SupplicantState.FOUR_WAY_HANDSHAKE
|| state == SupplicantState.GROUP_HANDSHAKE
|| (/* keep autojoin enabled if user has manually selected a wifi network,
so as to make sure we reliably remain connected to this network */
mConnectionRequests == 0 && selection == null)) {
// Dont attempt auto-joining again while we are already attempting to join
// and/or obtaining Ip address
attemptAutoJoin = false;
}
//add by a_bin for blzn network
if(SystemProperties.getBoolean("ro.zwqx.blzn_network",false)){
attemptAutoJoin = true;
}
...
}
连接高优先级的网络,低优先级的不断开
frameworks/base/services/core/java/com/android/server/ConnectivityService.java
private void teardownUnneededNetwork(NetworkAgentInfo nai) {
//add by a_bin for blzn network
if(SystemProperties.getBoolean("ro.zwqx.blzn_network",false)){
return;
}
}
最后就是路由表的设置了,这里需要 root 权限
frameworks/base/services/core/java/com/android/server/ConnectivityService.java
private void updateLinkProperties(NetworkAgentInfo networkAgent, LinkProperties oldLp){
...
if(networkAgent.linkProperties.getInterfaceName().equals("wlan0")){
java.lang.Process pro = Runtime.getRuntime().exec("su");
DataOutputStream outputStream = new DataOutputStream(pro.getOutputStream());
outputStream.writeBytes("ip route flush table 3\n");
outputStream.writeBytes("ip route add dev wlan0 table 3\n");
outputStream.writeBytes("ip rule add to 10.1.0.0/16 table 3\n");
outputStream.writeBytes("ip route flush cache\n");
outputStream.writeBytes("exit\n");
outputStream.flush();
outputStream.close();
pro.waitFor();
}else if(networkAgent.linkProperties.getInterfaceName().equals("eth0")){
java.lang.Process pro = Runtime.getRuntime().exec("su");
DataOutputStream outputStream = new DataOutputStream(pro.getOutputStream());
outputStream.writeBytes("ip route flush table 1\n");
outputStream.writeBytes("ip route add dev eth0 table 1\n");
outputStream.writeBytes("ip rule add to 192.168.0.0/16 table 1\n");
outputStream.writeBytes("ip route flush cache\n");
outputStream.writeBytes("exit\n");
outputStream.flush();
outputStream.close();
pro.waitFor(); ok::updateLinkProperties()");
}
...
}
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.