Wifi经理和wifi信息

时间:2013-12-13 18:04:33

标签: java android xml wifi

我想获得一些关于Wifi的信息,比如SSID名称,IP地址和速度,所以我写了这段代码

WifiManager wifiMgr = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();

int ip = wifiInfo.getIpAddress();
int speed = wifiInfo.getLinkSpeed();
speedString = Integer.toString(speed);
mac = wifiInfo.getMacAddress();
ssid = wifiInfo.getSSID();
ipAddress = Formatter.formatIpAddress(ip);

问题是,如果启用了WiFi,但手机没有连接到我显示的任何网络

SSID: 0x
Ip: 0.0.0.0
Speed: -1 mbps

我不想显示此类信息,因此我尝试使用

if(ipAddress=="0.0.0.0") {
    Ip.setSummary("Not connected");
}

但是不行,因为我看到相同的信息(ssid:0x,ip:0.0.0.0 ecc)。我该怎么办?

1 个答案:

答案 0 :(得分:1)

NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (networkInfo.isConnected()) {
WifiManager wifiMgr = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();

int ip = wifiInfo.getIpAddress();
int speed = wifiInfo.getLinkSpeed();
speedString = Integer.toString(speed);
mac = wifiInfo.getMacAddress();
ssid = wifiInfo.getSSID();
ipAddress = Formatter.formatIpAddress(ip);
}

试试这个