1

I want to retrieve the IP address(es) assigned to the ethernet- (not WiFi) interface of my Android tablet. How can I do this programmatically? I only stumble upon solutions for WiFi and some other making assumptions about the name of the enterface, i.e. 'eth0' or such. I don't feel this is good enough. How can I know the type of the interface and get its assigned IP's?

I'd rather do it programmatically in code, without scripts, reflection or assumptions. Is there such a way?

I am on API min-level 19.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
JohnyTex
  • 3,323
  • 5
  • 29
  • 52
  • Have you tried the "traditional Java approach" via [NetworkInterfaces.getNetworkInterfaces()](https://developer.android.com/reference/java/net/NetworkInterface#getNetworkInterfaces())? It lists you all interfaces. – Robert Mar 03 '20 at 13:27
  • Yes, but how do I know the type of the interfaces contained therein? – JohnyTex Mar 03 '20 at 13:27
  • you can check connected network see: https://stackoverflow.com/a/22304485/2855810 Also you can get ip address by network type. Check this answer : https://stackoverflow.com/a/10683636/2855810 – Ergin Ersoy Mar 03 '20 at 13:29
  • By elimination: If you know the Wifi address and remove the localhost interface what remains should be the Ethernet interface. Additionally you could check the interface name. – Robert Mar 03 '20 at 13:30
  • Elimination doesn't work because I have to make lots of assumptions of which interfaces may exist. For example I also have the following types on my tablet: rmnet0, rmnet1 rmnet2, sit0, p2p0, in other words: Lots more than just wifi och loopback. Also an ethernet interface may be named anything as far as I know. – JohnyTex Mar 03 '20 at 13:55
  • Strange thing is connectivityManager.getActiveNetworkInfo() seems to contain no information about which interface it actually uses. networkInfo.getExtraInfo() seem to return the mac-address, but it looks according to docs it might return just about anything. – JohnyTex Mar 03 '20 at 13:58

0 Answers0