How to get available wifi networks and display them in a list in android
Asked Answered
K

4

39

Friends, I want to find all available WiFi networks and display them in a list I have tried as below. But it's not working. I have edited my code, and now I got the result but with all the result that I don't need. I only need names of wifi network in my list.

public class MainActivity extends Activity {

    TextView mainText;
    WifiManager mainWifi;
    WifiReceiver receiverWifi;
    List<ScanResult> wifiList;
    StringBuilder sb = new StringBuilder();

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mainText = (TextView) findViewById(R.id.tv1);
        mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

        if (mainWifi.isWifiEnabled() == false)
        {  
             // If wifi disabled then enable it
             Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled",
             Toast.LENGTH_LONG).show();
             mainWifi.setWifiEnabled(true);
         } 

         receiverWifi = new WifiReceiver();
         registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
         mainWifi.startScan();
         mainText.setText("Starting Scan...");

     }

     public boolean onCreateOptionsMenu(Menu menu) {
            menu.add(0, 0, 0, "Refresh");
            return super.onCreateOptionsMenu(menu);
     }

     public boolean onMenuItemSelected(int featureId, MenuItem item) {
            mainWifi.startScan();
            mainText.setText("Starting Scan");
            return super.onMenuItemSelected(featureId, item);
     }

     protected void onPause() {
            unregisterReceiver(receiverWifi);
            super.onPause();
     }

     protected void onResume() {
            registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
            super.onResume();
     }

        // Broadcast receiver class called its receive method
        // when number of wifi connections changed

      class WifiReceiver extends BroadcastReceiver {

            // This method call when number of wifi connections changed
            public void onReceive(Context c, Intent intent) {

                sb = new StringBuilder();
                wifiList = mainWifi.getScanResults();
                sb.append("\n        Number Of Wifi connections :"+wifiList.size()+"\n\n");

                for(int i = 0; i < wifiList.size(); i++){

                    sb.append(new Integer(i+1).toString() + ". ");
                    sb.append((wifiList.get(i)).toString());
                    sb.append("\n\n");
                }

                mainText.setText(sb); 
            }

       }
}
Kiloliter answered 11/9, 2013 at 12:19 Comment(0)
M
45

You need to create a BroadcastReceiver to listen for Wifi scan results:

private final BroadcastReceiver mWifiScanReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context c, Intent intent) {
        if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
            List<ScanResult> mScanResults = mWifiManager.getScanResults();
            // add your logic here
        }
    }
}

In onCreate() you would assign mWifiManager and initiate a scan:

mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
registerReceiver(mWifiScanReceiver,
        new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
mWifiManager.startScan();

getScanResults() will return data only if you have appropriate permissions. For this, add one of the following two lines to your AndroidManifest.xml:

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

Also note that in API 23+, permissions must be requested at runtime. (For a lab environment, you can also grant permissions manually in Settings instead—less coding required, but not recommended for an end-user app.)

Note that the code which handles your scan results would run every time a new scan result is available, updating the result.

Milson answered 11/9, 2013 at 12:30 Comment(7)
Please see my updated code...now i got the all available networks in list but it comes with details like BSSID,level and all that...so i only need the name of that wifi network..can you pls help me to sort it out..!Kiloliter
See the SDK documentation for ScanResult. There is a method called getSSID() (or similar). The SSID is the "display" name of the network.Milson
@BalaVishnu you need the appropriate permissions to do that; I’ve updated my answer. The code definitely works—I have done the exact same thing in my SatStat app, which runs as intended on Android 6.Milson
Sorry, there is a bug in my Nexus 5, it shows the list of WiFi only when GPS is turned on..Your code worksEmbryonic
@BalaVishnu Looks like the GPS setting enables/disables all location services, including wifi scans, so that no location can be inferred from nearby wifis.Milson
android.permission.ACCESS_WIFI_STATE is also needed. Didn't find anything in the docs, but that's what the RuntimeException is telling me. Is this a new thing? Why isn't anybody mentioning that here?Divergence
Must enable the location on device manually if not, it won't show any wifi networks.Snaky
D
14

After Android 6.0

If your Android OS version is 6.0 or above then your application must ask for the following permission at runtime(Either of the following).

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

How to ask the permission at runtime

Just add this code in the onResume method of your activity

@Override
public void onResume()
{
    super.onResume();

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    {
        if(checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
        {
            requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 87);
        }
    }
}

What will happen if you don't have the above permission at Runtime?

Wifi.getScanResults() will return 0 results.

Many people including me have faced this problem in Nexus 5 phones and referred to it as a "bug".

How to scan and read wifi networks? (Deeper Understanding)

Scanning can be requested by

wifiManager.startScan();

boolean startScan() returns true or false immediately based on the fact whether the scan has started successfully or not.
However it starts an asynchronous event which sends an intent (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) when the scan completes. Since the scan results (asynchronous event result) will not be available immediately, you will have to register your activity with a BroadcastReceiver.

Here is the code snippet to read those results(asynchronously) using BroadcastReceiver.

public class WifiScanReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if(intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION))
        {
            List<ScanResult> scanResults = wifimanager.getScanResults();
            // Write your logic to show in the list
        }

    }
}

You can see this demo on BroadcastReceiver

Relevant links

Scan result returns an empty list in Android 6.0

Diplomacy answered 23/2, 2018 at 11:59 Comment(2)
probably "android.permission.ACCESS_FINE_LOCATION" also covers "android.permission.ACCESS_COARSE_LOCATION" if am not wrongLegra
Indeed it does. You can pick any one of the two mentioned Permissions. It will work fine. @LegraDiplomacy
C
6
  class WifiReceiver extends BroadcastReceiver {

       public void onReceive(Context c, Intent intent) {

          sb = new StringBuilder();
          wifiList = mainWifi.getScanResults();

            for (int i = 0; i < wifiList.size(); i++){
                sb.append(new Integer(i+1).toString() + ".");
                sb.append((wifiList.get(i)).SSID);
                sb.append("\n");
            }

          mainText.setText(sb);

        }

  }
Carolacarolan answered 1/9, 2015 at 5:19 Comment(0)
O
0

Android 10 (API level 29) and higher:

A successful call to WifiManager.startScan() requires all of the following conditions to be met:

If your app is targeting Android 10 (API level 29) SDK or higher, your app has the ACCESS_FINE_LOCATION permission.
If your app is targeting SDK lower than Android 10 (API level 29), your app has the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.
Your app has the CHANGE_WIFI_STATE permission.
Location services are enabled on the device (under Settings > Location).

To successfully call WifiManager.getScanResults(), ensure all of the following conditions are met:

If your app is targeting Android 10 (API level 29) SDK or higher, your app has the ACCESS_FINE_LOCATION permission.
If your app is targeting SDK lower than Android 10 (API level 29), your app has the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.
Your app has the ACCESS_WIFI_STATE permission.
Location services are enabled on the device (under Settings > Location).

If the calling app doesn't meet all of these requirements, the call fails with a SecurityException.

this is the code which you can try:

BtWifiscan.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
            }
        }
    });




 public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                       int[] grantResults) {
    if (requestCode == 1/*PERMISSIONS_REQUEST_CODE_ACCESS_COARSE_LOCATION*/
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        permisio_state[1] = 1;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.CHANGE_WIFI_STATE}, 2);
        }
    }
    if (requestCode == 2/*PERMISSIONS_REQUEST_CODE_ACCESS_COARSE_LOCATION*/
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        permisio_state[2] = 2;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 3);
        }
    }
    if (requestCode == 3/*PERMISSIONS_REQUEST_CODE_ACCESS_COARSE_LOCATION*/
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        permisio_state[3] = 3;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_WIFI_STATE}, 4);
        }
    }
    if (requestCode == 4/*PERMISSIONS_REQUEST_CODE_ACCESS_COARSE_LOCATION*/
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        permisio_state[4] = 4;
    }
    if (permisio_state[1] == 1 && permisio_state[2] == 2 && permisio_state[3] == 3 && permisio_state[4] == 4)
        WifiScaning();
}
Ophiuchus answered 4/7, 2023 at 12:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.