Can I have both Wi-Fi and cellular network interfaces open at the same time on Android?

痴心易碎 提交于 2019-12-03 04:53:27
nanoquack

This thread Android: Force data to be sent over radio vs WiFi mentions two possible approaches to the problem.

  1. Set the network preference whenever you want your app to use a specific connection:

    ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    cm.setNetworkPreference(ConnectivityManager.TYPE_MOBILE);
    
  2. Enable high priority mobile data connection:

    connectivityManager.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, "enableHIPRI");
    

For the second approach it is specifically stated it works with Android 2.2, no idea if this works in actual versions as well. However as far as I found out, enableHIPRI is more or less a hidden network setting, so I would prefer the first method if possible.

If you can't make it using API calls and if you are willing to get your hands dirty with the lower level, some linux knowledge may help. Basically what you have to do is to bring up both interfaces and have the default route set on the 3g interface. You will have to use system commands with root privileges for this kind of task. The reason for the close sockets is probably the interface that goes down and up again because of the API call.

Without root access the app cannot influence much.

With Android API what you can do at most is just turn on WiFi in hope that the device will switch to it and turn WiFi off to make the device switch to 3G (if it's there, the APN is correct etc.).

Anything else is not guaranteed to work. E.g. setting preferred connectivity type doesn't guarantee that the device will switch to that type.

The usual behavior is that as soon as WiFi becomes available, the device will have both 3G and WiFi on for a short while (3-5 secs) and then turn off 3G. As soon as WiFi is turned off by the user or your app, and the device attempts to connect to the Internet, it will turn on 3G after a short while.

Starting with Android 2.3 you can't event disable/enable 3G anymore. One used to spoil/restore APN settings to enable/disable 3G, but starting with 4.0 you can't change APN settings programmatically.

I think only one service is possible at a time. Either you can use WiFi or you can use use Cellular 3G Data. Both can't work simultaneously at a time.

The 3G and Wifi data networks can not connect at the same time, but 3G and Wifi can be connected simultaneously if the operator support and the android framework also change for the operator. Now some operators already have this feature but some can not.

Using both network at a time is not possible in Android App at present but you can do this in PC.
Connect one from your WIFI router and one from LAN or USB Internet Stick.
So create APp and use on PC. If you do not know how to run Android on PC than google it.

If I am not mistaken, if both Wi-Fi and 3G/4G is available, Wi-Fi will take the precedence.

Only when Wi-Fi is not available, it will switch to 3G/4G. Of course this provided both are switched on at the same time.

Instead of using a private wifi thus limiting your choice you may consider having a internet facing VPN, so that the device will connect to the VPN from 3G/4G and than subsequently access the data if security is your concern for using private wifi,.

And VPN clients are inbuilt in Android and there are also third party clients available.

Hope it helps!

You might want to look into ad-hoc wifi networks. It connects to devices over wifi even if the droid is already using wifi for internet (does not use the ip stack, though).

Dont confuse wifi (IEEE 802.11x) connections with internet (IP) connections. IP can run on wifi, mobile, ethernet, DSL, dailup or homing pigeons, but only one network at a time.

For more information on ad-hoc wifi networks, have a look at the following link.

https://code.google.com/p/android-wifi-tether/

Please note that many carriers forbid multicasting (sharing) their IP connections. Nice.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!