I'm trying to send data over a specific network interface (Wi-Fi or cellular) on Android but all the methods I've tried just return the primary network interface. In general this means Android returns Wi-Fi when it's available or cellular when it's not.
Is there a way to get either a Network or NetworkInterface to connect to for the non-primary network? I'd prefer a Network because I can use its socket factory with OkHttp but if it's not possible I'm fine with binding a Socket to a specific interface and doing the HTTP work myself.
ConnectivityManager.registerNetworkCallback
I registered a network callback with the following request:
val networkRequest = NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.build()
I also tried registering separate requests for a specific network types:
val wifiNetworkRequest = NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.build()
val cellularNetworkRequest = NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
.build()
All of these appear to work the same as registerDefaultNetworkCallback. The newer registerBestMatchingNetworkCallback and the deprecated getAllNetworks also return just the active network.
NetworkInterface.getNetworkInterfaces
Not much to say here except that it doesn't work and behaves the exact same as ConnectivityManager
getifaddrs
My final attempt because I figured if anything would behave differently it would be the low level Linux based C-API but sadly it behaves the same as all the other methods.


NWConnectioncan be tied to a specificNWInterfaceType. I have a working iOS app where I can show the user information about each of their network interfaces (current IP, ASN, rough location, etc). I would like to do the same for the Android app.