gpt4 book ai didi

android - 如何通过wifi查找设备的IP地址?

转载 作者:行者123 更新时间:2023-11-29 16:12:25 25 4
gpt4 key购买 nike

我想获取IP地址。使用该 IP 地址必须检测连接到特定网络/设备的所有设备。并且应该能够获得 MAC 地址?

 tv=(TextView)findViewById(R.id.tv);
wifi=(WifiManager)getSystemService(WIFI_SERVICE) ;
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
InetAddress inet=InetAddress.getLocalHost();
Toast.makeText(getApplicationContext(),inet.toString(),Toast.LENGTH_LONG).show();
} catch (Exception e) {
System.out.println(" ");
}

最佳答案

如果您想检测连接到任何网络的“模拟器”或安卓设备的 IP 地址,请在您的程序中使用此代码。它将为您提供网络分配给您设备的确切 IP 地址。

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 - 如何通过wifi查找设备的IP地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12052502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com