4

我正在尝试制作一个可以创建可用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 真的不同吗?它们有什么不同?谁能解释一下?

4

3 回答 3

10

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

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

于 2013-04-20T12:06:35.677 回答
3

使用以下代码删除具有最高信号强度的重复 ssid

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();
    }
}
于 2014-11-20T18:00:47.480 回答
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);

}
于 2020-02-21T06:53:26.663 回答