4
votes

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);
1

1 Answers

0
votes

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
    }
}