Search code examples
javaandroidandroid-wifi

Trigger some code as soon as android app detects a particular Wi-Fi network


Im developing an android app. For this app to work the Wi-Fi needs to be enabled all the time. Because the Wi-Fi is enabled it will keep on scanning for available networks.

I want some function to be called as soon as the Wi-Fi connects to a particular network. How do I achieve this?

I have written the following code but this works only once, how do I make this scan for networks continuously?

ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                      NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                      if(wifi.isConnected()){
                          final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                            final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
                            if (connectionInfo != null && !(connectionInfo.getSSID().equals(""))) {
                              String ssid = connectionInfo.getSSID();
                            }
                          Log.i("wifi", "connected");
                      }
                      else{
                          Log.i("wifi", "not connected");
                      }

Solution

  • Follow the steps and do a trick

    1) Create NetworkChangeReceiver

    public class NetworkChangeReceiver extends BroadcastReceiver {
    
        public static boolean isWifiConnected = true;
        public static final String tag = "NETWORKCHANGERECEIVER";
    
        @Override
        public void onReceive(final Context context, final Intent intent) {
    
            ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if (wifi.isConnected()) {
                final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
                if (connectionInfo != null && !(connectionInfo.getSSID().equals(""))) {
                    String ssid = connectionInfo.getSSID();
                }
                isWifiConnected = true;
                Log.i("wifi", "connected");
            } else {
                Log.i("wifi", "not connected");
                isWifiConnected = false;
            }
        }
    }
    

    2) Add this line to Manifest.xml

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    
        <receiver android:name="com.df.src.NetworkChangeReceiver" >
                    <intent-filter>
                        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                    </intent-filter>
                </receiver>