5

I'm creating an Android app that connects to another device via WiFi that sends and receives data through a socket connection. I want to be able to use my cellular data to do other things (such as browsing) while staying connected to this device.

On an iOS device, changing the network settings to Static and leaving the Router field blank seems to work. But on my Android device (Samsung Galaxy Note 5 running Android 7.0), it won't let me save the network settings if I leave it blank.

I have tried using 3rd party apps like Mobiwol, Super Download, and Speedify (Only Speedify seemed to work), but I want to be able to do this without the need of these apps.

I have also tried turning on "Keep Mobile data turned on" in the developer settings, and "Smart Network Switch" which just switches to my cellular data so my app does not work since it is not technically connected to the WiFi.

UPDATE: I managed to get cellular to work while connected through WiFi within my app (Thanks to Remy Lebeau and How to stay connected through mobile network after WIFI is connected on Android?). See code below.

Now I would like to be able to use cellular data also in background apps such as notifications or if I'd like to open up a browser etc. Is there a way to do this?

NetworkRequest.Builder req = new NetworkRequest.Builder();
req.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR); 
req.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET); 

ConnectivityManager.NetworkCallback networkCallback = new 
ConnectivityManager.NetworkCallback() {

     @Override
     public void onAvailable(Network network) {
            connectivityManager.bindProcessToNetwork(network)                   
     }
};

connectivityManager.requestNetwork(req.build(), networkCallback);
Nino Gonzales
  • 313
  • 2
  • 10

1 Answers1

0

Maybe this snippets works for you, it checks if there is wifi connection to do the job, or it will do it with mobile connection

ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobile = connManager .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

if (wifi.isConnected()){
    // If Wi-Fi connected
}

if (mobile.isConnected()) {
    // If Internet connected
}

Be sure to add this to your manifest:

"android.permission.ACCESS_NETWORK_STATE"

And thsi is a method that checks for wifi connection, you can add the mobile data as the else statment when return false is

private boolean checkWifiOnAndConnected() {
    WifiManager wifiMgr = (WifiManager) getSystemService(Context.WIFI_SERVICE);

    if (wifiMgr.isWifiEnabled()) { // Wi-Fi adapter is ON

        WifiInfo wifiInfo = wifiMgr.getConnectionInfo();

        if( wifiInfo.getNetworkId() == -1 ){
            return false; // Not connected to an access point
        }
        return true; // Connected to an access point
    }
    else {
        return false; // Wi-Fi adapter is OFF
    }
}
Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
  • I didn't click minus one :O – Nino Gonzales Jan 22 '18 at 21:14
  • 4
    If WiFi is connected, new connections will typically use WiFi instead of Cellular. If you want to force a connection to use Cellular while WiFi is active, use `ConnectivityManager` to get the cellular `Network`, then use either `Network.getSocketFactory().createSocket()`, `Network.bindSocket()`, or `Network.openConnection()` to create the connection. Do the same if you want to force a specific connection to use WiFi instead. So, you might use `ConnectivityManager.bindProcessToNetwork()` to make your app use Cellular by default, and then a WiFi `Network` to make the device connection. – Remy Lebeau Jan 23 '18 at 01:24
  • @RemyLebeau can you please provide proper source code for the same? – Sahil Shokeen Jul 10 '20 at 11:41