0

I want to use ConnectivityManager.NetworkCallback() to use to take the connected wifi SSID of the android device.

private ConnectivityManager.NetworkCallback mNetworkCallback = new ConnectivityManager.NetworkCallback() {

    @Override
    public void onAvailable(Network network) {
       onNetworkAvailable(network);
       //I think I need to find the connected SSID in here.
    }

    @Override
    public void onLost(Network network) {
       onNetworkLost(network);
    }
};

There is a method named onAvailable(Network network). So then I think I should find the connected SSID inside this method. But I don't know how to use this method and how to call the onAvailable(Network network). So what should I do for that?

Hilton
  • 337
  • 6
  • 19

1 Answers1

0

The onAvailable is an overriden method and you cant call it manually.

It is called automatically whenever the ConnectivityManager.NetworkCallback object detects an available network.

If you want to get the SSID of the wifi network, call below code in the onAvailable method

 WifiManager mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        WifiInfo currentWifi = mainWifi.getConnectionInfo();
 if(currentWifi!=null){
       String wifiSSID = currentWifi.getSSID();
 }
Oluwasegun Wahaab
  • 2,663
  • 3
  • 16
  • 19