Android 中 USB 网络共享模式下 PC 的 IP 地址,无需 WIFI,以编程方式
如何在不使用 WIFI 管理器的情况下以编程方式获取 Android 手机以 USB 网络共享模式连接的 PC 的 IP 地址、默认网关和端口号?
我使用了网络接口,但它没有给我正确的信息,还有其他方法吗?
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
address += inetAddress.getHostAddress().toString() ;
}
}
}
How can I programatically get the Ip address, default gateway and port number of the PC to which the android phone is connected in USB Tethering mode, without using WIFI Manager?
I used network interfaces, but it doesnt give me the correct information, is there any other way?
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
address += inetAddress.getHostAddress().toString() ;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,这是我达到的圆形解决方案,从通过网络接口收到的地址中,我删除了地址中的最后一部分,例如 192.168.1.40,我删除了 40 并迭代从 192.168.1.0 开始的循环并找出实际连接的端口。片段如下
Ok here is the round about solution that I reached, from the address received through the network interface, I remove the last section in the address, like 192.168.1.40, i remove 40 and iterate through a loop starting from 192.168.1.0 and find out the port which actually connects. The snippet is below