Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.5k 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 Reply

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...