Monday 7 March 2016

Display list of WiFi networks in Android

Android provides the WiFiManager class for managing all aspects of WiFi connectivity. It is used to to define the names of various Intent actions that are broadcast upon any sort of change in WiFi state.

Through this post, we will learn how to display a list of available WiFi networks to which an Android device can be connected. The list can be viewed and updated, and attributes of individual entries can be modified.

WifiManager mainWifiObj;
mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE); 

Methods defined in WifiManager class:


1)addNetwork(WifiConfiguration config)
This method add a new network description to the set of configured networks.

2)createWifiLock(String tag)
This method creates a new WifiLock.

3)disconnect()
This method disassociate from the currently active access point.

4)enableNetwork(int netId, boolean disableOthers)
This method allow a previously configured network to be associated with.

5)getWifiState()
This method gets the Wi-Fi enabled state

6)isWifiEnabled()
This method return whether Wi-Fi is enabled or disabled.

7)setWifiEnabled(boolean enabled)
This method enable or disable Wi-Fi.

8)updateNetwork(WifiConfiguration config)

This method update the network description of an existing configured network.

Sample Code:
  private void getWifiNetworksList(){
        IntentFilter filter = new IntentFilter();
        filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
        final WifiManager wifiManager =
                (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);;
        registerReceiver(new BroadcastReceiver(){

            @SuppressLint("UseValueOf") @Override
            public void onReceive(Context context, Intent intent) {
                sb = new StringBuilder();
                scanList = wifiManager.getScanResults();
                sb.append("\n  Number Of Wifi connections :" + " " +scanList.size()+"\n\n");
                for(int i = 0; i < scanList.size(); i++){
                    sb.append(new Integer(i+1).toString() + ". ");
                    sb.append((scanList.get(i)).toString());
                    sb.append("\n\n");
                }

                wifi_Address.setText(sb);
            }

        },filter);
        wifiManager.startScan();
    }


To Get Full Code Click Here



No comments:

Post a Comment