问题
I am building an application reading the signal strength of each available Wifi access point.
I've written code like:
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
// Get WiFi status
WifiInfo info = wifi.getConnectionInfo();
textStatus.append("\n\nWiFi Status: " + info.toString());
// List available networks
List<WifiConfiguration> configs = wifi.getConfiguredNetworks();
However, I have two problems:
In debugging,
configsonly contains one connection. However, I can see that there are several APs available in the system's wifi setting. I.e.configsis an incomplete list.I don't know how to get the signal strength in
WifiConfiguration.
btw, I am using HTC Hero and Android 1.5.
回答1:
According to the Android API documentation WifiManager.getConfiguredNetworks() does not fill the signal strength paramters. This data only represents the remembered access point settings, not the visible ones.
To get actually visible networks you must call WifiManager.startScan() to initiate WiFi radio scanning and WifiManager.getScanResults() after a while to get the scanning results.
回答2:
below code will help to get bar of wifi:
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
int state = wifi.getWifiState();
if(state == WifiManager.WIFI_STATE_ENABLED) {
List<ScanResult> results = wifi.getScanResults();
for (ScanResult result : results) {
if(result.BSSID.equals(wifi.getConnectionInfo().getBSSID())) {
int level = WifiManager.calculateSignalLevel(wifi.getConnectionInfo().getRssi(),
result.level);
int difference = level * 100 / result.level;
int signalStrangth= 0;
if(difference >= 100)
signalStrangth = 4;
else if(difference >= 75)
signalStrangth = 3;
else if(difference >= 50)
signalStrangth = 2;
else if(difference >= 25)
signalStrangth = 1;
tv.setText(tv.getText() + "\nDifference :" + difference + " signal state:" + signalStrangth);
}
}
}
}
}, new IntentFilter(WifiManager.RSSI_CHANGED_ACTION));
回答3:
mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
receiverWifi = new WifiReceiver();
registerReceiver(receiverWifi, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
class WifiReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
List<ScanResult> wifiList = mainWifi.getScanResults();
ArrayList<WifiConnectionBean> m4MessagesList = new ArrayList<WifiConnectionBean>();
for (int i = 0; i < wifiList.size(); i++) {
ScanResult scanResult = wifiList.get(i);
WifiConnectionBean bean = new WifiConnectionBean();
bean.setConnectionName(scanResult.SSID); // + "--" +
// scanResult.frequency);
bean.setDescription(scanResult.capabilities);
bean.setId(scanResult.SSID);
bean.setLevel(String.valueOf(scanResult.level));
bean.setSignalStrength(String.valueOf(scanResult.BSSID));
m4MessagesList.add(bean);
}
if (m4MessagesList == null) {
Toast.makeText(WifiIdentificationActivity.this,
"WifiConnection not available", Toast.LENGTH_SHORT)
.show();
} else {
String message = "Scanning complete. " + m4MessagesList.size()
+ " connections found!";
}
pd.dismiss();
}
}
where scanResult.SSID gives the signal strength.
回答4:
private void checkNetworkStrengh() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo Info = cm.getActiveNetworkInfo();
if (Info == null || !Info.isConnectedOrConnecting()) {
Log.i(TAG, "No connection");
} else {
int netType = Info.getType();
int netSubtype = Info.getSubtype();
if (netType == ConnectivityManager.TYPE_WIFI) {
Log.i(TAG, "Wifi connection");
WifiManager wifiManager = (WifiManager) getApplication().getSystemService(Context.WIFI_SERVICE);
List<ScanResult> scanResult = wifiManager.getScanResults();
for (int i = 0; i < scanResult.size(); i++) {
Log.d("scanResult", "Speed of wifi"+scanResult.get(i).level);//The db level of signal
}
// Need to get wifi strength
} else if (netType == ConnectivityManager.TYPE_MOBILE) {
Log.i(TAG, "GPRS/3G connection");
// Need to get differentiate between 3G/GPRS
}
}
}
回答5:
WifiManager wifiManager = (WifiManager)
MonitorActivity.this.getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(conf);
int numberOfLevels = 5;
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int level = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), numberOfLevels);
Log.e("level", "" + level);
// you will get the levels. Using these levels you can calculate strenghts.
来源:https://stackoverflow.com/questions/3437694/how-to-get-the-connection-strength-of-wifi-access-points