You can use the getNetworkPrefixLength InterfaceAddress method that you get from NetworkInterface . It returns the correct value in Lollipop.
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(ipAddress); for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) { short netPrefix = address.getNetworkPrefixLength()); }
Note. It returns the length of the network prefix, so you have to convert it (/ 24 for 255.255.255.0, etc.).
source share