Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the SSID from getConfiguredNetworks() added quotation mark?

Tags:

android

I hope to get both the name of current connected WIFI and the name of stored connected WIFI.

I find wifiInfo.getSSID() can get the correct result, such as MyWiFi1,

but the name from config.SSID is added quotation mark, such as "MyWiFi2".

How can I get correct name from config.SSID? Thanks!

public static List<MWiFi> ListPhoneStoredWiFi(Context context) {
    List<MWiFi> myWiFiList= new ArrayList<MWiFi>();
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 

    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    if (wifiInfo!=null){
        if (wifiInfo.getNetworkId()!=-1){
           MWiFi mWiFi=new  MWiFi();
           mWiFi.name=wifiInfo.getSSID();
           mWiFi.networkID=wifiInfo.getNetworkId();
           mWiFi.enabled=true;
           myWiFiList.add(mWiFi);
        }
    }

    // List stored networks
    List<WifiConfiguration> configs = wifiManager.getConfiguredNetworks();

    if (configs!=null){     
      for (WifiConfiguration config : configs) {
          if (config.networkId!=wifiInfo.getNetworkId()) {
             MWiFi mWiFi=new  MWiFi();
             mWiFi.name=config.SSID;
             mWiFi.networkID=config.networkId;
             mWiFi.enabled=true;                 
             myWiFiList.add(mWiFi);
          }
      }
    }   

    return myWiFiList;      
}
like image 219
HelloCW Avatar asked Feb 15 '15 02:02

HelloCW


1 Answers

According to the documentation for WifiConfiguration.SSID and WifiInfo.getSSID(), you should always expect those values to come back in quotes when they represent UTF-8 encoded strings. This is to differentiate them from SSID's that are returned as a string of hexadecimal digits...which is also a value you should expect to handle.

Since the framework tells you to expect the variation, your code ought to handle finding and masking out quote characters.

like image 69
devunwired Avatar answered Nov 15 '22 00:11

devunwired