简体   繁体   中英

Wifi manager and wifi information

i want to get some information about Wifi like SSID Name, ip address and speed, so i wrote this code

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);

The problem is that if the WiFi is enabled but the phone is not connected to any network i show

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

I do not want to display this type of information so I tried it with

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

But don't work because i see the same information (ssid: 0x, ip: 0.0.0.0 ecc). How can i fix?

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);
}

Try this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM