5

Yesterday I update my nexus 5 to lollipop and my application stops working, after a little investigation I found the problem the DhcpInfo isn't is returning null on the netmask variable.

I couldn't find any alternative to this class.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Carlos EduardoL
  • 670
  • 6
  • 14
  • I have the same problem and didn't find a solution yet. However where did you read that DhcpInfo is deprecated? – code monkey Nov 17 '14 at 14:31
  • Seems that the DhcpInfo isn't deprecated anymore, but my app is configured to API 18, in API 18 it use to be deprecated :) http://developer.android.com/sdk/api_diff/19/changes/android.net.wifi.WifiManager.html – Carlos EduardoL Nov 17 '14 at 15:26
  • You use Android Lollipop. So you should use Android API level 21 :) – code monkey Nov 17 '14 at 15:38
  • It seems a bug of Lollipop. I just buy a HTC nexus 9 with Android 5.0, and my App doesn't work on it. After checking the code, DhcpInfo's netmask always return 0. – Raymond Nov 18 '14 at 01:44
  • It's this bug in Android 5: https://code.google.com/p/android/issues/detail?id=82477 – some.birdie Apr 23 '15 at 13:26

1 Answers1

6

You can use getNetworkPrefixLength method of InterfaceAddress, which 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 network prefix length, so you'd have to convert it (/24 for 255.255.255.0 etc.)

mbmc
  • 5,024
  • 5
  • 25
  • 53
phaethon
  • 2,664
  • 1
  • 18
  • 7