在扫描wifi结果时重复SSID

时间:2013-04-20 11:48:20

标签: wifi android-wifi ssid

我正在尝试创建一个可以创建可用wifi接入点列表的应用。这是我使用的代码的一部分:

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");
                }
            }
        };

textStatus和textState都是TextView。 我可以让这个工作,但有时结果显示重复的SSID,但在一次扫描中具有不同的信号水平。可能有3-4个相同的SSID,但信号电平不同。

它真的是不同的SSID,它们有什么不同?任何人都可以解释一下吗?

3 个答案:

答案 0 :(得分:5)

您是否为同一网络使用多个路由器调制解调器?例如:一家公司有一个大型无线网络,在几个地方安装了多个路由器调制解调器,因此每个房间都有Wifi。如果您进行扫描,您将获得大量具有相同SSID但具有不同接入点的结果,从而获得不同的信号电平。

编辑: 根据沃尔特的评论,如果您的调制解调器是双频段,尽管只有一个接入点,您也可以有多个结果。

答案 1 :(得分:1)

使用下面的代码来删除具有最高信号强度的重复ssids

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();
    }
}

答案 2 :(得分:0)

这是我简单的解决方案,对我来说很有用

private void scanWifiListNew() {
    wifiManager.startScan();
    List<ScanResult>  wifiList = wifiManager.getScanResults();
    mWiFiList = new ArrayList<>();

    for(ScanResult result: wifiList){
       checkItemExists(mWiFiList, result);
    }

    setAdapter(mWiFiList);
}


private void printList(List<ScanResult> list){
    for(ScanResult result: list){
        int level = WifiManager.calculateSignalLevel(result.level, 100);
        System.out.println(result.SSID + " Level is " + level + " out of 100");
    }
}

private void checkItemExists(List<ScanResult> newWiFiList, ScanResult resultNew){
    int indexToRemove = -1;

    if(newWiFiList.size() > 0) {
        for (int i = 0; i < newWiFiList.size(); i++) {
            ScanResult resultCurrent = newWiFiList.get(i);

            if (resultCurrent.SSID.equals(resultNew.SSID)) {
                int levelCurrent = WifiManager.calculateSignalLevel(resultCurrent.level, 100);
                int levelNew = WifiManager.calculateSignalLevel(resultNew.level, 100);
                if (levelNew > levelCurrent) {
                    indexToRemove = i;
                    break;
                }else indexToRemove = -2;
            }
        }
        if(indexToRemove > -1){
            newWiFiList.remove(indexToRemove);
            newWiFiList.add(indexToRemove,resultNew);
        }else  if(indexToRemove == -1)newWiFiList.add(resultNew);
    } else newWiFiList.add(resultNew);
}

private void setAdapter(List<ScanResult> list) {
    listAdapter = new WifiListAdapter(getActivity().getApplicationContext(), list);
    wifiListView.setAdapter(listAdapter);

}