Android:选择最强的无线接入点?

时间:2015-04-03 21:48:38

标签: android

我正在尝试显示范围内最强的wifi接入点,其中availbe在我的xml文件中,但我得到类似的东西“,功能:[WPA-PSK-CCMP] [WPA2-PSK-CCMP] [WPS] [ESS],级别:-59,频率:2462,时间戳:1083973302545,距离:?(cm),distancceSD:?(cm)“如果我改变输出”textWifi.setText(a.toString());“ to“textWifi.setText(buf.toString());”然后我得到了所有可用的接入点。

如何在“calculateBestAp()”方法中传递“buf”才能使其正常工作?

我感谢任何帮助。

	private void check_wifi_available() {
		WifiManager wifiManager = (WifiManager) this
				.getSystemService(this.WIFI_SERVICE);

		WifiInfo info = wifiManager.getConnectionInfo();

		final List<ScanResult> results = wifiManager.getScanResults();

		if (results != null) {
			// StringBuffer buf = new StringBuffer();
			StringBuilder buf = new StringBuilder();

			for (int i = 0; i < results.size(); i++) {
				String ssid = results.get(i).SSID;
				if (ssid.startsWith("KD")) {

					buf.append(ssid + "\n");
				}
			}
			
			ScanResult a = calculateBestAP(results);
			textWifi.setText(a.toString());
		}
	}

	private ScanResult calculateBestAP(final List<ScanResult> results) {
		ScanResult bestSignal = null;
		for (ScanResult result : results) {
			if (bestSignal == null
					|| WifiManager.compareSignalLevel(bestSignal.level,
							result.level) < 0)
				bestSignal = result;
		}
		return bestSignal;
	}

1 个答案:

答案 0 :(得分:0)

//* remove the logic that loops around the StringBuffer, all you need is this logic where you create a new list of all access points with "KD" SSID.

     final List<ScanResult> results = wifiManager.getScanResults();
    if (results != null) {

        ArrayList<ScanResult> updatedResults  = new ArrayList<ScanResult>();
        for (int i = 0; i < results.size(); i++) {
            String ssid = results.get(i).SSID;
            if (ssid.startsWith("KD")) {

                updatedResults.add(results.get(i));
            }
        }
        if(updatedResults.size()>0)
        {
          ScanResult a = calculateBestAP(updatedResults);
          textWifi.setText(a.toString());
       }
    }
}


//* you can modify this method or just leave the original code as is if it works for you

private ScanResult calculateBestAP(ArrayList<ScanResult> results) {
        ScanResult bestSignal = (ScanResult)results.get(0);
        for (int i = 1; i < results.size(); i++) 
        {
            ScanResult result = (ScanResult)results.get(i);
            bestSignal = (WifiManager.compareSignalLevel(bestSignal.level, result.level) < 0) ? result : bestSignal;
        }
        return bestSignal;
    }