I'm working on an application that will run on a phone where the phone will be a station on a private Wi-Fi network. The phone will be a station, not an access point, and the private Wi-Fi network does not route to the Internet. My application needs to communicate with servers on the Internet as well as devices on the local Wi-Fi network, so it needs to have connections on both networks at the same time. I've been trying to figure out how to do this.
I've been trying the technique described in the discussion on the
Google Android developers group titled "Can
Android 2.X connect to 3G and Wifi data networks simultaneously?", but
it is not working well. What I find is that, when I enable the
cellular network by calling
ConnectivityManager.setNetworkPreference(ConnectivityManager.TYPE_MOBILE)
,
any sockets I have open on the Wi-Fi network are closed. I haven't
tried it, but I suspect the same thing will happen to sockets on the
cellular network when I switch back to Wi-Fi.
Another problem is that, these calls operate on a global level, changing the network settings for the entire phone, not just the application. Switching the network set up globally like this will interfere with any other app that happens to be running on the phone. Even after my application exits, the phone continues to run with the last network configuration it set.
I'm looking for a way to have connections open on both the cellular data and Wi-Fi networks at the same time, and without interfering with other applications running on the phone.
1.Does anyone know how to do
this?
2.Does anyone know if this is possible?
Thanks for your help.
This thread Android: Force data to be sent over radio vs WiFi mentions two possible approaches to the problem.
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);
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.