I'm working with the ConnectivityManager to toggle between networks for a specific task in my app. Basically, I need to connect to a specific network type (with specific network capabilities) while a particular fragment is up, and afterwards, switch back to the network that was used before (and that could be anything, WiFi, cellular, ...).
The problem is: I can't seem to find a way to build a request based on the information I get about the network that was previously connected. So far, I have done this to identify the kind of network that was used before:
connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
defaultNetwork = connectivityManager.getActiveNetwork();
defaultCapabilities = connectivityManager.getNetworkCapabilities(defaultNetwork);
defaultTransport = connectivityManager.getTransportInfo(defaultNetwork)
So, now I have the information, and if I understand correctly, if I wanted to reconnect to this network, I would need to put this information into a NetworkRequest defaultRequest
and then request the network via connectivityManager.requestNetwork(defaultRequest, defaultCallback)
where the defaultCallback
would bind to the network onAvailable via connectivityManager.bindProcessToNetwork(network)
.
How do I build this request based on the info I have? Do I just loop through the defaultCapabilities
and add every item to the Builder? Could I just bind the process to the network without the request instead?