Android 9
1.修改 frameworks/base/core/java/android/net/NetworkFactory.java
evalRequest 方法 注释两行
private void evalRequest(NetworkRequestInfo n) {if (VDBG) log("evalRequest");if (n.requested == false && n.score mScore || n.request.networkCapabilities.satisfiedByNetworkCapabilities(mCapabilityFilter) == false || acceptRequest(n.request, n.score) == false)) {if (VDBG) log("releaseNetworkFor");Log.d(TAG, "releaseNetworkFor");//releaseNetworkFor(n.request);//注释//n.requested = false;//注释} else {if (VDBG) log("done");}}
2.修改frameworks/opt/net/ethernet/java/com/android/server/ethernet/EthernetNetworkFactory.java文件修改 NETWORK_SCORE=30降低优先级
这样以太网和wifi都能连接成功
Android 12
frameworks\opt\net\ethernet\java\com\android\server\ethernet\EthernetNetworkFactory.java
需要修改下面的得分
private static final SparseArray sTransports = new SparseArray();static {// LowpanInterfaceTracker.NETWORK_SCOREsTransports.put(NetworkCapabilities.TRANSPORT_LOWPAN,new TransportInfo(ConnectivityManager.TYPE_NONE, 30));// WifiAwareDataPathStateManager.NETWORK_FACTORY_SCORE_AVAILsTransports.put(NetworkCapabilities.TRANSPORT_WIFI_AWARE,new TransportInfo(ConnectivityManager.TYPE_NONE, 1));// EthernetNetworkFactory.NETWORK_SCOREsTransports.put(NetworkCapabilities.TRANSPORT_ETHERNET,new TransportInfo(ConnectivityManager.TYPE_ETHERNET, 70));// BluetoothTetheringNetworkFactory.NETWORK_SCOREsTransports.put(NetworkCapabilities.TRANSPORT_BLUETOOTH,new TransportInfo(ConnectivityManager.TYPE_BLUETOOTH, 69));// WifiNetworkFactory.SCORE_FILTER / NetworkAgent.WIFI_BASE_SCOREsTransports.put(NetworkCapabilities.TRANSPORT_WIFI,new TransportInfo(ConnectivityManager.TYPE_WIFI, 60));// TelephonyNetworkFactory.TELEPHONY_NETWORK_SCOREsTransports.put(NetworkCapabilities.TRANSPORT_CELLULAR,new TransportInfo(ConnectivityManager.TYPE_MOBILE, 50));}
frameworks\libs\net\common\device\android\net\NetworkFactoryLegacyImpl.java
原因是updateAgent变了
Android 9
private void updateAgent() {if (mNetworkAgent == null) return;if (DBG) {Log.i(TAG, "Updating mNetworkAgent with: " +mCapabilities + ", " +mNetworkInfo + ", " +mLinkProperties);}mNetworkAgent.sendNetworkCapabilities(mCapabilities);mNetworkAgent.sendNetworkInfo(mNetworkInfo);mNetworkAgent.sendLinkProperties(mLinkProperties);// never set the network score below 0.mNetworkAgent.sendNetworkScore(mLinkUp? NETWORK_SCORE : 0);}
Android12
private void updateAgent() {if (mNetworkAgent == null) return;if (DBG) {Log.i(TAG, "Updating mNetworkAgent with: " +mCapabilities + ", " +mLinkProperties);}mNetworkAgent.sendNetworkCapabilities(mCapabilities);mNetworkAgent.sendLinkProperties(mLinkProperties);// As a note, getNetworkScore() is fairly expensive to calculate. This is fine for now// since the agent isn't updated frequently. Consider caching the score in the future if// agent updating is required more oftenmNetworkAgent.sendNetworkScore(getNetworkScore());}
/** * Determines the network score based on the transport associated with the interface. * Ethernet interfaces could propagate a transport types forward. Since we can't * get more information about the statuses of the interfaces on the other end of the local * interface, we'll best-effort assign the score as the base score of the assigned transport * when the link is up. When the link is down, the score is set to zero. * * This function is called with the purpose of assigning and updating the network score of * the member NetworkAgent. */private int getNetworkScore() {// never set the network score below 0.if (!mLinkUp) {return 0;}int[] transportTypes = mCapabilities.getTransportTypes();if (transportTypes.length < 1) {Log.w(TAG, "Network interface '" + mLinkProperties.getInterfaceName() + "' has no "+ "transport type associated with it. Score set to zero");return 0;}TransportInfo transportInfo = sTransports.get(transportTypes[0], /* if dne */ null);if (transportInfo != null) {return transportInfo.mScore;}return 0;}
所谓共存就是让两个网络同时使用,如果想让哪个网络作为外网,则该网络优先级要高于内网的网络,即可达到外网用来上网的需求,那么要想共存,就必须到从低优先级网络切换到优先级高的网络时,不要做断开操作,即可达到,两个网络同时存在的需求,做到以上两点,我们便满足了2个网络同时存在,且使用优先级高的外网上网
到了android12
已经废弃了这种直接的打分机制,使用了policy
机制,具体可参考packages/modules/Connectivity/service/src/com/android/server/connectivity/NetworkRanker.java
但由于这种评分机制涉及的条件比较多,对于我们做网络共存这个需求来说更加复杂,
对此android开发者也给我么预留了以前的评分方案,只要我们简单的将USE_POLICY_RANKING = false;
即可
/** * Find the best network satisfying this request among the list of passed networks. */@Nullablepublic NetworkAgentInfo getBestNetwork(@NonNull final NetworkRequest request,@NonNull final Collection nais,@Nullable final NetworkAgentInfo currentSatisfier) {final ArrayList candidates = filter(nais, nai -> nai.satisfies(request));if (candidates.size() == 1) return candidates.get(0); // Only one potential satisfierif (candidates.size() <= 0) return null; // No network can satisfy this requestif (USE_POLICY_RANKING) {return getBestNetworkByPolicy(candidates, currentSatisfier);} else {return getBestNetworkByLegacyInt(candidates);}}