HelloCW HelloCW - 8 months ago 47
Android Question

How to get unique id of WiFi in android?

I can get a name of WiFi using the code String ssid = wifiInfo.getSSID(),
but you know many WiFi have the same name such as "MyWiFi", I hope to get unique id of a WiFi, how can I do? Thanks!

I hope to upload my file in my app only when I connect to my home WiFi named "MyWiFi", but in other place, there are many WiFi named "MyWiFi".

if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if (networkInfo.isConnected()) {
// Wifi is connected
WifiManager wifiManager = (WifiManager) context .getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String ssid = wifiInfo.getSSID();

Toast.makeText(context, "OK " + ssid, Toast.LENGTH_LONG).show();
} else if(intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if(networkInfo.getType() == ConnectivityManager.TYPE_WIFI && ! networkInfo.isConnected()) {
Toast.makeText(context, "Wifi is disconnected: "+String.valueOf(networkInfo), Toast.LENGTH_LONG).show();


Try to use below network id.


used to identify the network when performing operations on the supplicant.


 WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
     WifiInfo wifiInfo = wifiManager.getConnectionInfo();
     String ssid = wifiInfo.getSSID();
     int networkid = wifiInfo.getNetworkId();
     Toast.makeText(context, "OK " + ssid, Toast.LENGTH_LONG).show();
     Toast.makeText(context, "Network ID  " + networkid, Toast.LENGTH_LONG).show();