HelloCW
HelloCW

Reputation: 2245

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!

Tips: 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();
        }
    }

Upvotes: 1

Views: 6080

Answers (3)

Javier Refuerzo
Javier Refuerzo

Reputation: 399

You can use the BSSID as a unique identifier https://en.wikipedia.org/wiki/Service_set_(802.11_network)#Basic_service_set_identification_.28BSSID.29

WifiManager wifiManager = (WifiManager)     this.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        String ssid = wifiInfo.getSSID();
        Toast.makeText(this, "Name is  " + ssid, Toast.LENGTH_SHORT).show();
        String networkid = wifiInfo.getBSSID();
        Toast.makeText(this, "Network ID  " + networkid,     Toast.LENGTH_SHORT).show();

Upvotes: 0

Arvind Kanjariya
Arvind Kanjariya

Reputation: 2097

Try to use below network id.

getNetworkId()

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

UPDATE YOUR CODE

 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();

Upvotes: 4

KOTIOS
KOTIOS

Reputation: 11194

The unique id of any wifi added is NETWORK_ID[The ID number that the supplicant uses to identify this network configuration entry.].

how do i get network id?

if you have WifiConfiguration object you get get its network id

For more detial on apis look here

Upvotes: 0

Related Questions