2010-08-20 74 views
55

我認爲我應該使用NetworkInterface :: getDisplayName。我有一些名字,但是當我選擇我想要連接的網絡時,這個名字與我可以看到的名稱不同。如何從Android使用android API獲取wifi網絡的名稱?

請幫助..

[編輯]

acording到洛克斯利答案:

WifiManager wifiMgr = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE); 
WifiInfo wifiInfo = wifiMgr.getConnectionInfo(); 
String name = wifiInfo.getSSID(); 
+2

有誰知道需要哪些權限才能做到這一點? – 2014-01-15 09:41:10

+8

android.permission.ACCESS_WIFI_STATE – 2014-01-29 23:04:02

回答

39

android.net.wifi.WifiInfo.getSSID

17

本(混合和Marakana等各種答案的匹配)將同時得到你想要提取的一切:

  1. 所有WiFi路由器範圍
  2. 連接無線路由器
  3. 所有存儲WiFi網絡(在設備上)

    public String getCurrentSsid(Context context) { 
    
        String ssid = null; 
        ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
        NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
        if (networkInfo.isConnected()) { 
        final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 
        final WifiInfo connectionInfo = wifiManager.getConnectionInfo(); 
        if (connectionInfo != null && !(connectionInfo.getSSID().equals(""))) { 
         //if (connectionInfo != null && !StringUtil.isBlank(connectionInfo.getSSID())) { 
         ssid = connectionInfo.getSSID(); 
        } 
    // Get WiFi status MARAKANA 
        WifiInfo info = wifiManager.getConnectionInfo(); 
        String textStatus = ""; 
        textStatus += "\n\nWiFi Status: " + info.toString(); 
        String BSSID = info.getBSSID(); 
        String MAC = info.getMacAddress(); 
    
        List<ScanResult> results = wifiManager.getScanResults(); 
        ScanResult bestSignal = null; 
        int count = 1; 
        String etWifiList = ""; 
        for (ScanResult result : results) { 
         etWifiList += count++ + ". " + result.SSID + " : " + result.level + "\n" + 
           result.BSSID + "\n" + result.capabilities +"\n" + 
           "\n=======================\n"; 
        } 
        Log.v(TAG, "from SO: \n"+etWifiList); 
    
        // List stored networks 
        List<WifiConfiguration> configs = wifiManager.getConfiguredNetworks(); 
        for (WifiConfiguration config : configs) { 
         textStatus+= "\n\n" + config.toString(); 
        } 
        Log.v(TAG,"from marakana: \n"+textStatus); 
        } 
        return ssid; 
    } 
    

免責聲明:雖然這是工作代碼,而不是僞代碼,但它的唯一目的是說明從wifi連接中提取數據的方法,它應在使用前進行調整(並清理)。

+1

1.誰是MArakana? 2.檢查:http://stackoverflow.com/a/16137563/281545 – 2013-04-22 00:07:41

+0

@Mr_and_Mrs_D 1. marakana是marko gargenta,許多示例項目的作者。 2.正如我所說,這是一個混合和匹配,包括你引用的示例代碼。 :) – 2013-04-22 16:14:43

+0

我的意思是此代碼中的NPE(請參閱上面鏈接的我的回答) – 2013-04-25 14:19:39

19
public String getWifiName(Context context) { 
    WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 
    if (manager.isWifiEnabled()) { 
     WifiInfo wifiInfo = manager.getConnectionInfo(); 
     if (wifiInfo != null) { 
      DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState()); 
      if (state == DetailedState.CONNECTED || state == DetailedState.OBTAINING_IPADDR) { 
       return wifiInfo.getSSID(); 
      } 
     } 
    } 
    return null; 
}