Skip to content

Instantly share code, notes, and snippets.

@dmpatel151282
Created February 7, 2017 12:27
Show Gist options
  • Save dmpatel151282/8a22100a917938578ba36b51bf85d4b8 to your computer and use it in GitHub Desktop.
Save dmpatel151282/8a22100a917938578ba36b51bf85d4b8 to your computer and use it in GitHub Desktop.
Open the following file from Android framework.
- /android/frameworks/base/services/java/com/android/server/ConnectivityService.java
- Add below code at appropriate place.
// the set of network types that can only be enabled by system/sig apps
List mProtectedNetworks;
+// Wifi/Eth coexist strategy V0.3
+private NetworkInfo mSecondaryNetworkInfo = null;
+private int mActiveSecondaryNetwork = -1;
+// End
- Go to "handleDisconnect" function. Add below code at appropriate place.
int prevNetType = info.getType();
+// Wifi/Eth coexist strategy V0.3
+if (prevNetType == ConnectivityManager.TYPE_WIFI) {
+ log("Wifi Disconnected!");
+ mSecondaryNetworkInfo = null;
+ mActiveSecondaryNetwork = -1;
+}
+// End
...
if (mActiveDefaultNetwork != -1) {
sendConnectedBroadcastDelayed(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo(),
getConnectivityChangeDelay());
}
+// Wifi/Eth coexist strategy V0.3
+if (prevNetType == ConnectivityManager.TYPE_ETHERNET && mActiveSecondaryNetwork != -1) {
+ log("Ethernet disconnect, refresh netowrk info to wifi.");
+ handleConnect(mSecondaryNetworkInfo);
+ log("Add wlan0 default route info");
+ LinkProperties wifiLp = mNetTrackers[mActiveDefaultNetwork].getLinkProperties();
+ String ifname = wifiLp.getInterfaceName();
+ for(RouteInfo r : wifiLp.getRoutes()) {
+ try {
+ if (r.isDefaultRoute()) {
+ log(ifname + "add default route: " + r);
+ mNetd.addRoute(ifname, r);
+ }
+ } catch (Exception e) {
+ // never crash - catch them all
+ if (DBG) loge("Exception trying to remove a route: " + e);
+ //return false;
+ }
+ }
+}
+// End
- Go to "handleConnectionFailure" function. Add below code at appropriate place.
mNetTrackers[info.getType()].setTeardownRequested(false);
+// Wifi/Eth coexist strategy V0.3
+if (info.getType() == ConnectivityManager.TYPE_WIFI) {
+ log("Wifi Disconnected!");
+ mSecondaryNetworkInfo = null;
+ mActiveSecondaryNetwork = -1;
+}
+// End
...
if (mActiveDefaultNetwork != -1) {
sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo());
}
+// Wifi/Eth coexixt strategy V0.3
+if (info.getType() == ConnectivityManager.TYPE_ETHERNET && mActiveSecondaryNetwork != -1) {
+ log("Ethernet disconnect, refresh netowrk info to wifi.[handleConnectionFailure]");
+ handleConnect(mSecondaryNetworkInfo);
+ log("Add wlan0 default route info[handleConnectionFailure]");
+ LinkProperties wifiLp = mNetTrackers[mActiveDefaultNetwork].getLinkProperties();
+ String ifname = wifiLp.getInterfaceName();
+ for(RouteInfo r : wifiLp.getRoutes()) {
+ try {
+ if (r.isDefaultRoute()) {
+ log(ifname + "add default route: " + r);
+ mNetd.addRoute(ifname, r);
+ }
+ } catch (Exception e) {
+ // never crash - catch them all
+ if (DBG) loge("Exception trying to remove a route: " + e);
+ //return false;
+ }
+ }
+}
+// End
- Go to "handleConnect" function. Add below code at appropriate place.
final int newNetType = info.getType();
+// Wifi/Eth coexist strategy V0.3
+log("Connected type: " + newNetType);
+log("Current preferred type: " + mNetworkPreference);
+// End
...
// if this is a default net and other default is running
// kill the one not preferred
if (mNetConfigs[newNetType].isDefault()) {
if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != newNetType) {
if (isNewNetTypePreferredOverCurrentNetType(newNetType)) {
+// Wifi/Eth coexist strategy V0.3
+if (newNetType == ConnectivityManager.TYPE_ETHERNET &&
+ mActiveDefaultNetwork == ConnectivityManager.TYPE_WIFI) {
+ log("Wifi already connected, but Eth connected after. Eth&Wifi coexit");
+ mSecondaryNetworkInfo = mNetTrackers[ConnectivityManager.TYPE_WIFI].getNetworkInfo();;
+ mActiveSecondaryNetwork = mActiveDefaultNetwork;
+ log("Delete wlan0 default route info");
+ LinkProperties wifiLp = mNetTrackers[mActiveDefaultNetwork].getLinkProperties();
+ String ifname = wifiLp.getInterfaceName();
+ for(RouteInfo r : wifiLp.getRoutes()) {
+ try {
+ if (r.isDefaultRoute()) {
+ log(ifname + "remove default route: " + r);
+ mNetd.removeRoute(ifname, r);
+ }
+ } catch (Exception e) {
+ // never crash - catch them all
+ if (DBG) loge("Exception trying to remove a route: " + e);
+ //return false;
+ }
+ }
+} else {
+ // tear down the other
+ NetworkStateTracker otherNet = mNetTrackers[mActiveDefaultNetwork];
+ if (DBG) {
+ log("Policy requires " + otherNet.getNetworkInfo().getTypeName() +
+ " teardown");
+ }
+ if (!teardown(otherNet)) {
+ loge("Network declined teardown request");
+ teardown(thisNet);
+ return;
+ }
+}
+// End
} else {
+// Wifi/Eth coexist strategy V0.3
+if (newNetType == ConnectivityManager.TYPE_WIFI &&
+ mActiveDefaultNetwork == ConnectivityManager.TYPE_ETHERNET) {
+ mSecondaryNetworkInfo = info;
+ mActiveSecondaryNetwork = newNetType;
+ log("Eth already connected, but wifi connected after. Eth&Wifi coexist.");
+ log("Eth default active \t Wifi seconday active");
+ return;
+} else {
+ // don't accept this one
+ if (VDBG) {
+ log("Not broadcasting CONNECT_ACTION " +
+ "to torn down network " + info.getTypeName());
+ }
+ teardown(thisNet);
+ return;
+}
+// End
}
}
synchronized (ConnectivityService.this) {
// have a new default network, release the transition wakelock in a second
// if it's held. The second pause is to allow apps to reconnect over the
// new network
if (mNetTransitionWakeLock.isHeld()) {
mHandler.sendMessageDelayed(mHandler.obtainMessage(
EVENT_CLEAR_NET_TRANSITION_WAKELOCK,
mNetTransitionWakeLockSerialNumber, 0),
1000);
}
}
mActiveDefaultNetwork = newNetType;
// this will cause us to come up initially as unconnected and switching
// to connected after our normal pause unless somebody reports us as reall
// disconnected
mDefaultInetConditionPublished = 0;
mDefaultConnectionSequence++;
mInetConditionChangeInFlight = false;
// Don't do this - if we never sign in stay, grey
//reportNetworkCondition(mActiveDefaultNetwork, 100);
}
- Go to "handleCaptivePortalTrackerCheck" function. Add below code at appropriate place.
if (DBG) log("Tear down low priority net " + info.getTypeName());
+// Wifi/Eth coexist strategy V0.3
+//teardown(thisNet);
+if (mActiveDefaultNetwork == ConnectivityManager.TYPE_ETHERNET)
+ mCaptivePortalTracker.detectCaptivePortal(new NetworkInfo(info));
+// End
return;
@TobiasVorsmann
Copy link

Hello Dharmesh Patel, I am facing the same problem. I would like to have two connections, one wifi and one ethernet.

You had a great fix here. The problem is, that I have to use Android 9 and there were some changes in the code. Could you please help me to map your fix to Android 9 code?

Thanks for your awesome fix anyway :)

Best regards, Tobias.

@dmpatel151282
Copy link
Author

Hello Dharmesh Patel, I am facing the same problem. I would like to have two connections, one wifi and one ethernet.

You had a great fix here. The problem is, that I have to use Android 9 and there were some changes in the code. Could you please help me to map your fix to Android 9 code?

Thanks for your awesome fix anyway :)

Best regards, Tobias.

Hello @tobias,

I will check for Android 9.0. I will update you in few days.

@dmpatel151282
Copy link
Author

Hello Dharmesh Patel, I am facing the same problem. I would like to have two connections, one wifi and one ethernet.
You had a great fix here. The problem is, that I have to use Android 9 and there were some changes in the code. Could you please help me to map your fix to Android 9 code?
Thanks for your awesome fix anyway :)
Best regards, Tobias.

Hello @tobias,

I will check for Android 9.0. I will update you in few days.

Hello @tovias,

Please refer below link and try it.
https://www.fatalerrors.org/a/android-8.1-and-android-5.1-are-connected-via-ethernet.html

@TobiasVorsmann
Copy link

Hello @dmpatel151282,

thanks for your fast reply. I will try solving the problem using the link you offered and give an update :)

@TobiasVorsmann
Copy link

TobiasVorsmann commented Jun 15, 2021

Hello @dmpatel151282

I made the changes according to the guide, but it doesnt seem to fix my problem. The change of network score seems to work, because if I am connected to ethernet and turn on wifi, ethernet turns off and wifi gets connected. The problem is, that ethernet gets disconnected if I turn on wifi and only gets connected if I turn off wifi. So I am connected to ethernet, wifi off -> turn on wifi, disconnected from ethernet and connected to wifi -> turn wifi off, disconnected from wifi and connected to ethernet.

Thanks for your help
Best regards, Tobias

@dmpatel151282
Copy link
Author

Hello @dmpatel151282

I made the changes according to the guide, but it doesnt seem to fix my problem. The change of network score seems to work, because if I am connected to ethernet and turn on wifi, ethernet turns off and wifi gets connected. The problem is, that ethernet gets disconnected if I turn on wifi and only gets connected if I turn off wifi. So I am connected to ethernet, wifi off -> turn on wifi, disconnected from ethernet and connected to wifi -> turn wifi off, disconnected from wifi and connected to ethernet.

Thanks for your help
Best regards, Tobias

Hello @TobianVorsmann

I have checked in Android Q based device and Ethernet and WiFi are running at same time.

I will look into it for changes in Android 9 and Android Q. Can you please check parallelly to get solution???

@TobiasVorsmann
Copy link

Hello @dmpatel151282

Thanks for your help. I am trying to find a solution parallelly for sure :)

@dmpatel151282
Copy link
Author

dmpatel151282 commented Aug 30, 2021

Hello @TobianVorsmann

Have you got any solution of it???

If not then add below patch line in frameworks/base/services/core/java/com/android/server/ConnectivityService.java file.

+ final NetworkRequest mEthernetRequest = createDefaultInternetRequestForTransport(
+                                                 NetworkCapabilities.TRANSPORT_ETHERNET,
+                                                 NetworkRequest.Type.REQUEST);
+           NetworkRequestInfo ethernetNRI = new NetworkRequestInfo(null, mEthernetRequest, new Binder());
+           mNetworkRequests.put(mEthernetRequest, ethernetNRI);
+           mNetworkRequestInfoLogs.log("REGISTER " + ethernetNRI); 
mDefaultMobileDataRequest = createDefaultInternetRequestForTransport(
  | NetworkCapabilities.TRANSPORT_CELLULAR, NetworkRequest.Type.BACKGROUND_REQUEST);

I have tried in Android Q and it's working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment