Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

google chrome os - Is there any way to get the real ip of a Chromebook from an Android app?

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?

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...