我正在获取 wifi 信号强度。它在 Android 3.0 中运行良好,但是当我在 Android 4.0 中运行相同的代码并尝试获取信号强度并获得信号强度时,但当我移动到其他位置并尝试获得信号强度时,我得到的强度相同。它没有改变。
这是我获取wifi信号强度的代码:
1)BroadcastReceiver
:
public class WiFiScanReceiver extends BroadcastReceiver {
private static final String TAG = "WiFiScanReceiver";
WiFiDemo wifiDemo;
Context context;
public WiFiScanReceiver(WiFiDemo wifiDemo) {
super();
this.wifiDemo = wifiDemo;
context = wifiDemo;
if(results != null){
receiver.clearAbortBroadcast();
receiver = null;
}
List<ScanResult> results = wifiDemo.wifiManager.getScanResults();
}
@Override
public void onReceive(Context c, Intent intent) {
ScanResult bestSignal = null;
for (ScanResult result : results) {
if (bestSignal == null || WifiManager.compareSignalLevel(bestSignal.level,result.level) < 0)
bestSignal = result;
}
String message = String.format(
"%s networks found. %s is the strongest.", results.size(),
bestSignal.SSID);
Log.i("System out","Results : "+results);
}
}
onCreate()
获得力量的方法
BroadcastReceiver receiver = new WiFiScanReceiver(this);
registerReceiver(receiver, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
for (int i = 0; i < results.size(); i++) {
wifiStrength = wifiNameMap.get(results.get(i).SSID);
if(wifiStrength==null){
wifiStrength = new ArrayList<Integer>();
}
wifiStrength.add(results.get(i).level);
wifiNameMap.put(results.get(i).SSID, wifiStrength);
}