Skip to content →

Android Sort ScanResult by Signal Strength


// Get List of ScanResults
List<ScanResult> wifiList = wifiManager.getScanResults();

// Create Temporary HashMap
HashMap<String, ScanResult> map = 
  new HashMap<String, ScanResult>();

// Add ScanResults to Map to remove duplicates
for (ScanResult scanResult : wifiList) {
  if (scanResult.SSID != null && 
     !scanResult.SSID.isEmpty()) {
    map.put(scanResult.SSID, scanResult);
  }
}

// Add to new List
List<ScanResult> sortedWifiList = 
  new ArrayList<ScanResult>(map.values());

// Create Comparator to sort by level
Comparator<ScanResult> comparator = 
  new Comparator<ScanResult>() {

  @Override
  public int compare(ScanResult lhs, ScanResult rhs) {
    return (lhs.level < rhs.level ? -1 : (lhs.level == rhs.level ? 0 : 1));
  }
};

// Apply Comparator and sort
Collections.sort(sortedWifiList, comparator);            

Published in Android