Android Ethernet and Wi-Fi at the same time
Asked Answered
K

4

9

I have a small TV box device running Android. Whenever I bring up the Wi-Fi interface, the Ethernet interface is disconnected. If I then bring up the Ethernet device, the Wi-Fi interface. I have tried various methods including using the command line to manually bring up the interfaces and the same issue occurs. I want to be able to connect to the Wi-Fi and have it as my default gateway but then also be connected to the Ethernet port to route certain traffic over that interface. Obviously this is possible to do in Linux so there must be a way... does anybody know what it is that is tearing down the interfaces when the other is initiated...

Kamakura answered 9/8, 2013 at 9:27 Comment(0)
K
8

This is a restriction in Android. It purposely only allows one connection to be up at a time and has a handler in the 'ConnectivityServices.java' file that tears down a 'non-preferred' network when a network with a priority is enabled. This is also what brings wifi up and tears down cellular data connections when in range of a recognised hotspot...

Just in case anybody ever needs this, here is what I did :

Download the AOSP source code for the version of Android on the device.

Edit the 'ConnectivityServices.java' file accordingly. I basically just commented out the command in the failover command in the connection change handler. I don't have the source code in front of me so message me if you need to know what I did here...

Anyway, then build the AOSP source code on your machine.

Once completed, grab the 'services.jar' file in the /dexclasses/ directory that has been created. Extract it using WinRAR then copy the 'classes.dex' file out of the .jar file to a separate directory. Use this to extract the classes.dex : https://code.google.com/p/smali/ Grab the 'ConnectivityServices.smali' file and keep it safe.

From the device Go onto the device you wish to enable both network interfaces on and copy the /system/framework/services.jar file to a PC. Extract it using WinRAR then copy the 'classes.dex' file out of the .jar file to a separate directory. Use the Java Smali command to extract the classes.dex.

Take the ConnectivityServices.smali file from the AOSP and put it in the directly you have just extracted using the classes.dex on the device. Might be a good idea to make a backup of the original ConnectivityServices.smali file before overwriting it.

Then simple repackage the classes.dex file using the baksmali command. Copy the classes.dex file into the original services.jar file using winRAR. Again, take a backup of the original but then overwrite it in the .jar file.

Then simply put the new services.jar file back on the device in the /system/framework/ directory.

Then reboot - it will take longer than normal to boot up on the first time.

Kamakura answered 20/11, 2013 at 20:15 Comment(1)
thanks for your comment! I tied the following (for Android 4.4.2), but without success: I edited my 'frameworks/base/services/java/com/android/server/ConnectivityService.java' file like this: I commented out in the function 'private void handleConnect' both calls 'teardown(thisNet);'. Then I compiled android, BAKSMALIed the compiled and original services.jar, replaced all original 'ConnectivityService*.smali' with the the compiled ones. SMALIed again and replace the services.jar on the android devices. Then I rebooted and still WIFI is disabled when I connect Ethernet. Any help appreciated!Hamann
M
4

There is a simpler way, which doesn't require you to build AOSP matching your device. You can simply modify smali-decompiled code and recompile it. Use https://github.com/android/platform_frameworks_base/blob/master/services/java/com/android/server/ConnectivityService.java for comparizon. Something along this lines:

adb pull /system/framework/services.jar
cp services.jar services.jar.bak
unzip services.jar classes.dex
java -jar baksmali.jar classes.dex

Edit out/com/android/server/ConnectivityService.smali in handleConnect(), so that this will be the result:

// if this is a default net and other default is running
// kill the one not preferred
if (false && mNetConfigs[newNetType].isDefault()) {
  if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != newNetType) { ...

I made the following change:

aget-object v5, v5, v1

invoke-virtual {v5}, Landroid/net/NetworkConfig;->isDefault()Z

move-result v5

#if-eqz v5, :cond_a6 # changed to unconditional jump
goto :cond_a6

Recompile, repack, push. Then reboot and test.

java -jar smali.jar -o classes.dex out
zip services.jar classes.dex
adb push services.jar /system/framework/services.jar
Martian answered 25/10, 2014 at 9:19 Comment(5)
thanks a lot! totally worked for me. Just look for the line: invoke-virtual {v5}, Landroid/net/NetworkConfig;->isDefault()Z and make the changesFears
any idea how to do this for Android 5.1?Fears
is it possibly here? github.com/CyanogenMod/android_frameworks_base/blob/…Fears
for Android 6 it seems that services.jar is empty :( any ideas where to find the fix in Android 6?Fears
Nevermind, it was just a non-deodexed ROM. installing CM13 fixed it for me.Fears
F
1

So I also found out how to do it on CM12.1

Just change this line over here https://github.com/CyanogenMod/android_frameworks_base/blob/e49d5ea0858a765c22d8aa96cc660d4708a413fb/services/core/java/com/android/server/ConnectivityService.java#L4264

or in smali replace the line before .line 4266 with goto :cond_1b1

Fears answered 26/11, 2015 at 11:59 Comment(0)
D
0

I also had this issue local Ethernet stops working when WiFi is on and connected.

Ping stopped working, UDP communication (sending) stopped working.
Nowadays (at least with Android 14) you can force a UDP/TCP socket to be on a specific network interface:

SO_BINDTODEVICE works great if you're using C++ or Python code, doesn't require root.

When pinging, there's a -I (Interface) command line option with the ping command bypasses this issue as well.

Try this on Termux (app for Android). Make sure to get Termux from the official release page https://github.com/termux/termux-app/releases/tag/v0.118.1 because the Google Play Store version is very outdated.

ping -i eth0 192.168.2.103
Demob answered 24/7, 2024 at 21:53 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.