Let WiFi and 3G connection work together by hacking ConnectivityService.java

On Android phone, a system service (e.g, Connectivity Service ) is responsible to manage WiFi and 3G data connections. In particular, it tracks the state of the two interfaces, and performs the following policy to let only the prefered data connection (the system default is WiFi) is kept:

case1:  Given WiFI is connected, then turn on  3G data connection. 3G connection will be automatically torn down by Connectivity Service.

case2: Given 3G is connected, then turn on WiFi connection. Connectivity Service will tear down the existing 3G connection.

case3: Given WiFi is connected, then WiFi is disabled by user.  Connectivity Service will check if 3G  is enable. If it is true, it will reconnect 3G automatically.

The goal of COIN project is to use WiFi and 3G connections simultaneously. So it conflicts with the policy of Connectivity Service, but there is no configuration to edit the policy, and it is hard coded. You can find the clue in ConnectivityService.java:handleConnect function.

Our current solution is quite brutal:), which is to mask the eyes of Connectivity Service by modifying its message handler entry like the following:

// must be stateless – things change under us.
private class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
NetworkInfo info;
//added by COIN to disable Connectivity Service
int networkState = 8; //not any following state
/*use static google dns server for wifi and 3g*/
if (msg.what == NetworkStateTracker.EVENT_STATE_CHANGED) {
SystemProperties.set(“net.dns1″, “8.8.8.8″);
SystemProperties.set(“net.dns2″, “8.8.4.4″);
bumpDns();
}
//////////////////////////////////////////////
//switch (msg.what) {
switch (networkState) {
case NetworkStateTracker.EVENT_STATE_CHANGED:
info = (NetworkInfo) msg.obj;
int type = info.getType();

…..

And then compile the modified ConnectivityService.java in the android source code tree, you can get an new services.jar file in framework directory. Replace the existing services.jar on the cell phone with the following adb commands, then reboot the phone

adb shell “mount -o rw,remount -t yaffs2 /dev/block/mtdblock3 /system”
adb shell “chmod 0777 /system/framework”
adb push services.jar /system/framework
adb shell “chmod 0644 /system/framework/services.jar”
adb shell “chmod 0755 /system/framework”

So far, the solution seems work well and no applications are affected. But it is quite intuitive, and if you find it causes any trouble please tell us.

Mar03

34 Responses to “Let WiFi and 3G connection work together by hacking ConnectivityService.java”

  1. Hey, I’m working on a project which need the use of the 3g and wifi simultaneously but I don’t really understand why you use a static dns server, can you explain us a bit your script.
    Another question, when these modifications are applied, if you lauchned a terminal do you see both of the interface up ?

    In an application which need the use of both interface together, how can I specify in the code which connection is used because for the Android system if wifi is enabled then 3g connection is disabled in NetworkInfo.

    Regards,
    Pierrot

    • Gennerally the DNS server setting is done by ConnectivityService automatically, which means when wifi is on, the DNS server ip is set with dns server within WLAN network, otherwise, dns server within 3G network. And these DNS server IP addresses are all private IP address.
      Since we mask the ConnectivityService, we need to do this job, othwise, all the applicatons will suffer DNS lookup failure. Given the two interfaces are both on, we don’t know which one will be used to transmit DNS query message, so we should set a public DNS server ip address.
      Regarding the application using both interfaces, i don’t think you need do any modificatoin on your code, just create socket, bind , connect…, and the simutaneous transmit should be done by network stack tricks, if you are interested, pleae pay more attention our COIN project.

  2. Hello,

    I’m working on the same project as Pierrot and I used your COIN project in order to enable the 3G/Wifi interfaces at the same time. We tried it on a Galaxy Tab (We changed the ConnectivityService.java from services.jar as indicated above) and it changes nothing. I wanted to know if there are some special procedures to reboot the system?

    Thankfully,

    Marc

    • Hi Marc

      There are no special procedures to reboot. We only tested it on Android phones (Droid, Nexus one) with Android 2.2, so i am not sure it still works on Galaxy Tab. But you can add print info with Log.d(…) to debug your prolbem.
      BTW, are you sure have replaced the oringal services.jar successully? If your andriod is running, direct override with copy command does not work, you should mv the original services.jar first and then copy the new one to the system.

      • Hi,
        I have the same problem as Marc. I followed the instructions posted here on a Nexus S running 2.3.6 and nothing changed. One thing I noticed is that the original services.jar in the /system/framework dir of the Nexus S was only 313 Bytes and there is a 1.5M services.odex file. I am not sure how I can go about deodexing this file.

        Replacing the old services.jar with the new one which was 614Kb did nothing at all on Nexus S and same behavior as the original. Any clues on how to make this work ?

        Can you also pls post a direct link to your COIN project. I have tried searching but cant find any references to it.

        Tks.

    • Christos Says:
      March 1, 2013 at 3:19 pm

      Hello i am interest to do the same for my project! But where i can find the file ConnectivityService.java? i have a Samsung galaxy s2. And i don’t know where to find the files!

  3. Hello. I prevent you I’m e French student and my english leaves something to be desired. I’m work for my intership on the problem of mutlhoming, so I must manage 3G and wifi connection and the different problems.
    I find your solution cool, but how do you find this ? I explore the source code of android but I don’t understand this manipulation.

    If you can explain me why I’ll love it.

    Thank ^^

    • Hi Jordann,

      It is quite simple. Just tracking the output of the logcat command when i turn on/off the two interfaces, and searching the key words in Android source. :)

      • Ok. If I have understanded, you pu jsut the public DNS of Google and we make anything after because we haven’t no case with the value 8.

        This manipulation is brutal, but how you find it? Do you find an other solution to enable 3G and Wifi in the same time? Because I explore source code of Android, and I run ddms in the aim to understand the behaviour when I turn on or off the wifi toggle.
        For me I think we can juste put Android in Strong model host, and modify a little the code to route different packet and don’t disable APN when we change to the Wifi.

  4. Where I can read more about the COIN project?

  5. this is just like i wanted…
    but im very noob in this kind of things …
    so where i can find the ConnectivityServer.java or modifying the message handler?

    fyi, i use Samsung Galaxy Gio S5660, with Froyo 2.2.1

    • Hi, Fathul

      The file is in
      frameworks/base/services/java/com/android/server/ConnectivityService.java

  6. [...] [...]

  7. I tried to use this hack for my research project. I followed this link to download the android source. Then compiled it by make command but i could not find the services.jar. Please help me to compile command and location to get the updated jar file. And i am getting this error

    target Java: services (out/target/common/obj/JAVA_LIBRARIES/services_intermediates/classes)
    javac: target release 1.5 conflicts with default source release 1.7
    make: *** [out/target/common/obj/JAVA_LIBRARIES/services_intermediates/classes-full-debug.jar] Error 41
    Please help me regarding this

  8. Hi,
    I followed the instructions posted here on a Nexus S running 2.3.6 and looks like the changes have no effect. One thing I noticed is that the original services.jar in the /system/framework dir of the Nexus S was only 313 bytes and there is a 1.5M services.odex file. I am not sure how I can go about deodexing this file.

    Replacing the old services.jar with the newly compiled one which was 614Kb did nothing at all on Nexus S and it exhibits the same behavior as the original. Any clues on how to make this work for newer Android versions ?

    Can you also pls post a direct link to your COIN project. I have tried searching for it but cant find any references.

    Tks.

  9. Okay

  10. This is a great article. It’s not only well-written, it’s interesting and you have expressed your views beautifully. You have struck gold with this content.

  11. hey leo,

    interesting solution for a problem i have now encountered myself.

    i’ve followed instructions on a galaxy s running cyanogenmod 2.3 andro.

    alas that has resulted in 3g communication being altogether disabled!

    that is: turning off wifi (with ‘data enabled’ set to true of course) will
    disable the device from performing any type of networking.

    since i can move my project to other andro versions i would
    appreciate it if you could direct me to an android version (preferably
    cyangenmod or some other custom rom release) and device (whichever)
    for which this manipulation had worked fine for you.

    tnx

  12. this is great.This was really helpful in my project.
    However I wish to know if there is anyway to modify this code so that only selected applications can access the internet through 3G while other applications that are not selected will not be able to use 3G data connection.
    This is the last part of my project and I have been looking for a solution and I have not found any. only this was the closest thing to a solution.
    I hope to get your opinion on my problem.

  13. did you get a chance to look at Android ICS to check whether this is easier to achieve?

  14. are you sure the packets are going thru both interfaces?
    I think it doesn’t work, simply because you would need two default gateways, leading to some hard problems:

    - how does the system choose where to send the packets?
    - for outgoing packets: unless the two connections have both statically assigned public IP addresses, which is very unlikely, you will end up with two differently NATed paths, and the client will refuse packets coming from two different ip addresses on the same connection.
    - for incoming packets: to let the client send packets to both interfaces, you would need to send them from both interfaces with different source ip addresses: it will not work, the client will get confused. and anyway you would need support at the application level.

    to solve all this, you’d need to:

    - make an ad-hoc application which understands all this and can send chuncks to both interfaces, then merge all the returning chunks. you’d need support at the application level: for example you’d need http byte range support on both client and server
    - divide “equally” the single specific connections thru the two gateways. this may work but it’s pretty hard if you do not have access to advanced routing and traffic shaping at the kernel level. may be possible on a phone with custom compiled aosp rom and modified kernel

  15. I need some help regarding this hack. Is it possible to turn on 3G and Wifi using the method Leo has mentioned without rooting the device? I have seen some applications turning on tethering without rooting… but I am not sure how it’s done.

  16. These pieces are all Swiss made and offer Quartz movements.
    Designer watches are in fact some of those costly watches that are made uniquely.
    Franck Muller himself prides with the fact that he essentially models
    the system of every sole timepiece himself.

  17. How i can do all this? can you send me the steps! where i can found that file. and how i can do the adb!
    thanks for your help

  18. [...] [...]

  19. Can you please provide the source files? I did find the services.jar file but I’m using eclipse to compile the code so please give the link to the whole COIN project!!

  20. It’s really working the coin project? i Changed the file and still the same default issues? I have a Samsung galaxy S2 mobile phone.

  21. Quality articles or reviews is the key to be a focus for the visitors to pay a visit
    the web page, that’s what this web page is providing.

  22. Just desire to say your article is as amazing. The clearness
    in your post is simply nice and i can assume
    you are an expert on this subject. Fine with your permission allow me to grab your RSS feed to keep updated with forthcoming post.
    Thanks a million and please keep up the enjoyable work.

  23. Every weekend i used to pay a visit this website, because i wish for enjoyment,
    as this this site conations genuinely fastidious funny information too.

  24. Hi mates, how is the whole thing, and what you would like to
    say about this article, in my view its really awesome designed for me.

  25. I like the helpful information you provide in your articles.

    I will bookmark your blog and check again here regularly.

    I am quite sure I will learn many new stuff right here! Best of luck
    for the next!

Leave a Reply