How to get name of wifi-network out of android using android API?

无人久伴 提交于 2019-12-27 11:56:27

问题


I thought that I should use NetworkInterface::getDisplayName. I got some name, but this name is different that this name which I can see, when I choosing to which network I want to connect.

Please help..

[EDIT]

acording to Loxley answer:

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

回答1:


android.net.wifi.WifiInfo.getSSID?




回答2:


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;
}



回答3:


This (mix and match of various answers from Marakana and others) will simultaneously get everything you want to extract from:

  1. all wifi routers in range
  2. connected wifi router
  3. all stored wifi networks (on your device)

    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;
    }
    

DISCLAIMER: while this is working code, not pseudo code, its only purpose is to illustrate the methods for data extraction from wifi connections and it should be adapted (and cleaned) before use.



来源:https://stackoverflow.com/questions/3531940/how-to-get-name-of-wifi-network-out-of-android-using-android-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!