Workaround mapping between NetworkInfo and NetworkInterface Workaround mapping between NetworkInfo and NetworkInterface android android

Workaround mapping between NetworkInfo and NetworkInterface


Since API 21, you can use LinkProperties to get a NetworkInterface object out of a Network network object:

ConnectivityManager manager = getSystemService(ConnectivityManager.class);LinkProperties prop = manager.getLinkProperties(network);    NetworkInterface iface = NetworkInterface.getByName(prop.getInterfaceName());

However, although getAllNetworks() is supposed to replace getAllNetworkInfo() (the former, introduced in API 21, has deprecated the latter in API 23), as of Android 6.0.1, it does not. getAllNetworks() does not list all networks (see SO Q#33374766). Looks like a bug.

Alternatively, in order to inform the user about the status of all network connections, a set of callbacks may be registered for each transport type available in NetworkCapabilities (i.e. TRANSPORT_CELLULAR, TRANSPORT_WIFI, TRANSPORT_ETHERNET, TRANSPORT_BLUETOOTH & TRANSPORT_VPN)using registerNetworkCallback.