使用 android 设备的热点获取 Android 设备的 IP 地址
Getting the IP Address of an Android Device using it's hotspot from another android device
我启用了两个 android
devices.On 一个设备,我打开了热点,我正在从其他设备连接到该热点。现在我想获取第一个设备的 IP
地址。我怎么才能得到它。因为我想将文件发送到第一台设备,所以我需要 device.I 的 IP
地址,我假设 WifiManager
将用于此目的,但我不知道如何使用。为此,我阅读了一些其他使用 NetworkInterface
的线程。
-乌斯曼
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("ServerActivity", ex.toString());
}
我启用了两个 android
devices.On 一个设备,我打开了热点,我正在从其他设备连接到该热点。现在我想获取第一个设备的 IP
地址。我怎么才能得到它。因为我想将文件发送到第一台设备,所以我需要 device.I 的 IP
地址,我假设 WifiManager
将用于此目的,但我不知道如何使用。为此,我阅读了一些其他使用 NetworkInterface
的线程。
-乌斯曼
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("ServerActivity", ex.toString());
}