Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);              }         } } 
like image 628
jigar Avatar asked Sep 11 '13 12:09

jigar


People also ask

How do I see all Wi-Fi networks on Android?

In your advanced Wi-Fi settings, select Manage networks. This is usually found beneath Network Settings. Now you'll see a list of all the Wi-Fi networks currently saved on your phone.

How do I get a list of available Wi-Fi networks?

To view available wireless networks in Windows, click the network icon located on the right side of your Taskbar. Depending on your network connection, the network icon will either appear as a computer monitor and network cable, or as five ascending bars.

Why can't I see all available wireless networks?

If no networks are shown in the list, your wireless hardware could be turned off, or it may not be working properly. Make sure it is turned on. You could be out of range of the network. Try moving closer to the wireless base station/router and see if the network appears in the list after a while.

How do I find hidden networks on Android?

Open the system menu. Click the WiFi icon and go to WiFi settings. Press the menu button in the top-right corner of the window and select Connect to Hidden Network.


1 Answers

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.

like image 176
user149408 Avatar answered Oct 15 '22 19:10

user149408