简体   繁体   English

唯一过滤wifi网络名称

[英]Filter wifi network names uniquely

I want to display a list of available wifi networks in a spinner.我想在微调器中显示可用 wifi 网络的列表。 But with the current version, I'm getting a lot of duplicate networks and white space values when there are many wifi networks in the range.但是在当前版本中,当范围内有很多 wifi 网络时,我会得到很多重复的网络和空白值。 How can I uniquely identify each wifi network name?如何唯一标识每个wifi网络名称? I have tried various methods and still could not get it correctly.我尝试了各种方法,但仍然无法正确获取。 Any help in this regard would be highly appreciated.在这方面的任何帮助将不胜感激。 my current code which is made for android 24 onwards is:我当前为 android 24 制作的代码是:

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            List<ScanResult> wifiList = wifiManager.getScanResults();
            ssids = wifiList.stream()
                    .map(scanResult -> scanResult.SSID)
                    .filter(ssid -> ssid.equals(""))
                    .distinct()
                    .collect(Collectors.toList());

            deviceList = new String[ssids.size()];
            for (int i = 0; i < ssids.size(); i++) {
                deviceList[i] = ((ssids.get(i)));

                if(SCAN_RESULTS_AVAILABLE_ACTION.equals(true)){
                    wifiManager.disconnect();
                }
            }


             ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, 
             android.R.layout.simple_spinner_item, deviceList) {
                @Override
                public View getView(int position, View convertView, ViewGroup parent) {

                    View v = super.getView(position, convertView, parent);
                    if (position == getCount()) {
                        ((TextView)v.findViewById(android.R.id.text1)).setText("");
                        ((TextView)v.findViewById(android.R.id.text1)).setHint(getItem(getCount())); 
                        //"Hint to be displayed"
                    }

                    return v;
                }

                @Override
                public int getCount() {
                    return super.getCount()-1; // you don't display last item. It is used as hint.
                }
            };
            adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
            wifiDeviceList.setAdapter(adapter);
            wifiDeviceList.setSelection(adapter.getCount());//set the hint the default selection so it appears on launch.
            wifiDeviceList.setOnItemSelectedListener(this);
            adapter.notifyDataSetChanged();
        }

If you're seeing the same SSID, it's likely because the BSSID is different.如果您看到相同的 SSID,可能是因为 BSSID 不同。

You may be seeing different routers which belong to different networks but happen to have the same name (eg "netgear" or "dlink").您可能会看到属于不同网络但碰巧具有相同名称的不同路由器(例如“netgear”或“dlink”)。 This is common in dense residential areas.这在密集的住宅区很常见。

You may also be seeing different routers which belong to the same network.您可能还会看到属于同一网络的不同路由器。 These would be different access points on a corporate network, for example, strategically placed to provide maximum coverage.例如,这些将是公司网络上的不同接入点,战略性地放置以提供最大的覆盖范围。

You may also be seeing legitimately duplicated entries.您可能还会看到合法重复的条目。

You have several options for how to deal with these, but the generally accepted approach is to de-duplicate by BSSID, then display a list of SSIDs with signal meters.对于如何处理这些问题,您有多种选择,但普遍接受的方法是按 BSSID 进行重复数据删除,然后显示带有信号计的 SSID 列表。

The problem with your code above is that filter() does not work the way you expect it to.上面代码的问题是filter()不能按您期望的方式工作。 It seems like you're expecting it to filter out ssid.equals("") , when in fact filter() will remove any items which to not match the criteria.似乎您希望它过滤ssid.equals("") ,而实际上filter()将删除任何符合条件的项目。 You can see an example of this on the Stream documentation page .您可以在Stream文档页面上看到一个示例。

You want to change .filter(ssid -> ssid.equals("")) to be您想将.filter(ssid -> ssid.equals(""))更改为

.filter(ssid -> !ssid.equals(""))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM