我需要确定Android设备是否连接到Wifi,如果连接,则获取其Wifi IP地址。
我知道如何使用ConnectivityManager来确定活动网络是否为Wifi网络,我还知道如何使用java.net.NetworkInterface遍历可用的网络接口并获取它们的IP地址。
我不知道如何确定哪个IP地址属于Wifi网络,如果找到多个地址的话。有什么建议吗?
谢谢。
发布于 2011-11-02 12:04:36
请尝试此代码。
ConnectivityManager connec = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
//To fetch the state of the Wi-Fi network in the device
Boolean isWifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
WifiManager wifiMgr = (WifiManager) getBaseContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
//To fetch the name of the Wi-Fi network to which the device is connected
String wifiName = wifiInfo.getSSID();
发布于 2012-07-09 18:13:56
static final int IP_ADDRESS_LENGTH = 32;
public static Integer getSystemWifiIpAddress(Context context)
{
WifiManager wManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wInfo = wManager.getConnectionInfo();
int ipAddress = wInfo.getIpAddress();
if (ipAddress == 0)
return null;
return ipAddress;
}
https://stackoverflow.com/questions/7975473
复制相似问题