[ACCEPTED]-Android WIFI How To Detect When Specific WIFI Connection is Available-wifi

Accepted answer
Score: 30

You can use BroadcastReceiver to find out 4 that wifi network has changed:

BroadcastReceiver broadcastReceiver = new WifiBroadcastReceiver();

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
context.registerReceiver(broadcastReceiver, intentFilter);

The BroadcastReceiver 3 may look like this. And to check for specific 2 MAC address see the checkConnectedToDesiredWifi() method 1 bellow.

public class WifiBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION .equals(action)) {
            SupplicantState state = intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
            if (SupplicantState.isValidState(state) 
                    && state == SupplicantState.COMPLETED) {

                boolean connected = checkConnectedToDesiredWifi();
            }
        }
    }

    /** Detect you are connected to a specific network. */
    private boolean checkConnectedToDesiredWifi() {
        boolean connected = false;

        String desiredMacAddress = "router mac address";

        WifiManager wifiManager = 
            (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

        WifiInfo wifi = wifiManager.getConnectionInfo();
        if (wifi != null) {
            // get current router Mac address
            String bssid = wifi.getBSSID();
            connected = desiredMacAddress.equals(bssid);
        }

        return connected;
    }
}
Score: 12

As long, as we are nothing like code as you need, for free service, I can 3 only recommend you, to read everything possible 2 about Android and its Network/Wifi possibilities, when 1 creating such app.

  • Sources you should read up and study

http://developer.android.com/reference/android/net/wifi/package-summary.html
how to see if wifi is connected in android
How to get my wifi hotspot ssid in my current android system
How to get name of wifi-network out of android using android API?
Get Wifi Interface name on Android

  • Permissions you should ask for when creating application manifest

AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

(the last one, only if you want it to detect your location, to prevent unnecessary calls)

  • You should also declare, that your application needs wifi to be available in device, to work properly:

AndroidManifest.xml

<uses-feature android:name="android.hardware.wifi" />
Score: 4

Use the standard code to list all available 4 networks:

  • start the scan

    String connectivity_context = Context.WIFI_SERVICE;
                final WifiManager wifi = (WifiManager) getSystemService(connectivity_context);  
    if (wifi.isWifiEnabled()) {
                            wifi.startScan();
                        }
    
  • register a receiver 3 for the data

    IntentFilter i = new IntentFilter();
    i.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
    
    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent i) {
            // TODO Auto-generated method stub
            ScanWiFiActivity a = ScanWiFiActivity.instance();
            WifiManager w = (WifiManager) context
                    .getSystemService(Context.WIFI_SERVICE);
            List<ScanResult> l = w.getScanResults();
            a.Clear();
            for (ScanResult r : l) {
                                  //use r.SSID or r.BSSID to identify your home network and take action
                a.setText(r.SSID + "" + r.level + "\r\n");
            }
        }
    };
    registerReceiver(receiver, i);
    

In the FOR block work your magic 2 and take action when you identify your network 1 by SSID or BSSID

Score: 4

I had exactly the same problem for a project 11 of mine and it took a while to find a solution.

First 10 of all, "connecting to a WiFi" is something 9 very abstract, and it turns out rightly 8 so. In practice, people usually mean all 7 of the following:

  • authenticated with a WiFi access point
  • associated with the access point
  • got an ip address from the network

All these stages (and several 6 more) are associated with different Andoid 5 events. So, without further ado, here is 4 my (slightly modified) code:

public class MyService extends Activity { // or Service

    //... Other stuff

    BroadcastReceiver awaitIPAddress = null;
    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) {
                if (intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE) == SupplicantState.COMPLETED) {
                    //WiFi is associated
                    WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                    WifiInfo wi = wifiManager.getConnectionInfo();
                    if (wi != null) {
                        // Wifi info available (should be, we are associated)
                        if (wi.getIpAddress() != 0) {
                            // Lucky us, we already have an ip address.
                            // This happens when a connection is complete, e.g. after rekeying
                            if (wi.getBSSID().equals("c0:ff:ee:c0:ff:ee")) {
                                // ... Do your stuff here
                                // ...
                                // ...
                            }
                        } else {
                            // No ip address yet, we need to wait...
                            // Battery friendly method, using events
                            if (awaitIPAddress == null) {
                                awaitIPAddress = new BroadcastReceiver() {
                                    @Override
                                    public void onReceive(Context ctx, Intent in) {
                                        WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                                        WifiInfo wi = wifiManager.getConnectionInfo();
                                        if (wi != null) {
                                            if (wi.getIpAddress() != 0) {
                                                if (wi.getBSSID().equals("c0:ff:ee:c0:ff:ee")) {
                                                    // ... Do your stuff here
                                                    // ...
                                                    // ...
                                                }
                                            }
                                        } else {
                                            ctx.unregisterReceiver(this);
                                            awaitIPAddress = null;
                                        }
                                    }
                                };
                                // We register a new receiver for connectivity events
                                // (getting a new IP address for example)
                                context.registerReceiver(awaitIPAddress, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
                            }
                        }
                    }
                } else {
                    // wifi connection not complete, release ip address receiver if registered
                    if (awaitIPAddress != null) {
                        context.unregisterReceiver(awaitIPAddress);
                        awaitIPAddress = null;
                    }
                }
            }
        }
    };

    //... Other stuff

    @Override
    public void onCreate() {
        super.onCreate();
        //... Other stuff
        IntentFilter filter = new IntentFilter();
        filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
        registerReceiver(receiver, filter);
        //... Other stuff
    }

    //... Other stuff

}

Also, don't 3 neglect the appropriate permissions to the 2 manifest:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

And I strongly suspect you will 1 also need:

<uses-permission android:name="android.permission.INTERNET"/>

More Related questions