2012-09-17 54 views
13

我需要獲得所有AP的BSSID(MAC)。請在下面的代碼塊中找到。如何獲得所有wifi接入點的BSSID?

List<WifiConfiguration> test = wifiManager.getConfiguredNetworks(); 
    for(int k=0;k<test.size();k++){ 
     Log.d("acheck", "test BSSID = "+test.get(k).BSSID);   
     Log.d("acheck", "test BSSID = "+test.get(k).BSSID);  
    } 

但上面的代碼塊返回SSID正確,但BSSID爲空。我想用用戶定義的SSID連接到最強的接入點。是否有任何API方法可以用用戶定義的SSID獲得最佳信號強度的AP(在我的情況下,有許多SSID和單個SSID,可用多個AP)。

回答

5

在配置中它只是設置部分(例如像對話框,詢問用戶名/密碼/ WPA或NULL等等等等)

要想從AP BSSID,你需要從ScanResult

public void loadWifiAvailableList() { 
    List<ScanResult> results = wifiManager.getScanResults(); 
    ScanResult bestSignal = null; 
    etWifiList.setText(""); // etWifiList is EditText 
    int count = 1; 
    for (ScanResult result : results) { 
     etWifiList.append(count++ + ". " + result.SSID + " : " + result.level + "\n" + 
       result.BSSID + "\n" + result.capabilities + "\n" + 
       "\n=======================\n"); 

     if (bestSignal == null || WifiManager.compareSignalLevel(bestSignal.level, result.level) < 0) 
     { 
     bestSignal = result; 
     } 
    } 


    String message = String.format("%s networks found. %s is the strongest.", 
     results.size(), bestSignal.SSID + " : " + bestSignal.level); 
    Toast.makeText(this, message, Toast.LENGTH_LONG).show(); 
} 
+1

適應了這個優秀的片段,並與來自Marakana一些代碼(古拉爵米爾,馬爾科!)混合以解壓一切從設備連接和wifi網絡信息存儲。 :) UPVOTED! http://stackoverflow.com/a/15976165/1166727 –

11

適於通過@ToonSuperLove答案得到它,主要是爲了避免NPE上。

public class WifiTest extends Activity { 

    public void loadWifiAvailableList(EditText etWifiList) { 
     WifiManager wifiManager = (WifiManager) 
            getSystemService(Context.WIFI_SERVICE); 
     List<ScanResult> results = wifiManager.getScanResults(); 
     String message = "No results. Check wireless is on"; 
     if (results != null) { 
      final int size = results.size(); 
      if (size == 0) message = "No access points in range"; 
      else { 
       ScanResult bestSignal = results.get(0); 
       etWifiList.setText(""); // etWifiList is EditText 
       int count = 1; 
       for (ScanResult result : results) { 
        etWifiList.append(count++ + ". " + result.SSID + " : " 
          + result.level + "\n" + result.BSSID + "\n" 
          + result.capabilities + "\n" 
          + "\n=======================\n"); 
        if (WifiManager.compareSignalLevel(bestSignal.level, 
          result.level) < 0) { 
         bestSignal = result; 
        } 
       } 
       message = String.format(
         "%s networks found. %s is the strongest.", size, 
         bestSignal.SSID + " : " + bestSignal.level); 
      } 
     } 
     Toast.makeText(this, message, Toast.LENGTH_LONG).show(); 
    } 
} 
+0

什麼是NPE? \t \t \t \t – x0a

+0

@ x0a我想這是'NullPointerException' – renyuneyun

1

要獲取當前連接的WIFI網絡的BSSID,請使用WiFiInfo類。

  WifiManager wifiMan = (WifiManager) context.getSystemService(
          Context.WIFI_SERVICE); 
      WifiInfo wifiInfo = wifiMan.getConnectionInfo(); 

      String macAddr = wifiInfo.getMacAddress(); 
      String bssid = wifiInfo.getBSSID(); 
相關問題