(Android Development) WiFi scan list has multiple hotspot filters with the same SSID

In WiFi development, when getScanResults() method is used to obtain the list of WiFi scanned, multiple hotspots with the same SSID and different last four bits of BSSID will appear in the list, and both of them can be connected. Referring to the WiFi filtering mechanism of the system, the filtering strategy implemented here is to remove the hotspots with the same SSID from the list and only keep the hotspots with the strongest signal

    public List<ScanResult> getScanList() {
	    if (mWifiManager != null) {
		    List<ScanResult> olist = mWifiManager.getScanResult();
		    if (olist != null) {
			    List<ScanResult> nlist = new ArrayList<>();
			    WifiInfo info = mWifiManager.getConnectedInfo();
			    for (int i = 0; i < olist.size(); i++) {
				    if (info != null && info.getBSSID().equals(olist.get(i).BSSID)) {
					    // Currently connected devices do not appear in the list
				    // Whether the hotspot SSID is already in the list
				    int position = getItemPosition(nlist, olist.get(i));
				    if (position != -1) { // Already in the list
					    // Same SSID hotspot, strong
					    if (nlist.get(position).level < olist.get(i).level) {
						    nlist.add(position, olist.get(i));
				    } else {
			    if (comparator != null) {
				    // Sort by signal strength
				    Collections.sort(nlist, comparator);
			    return nlist;
	    return null;

The above code has comments, which is easy to understand. The following is how to extract the same SSID coordinates:

	 * Return the coordinates of the item in the list
	private int getItemPosition(List<ScanResult>list, ScanResult item) {
	    for (int i = 0; i < list.size(); i++) {
		    if (item.SSID.equals(list.get(i).SSID)) {
			    return i;
	    return -1;

To get the coordinates of the existing hotspots in nlist, the list.contains() method is not used.

There is a pit. If two hot spots with the same SSID are opened, the weak one will be filtered out.

Is there a better way to filter it

Posted on Fri, 31 Jan 2020 15:08:02 -0500 by gls2ro