3

I have an Android app that needs the real IP of the device, not the virtual network IP. I don't want the the public IP unless the Chromebook is on a public IP, I just need the private one of the LAN, but right now I'm getting a virtual IP of what I'm guessing is a virtual network for Android apps.

Using both WifiManager and NetworkInterface.getNetworkInterfaces() all I get is the virtual IP. Calling an external server won't work because that would get me the public IP.

I know this has been asked in the past but I just want to check if anyone has found a way yet?

casolorz
  • 8,486
  • 19
  • 93
  • 200
  • Don't know if idea is workable or not: Given that on ChromeOS [Android apps run in a container and not a VM](https://chromium.googlesource.com/chromiumos/docs/+/master/containers_and_vms.md#Don_t-Android-apps-ARC_run-in-a-container-and-not-a-VM), you may want to look NDK based options to get private LAN ip address. – Morrison Chang Jan 14 '21 at 23:48
  • Interesting idea, I have no clue where to get started but thanks for the suggestion. – casolorz Jan 16 '21 at 00:59
  • Why do you need the real IP address? There are other solutions that are designed for Android regarding networking and if we have a better understanding of what the goals of the application are, one of the other APIs may be a better fit. – Alexander N. Jan 20 '21 at 14:23
  • I need two devices to communicate with each other and the secondary device often has very limited capabilities in terms of networking. So usually the user needs to tell the secondary device what the ip of the chromebook is and for that the app must tell the user what ip to give to the secondary device. – casolorz Jan 21 '21 at 15:02

1 Answers1

7

This is answered in more detail on ChromeOS.dev.

To get the IPv4 address assigned to the highest priority network that the Chrome OS device is connected to, examine the android system property arc.net.ipv4.host_address and, if needed, arc.net.ipv4.host_gateway. One way to do this is:

fun getChromeOsIpAddress() : String {
   val process = ProcessBuilder().command("/system/bin/getprop", "arc.net.ipv4.host_address").start()
   val ipAddress = readInput(process.inputStream)
   return ipAddress
}

fun getChromeOsIpGateway() : String {
   val process = ProcessBuilder().command("/system/bin/getprop", "arc.net.ipv4.host_gateway").start()
   val gatewayAddress = readInput(process.inputStream)
   return gatewayAddress
}

This post is licensed under Apache 2.0.

https://developers.google.com/open-source/devplat

Alexander N.
  • 1,458
  • 14
  • 25