Wifi信号强弱判断

1.通过wifiinfo获取rssi 范围为0到-100 -100到-80信号最差 -80到-70 信号较差 -50到-70 信号较强 -50到0 信号最强
WifiManager wifi_service = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifi_service.getConnectionInfo();
if (wifiInfo != null) {
int rssi = Math.abs(wifiInfo.getRssi());
if (rssi > 100) return;
if (rssi >= 80&&rssi<100) {
wifiImg.setBackgroundResource(R.drawable.wifi_4);
} else if (rssi >= 70&&rssi<80) {
wifiImg.setBackgroundResource(R.drawable.wifi_3);
} else if (rssi >= 50&&rssi<70) {
wifiImg.setBackgroundResource(R.drawable.wifi_2);
} else {
wifiImg.setBackgroundResource(R.drawable.wifi_1);
}
wifiImg.setVisibility(View.VISIBLE);
2. 判断wifi是否连接
public boolean isWifiEnabled() {
WifiManager wifiMgr = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wifiMgr.getWifiState() == WifiManager.WIFI_STATE_ENABLED) {
ConnectivityManager connManager = (ConnectivityManager) getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiInfo = connManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
return wifiInfo.isConnected();
} else {
return false;
}

}

你可能感兴趣的:(WIFI)