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;
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With