使用电话作为接入点的设备的 IP 地址
任何人都可以解释或展示如何获取通过手机的便携式 WI-FI 热点连接的计算机(或其他设备)的 IP 地址吗?
我在此处
public String getLocalIpAddress() {
try {
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()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e(LOG_TAG, ex.toString());
}
return null;
}
但这仅返回默认网关。
我还发现另一个例子< /a> 在这里,这可能只是解决方案,但我不知道如何将其应用于我的情况。具体来说,我看不到 IP 地址在那段代码中的位置。
Can anyone explain or show how to get the IP address of a computer (or other device) that's connected through the phone's portable WI-FI hotspot?
I tried the following code from here
public String getLocalIpAddress() {
try {
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()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e(LOG_TAG, ex.toString());
}
return null;
}
But that only returns the default gateway.
I also found another example here on SO, and it might just be the solution, but I don't know how to apply it to my situation. Specifically I cannot see where the IP address is in that piece of code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下代码将为您提供 ip 地址和连接到 Android 热点设备的支持 wifi 的设备的其他详细信息
Main.java
ClientScanResult.java
}
WIFI_AP_STATE.java
WifiApManager。 java
The following code will give you the ip adrress & other details of the wifi enabled devices connected to the the android hotspot device
Main.java
ClientScanResult.java
}
WIFI_AP_STATE.java
WifiApManager.java
检查 WLAN 接口的 arp 表。在命令行中
cat /proc/net/arp
。Check your arp table for the wlan interface.
cat /proc/net/arp
at the command line.虽然给出了答案,但万一有人对直接答案感兴趣:
另外根据我的理解(我自己没有尝试过),如果您正在使用 aysntask 并且需要在工作线程上执行(不会在主线程上工作) ),也可以采用这个过程。要么使用方法查找热点接口,要么直接使用 _hotspotInterface 作为“wlan0”
为了查找热点,我使用了这种方法(虽然不是很好的例子)
Though the answer if given, but in case anyone is interested in direct answer:
Also as per my understanding (didn't try by myself), if you are using aysntask and need to do on worker thread(won't work on main thread), this process can also be used. Either use method to find hotspotinterface or directly use _hotspotInterface as "wlan0"
To find hotspot I used this method(though not very good example)