Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?
like image 518
digiGuy Avatar asked Jan 03 '12 20:01

digiGuy


2 Answers

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.

like image 115
nanoquack Avatar answered Sep 23 '22 18:09

nanoquack


This is actually pretty simple. You need to file two requests; one for a network with the cellular transport type and an additional request with the wifi transport type. Then, with whatever networks are returned from those requests, you can use them as needed (e.g. only do work on the internal resources with the network returned from the wifi request).

Here is an example to keep both Wi-Fi and Cellular up at the same time:

final ConnectivityManager connectivityManager = (ConnectivityManager)
  context.getSystemService(Context.CONNECTIVITY_SERVICE);

final NetworkRequest requestForWifi =
  new NetworkRequest.Builder()
  .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
  .build();

final NetworkRequest requestForCellular =
  new NetworkRequest.Builder()
  .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
  .build();

final NetworkCallback cbWifi = new NetworkCallback() {
  @Override
  void onAvailable(Network network) {
      // Triggers when this network is available so you can bind to it.
      
      // Examples of how to bind to it include 
      // (uncomment the best option for your use-case):
      
      // If you want to set connections for the entire app
      // connectivityManager.bindProcessToNetwork(network);

      // If you want to open a specific connection:
      // By socket:
      // try (Socket socket = new Socket()) {
      //     Network network = getNetwork();
      //     network.bindSocket(socket);
      // }
      // Or by URL:
      // URLConnection conn = network.openConnection(URL url);
  }
};

final NetworkCallback cbCellular = new NetworkCallback() {
  @Override
  void onAvailable(Network network) {
      // Triggers when this network is available so you can bind to it.
  }
};

connectivityManager.requestNetwork(requestForWifi, cbWifi);
connectivityManager.requestNetwork(requestForCellular, cbCellular);

As long as there are requests for a particular network type (i.e. Wi-Fi or Cellular), ConnectivityService (service that ConnectivityManager relies on) will keep those networks up if available. Therefore using the above pattern but tweaking the NetworkRequest object to meet your needs, you can keep any number of networks up.

like image 37
Always Learning Avatar answered Sep 22 '22 18:09

Always Learning