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