2013-04-20 104 views
2

我試圖做一個應用程序,可以創建一個可用的wifi接入點列表。這裏是我使用的代碼的一部分:重複掃描wifi結果中的SSID

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和它們有什麼不同嗎?誰能解釋一下?

+0

對於路人:變化'尺寸= results.size(); if(results!= null){'to'if(results!= null){ size = results.size();' – 2013-11-29 02:03:10

回答

3

您是否有幾臺路由器調制解調器用於同一網絡?例如:一個公司有一個大的無線網絡,在多個地方安裝了多個路由器調制解調器,因此每個房間都有Wifi。如果你這樣做了掃描,你將得到很多具有相同SSID但結果點不同的結果,從而得到不同的信號電平。

編輯: 根據沃爾特的評論,如果你的調制解調器是雙頻帶,儘管只有一個接入點,你也可以得到多個結果。下面的代碼

+0

什麼可能導致這些AP之間的差異?像BSSID或頻道或其他東西?我如何得到它們? – randms26 2013-04-20 12:29:34

+0

是的,如果我沒有記錯,BSSID會有所作爲。爲了得到這些檢查:[ScanResult API](http://developer.android.com/reference/android/net/wifi/ScanResult.html)。換句話說:String bssid = scanresult.BSSID就像你用「ssid」做的那樣 – DuKes0mE 2013-04-20 13:00:49

+0

好吧,我會盡快再試一次並確認結果 – randms26 2013-04-20 13:15:33

1

使用以刪除重複的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(); 
    } 
}