duplicate SSID in scanning wifi result

2019-04-10 22:53发布

问题:

i'm trying to make an app that can create a list of available wifi access point. here's part of the code i used:

x = new BroadcastReceiver()
        {
            @Override
            public void onReceive(Context c, Intent intent) 
            {
                results = wifi.getScanResults();
                size = results.size();
                if (results != null) {
                    for (int i=0; i<size; i++){
                        ScanResult scanresult = wifi.getScanResults().get(i);
                        String ssid = scanresult.SSID;
                        int rssi = scanresult.level;
                        String rssiString = String.valueOf(rssi);
                        textStatus.append(ssid + "," + rssiString);
                        textStatus.append("\n");
                    }
                    unregisterReceiver(x); //stops the continuous scan
                    textState.setText("Scanning complete!");
                } else {
                    unregisterReceiver(x); 
                    textState.setText("Nothing is found. Please make sure you are under any wifi coverage");
                }
            }
        };

both textStatus and textState is a TextView. i can get this to work but sometimes the result shows duplicate SSID but with different signal level, in a single scan. there might be 3-4 same SSIDs but with different signal level.

is it really different SSIDs and what differs them? can anyone explain?

回答1:

Are you having several router modems for the same network? For example: A company has a big wireless network with multiple router modems installed in several places so every room has Wifi. If you do that scan you will get a lot of results with the same SSIDs but with different acces points, and thus different signal level.

EDIT: According to Walt's comment you can also have multiple results despite having only one access point if your modem is dual-band.



回答2:

use below code to to remove duplicate ssids with highest signal strength

public void onReceive(Context c, Intent intent) {
    ArrayList<ScanResult> mItems = new ArrayList<>();
    List<ScanResult> results = wifiManager.getScanResults();
    wifiListAdapter = new WifiListAdapter(ConnectToInternetActivity.this, mItems);
    lv.setAdapter(wifiListAdapter);
    int size = results.size();
    HashMap<String, Integer> signalStrength = new HashMap<String, Integer>();
    try {
        for (int i = 0; i < size; i++) {
            ScanResult result = results.get(i);
            if (!result.SSID.isEmpty()) {
                String key = result.SSID + " "
                        + result.capabilities;
                if (!signalStrength.containsKey(key)) {
                    signalStrength.put(key, i);
                    mItems.add(result);
                    wifiListAdapter.notifyDataSetChanged();
                } else {
                    int position = signalStrength.get(key);
                    ScanResult updateItem = mItems.get(position);
                    if (calculateSignalStength(wifiManager, updateItem.level) >
                            calculateSignalStength(wifiManager, result.level)) {
                        mItems.set(position, updateItem);
                        wifiListAdapter.notifyDataSetChanged();
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}