1

I want to search available wifi networks and show them in a list when I click the start button in my application, but my code never finds any WiFi network. I tried to show number of available networks with TextView or Toast, but a 0 is always shown.

Here is my code:

public class MainActivity extends AppCompatActivity {

    //Listview
    private ArrayAdapter<String> adapter;
    private ListView list;
    private ArrayList<String> mylist = new ArrayList<String>();
    ListAdapter adapter1;
    TextView tV;

    //Wifi
    private WifiManager mywifi;
    WifiReceiver myreceiver;
    List<ScanResult> wifiList;
    StringBuilder sb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //definieren
        Button start = (Button) findViewById(R.id.btnstart);

        //Listview
        list = (ListView) findViewById(R.id.lV);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mylist);
        list.setAdapter(adapter);

        //Textview
        tV = (TextView) findViewById(R.id.tV);
        tV.setText("");

        //Wifi
        mywifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        if (mywifi.isWifiEnabled() == false) {
            Toast.makeText(getApplicationContext(), "Wifi is disabled ....making it enabled", Toast.LENGTH_SHORT).show();
            mywifi.setWifiEnabled(true);
        }
        myreceiver = new WifiReceiver();
        registerReceiver(myreceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        mywifi.startScan();

        //buttenevents
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //getWifiNetworksList();
            }
        });

    }

    public void getWifiNetworksList() {
        tV.setText("Wifi is starting");
        tV.setText(myreceiver.Wifilist());

    }

    class WifiReceiver extends BroadcastReceiver {

        public String txt;

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

            sb = new StringBuilder();
            wifiList = mywifi.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");
            }

            tV.setText(sb);//tV...TextView
            Toast.makeText(getApplicationContext(), "reciever finished", Toast.LENGTH_SHORT).show();
        }

        public String Wifilist() {
            return txt;
        }

    }
}
gary
  • 4,227
  • 3
  • 31
  • 58
ogris
  • 21
  • 5
  • [see this link](http://stackoverflow.com/a/18741306/4101906) to create your broadcastreciver, and [this one](http://stackoverflow.com/a/7527380/4101906) to show results in listview – Rahmat Waisi Jul 01 '16 at 19:43
  • i tried both links. But i still get the same answer. – ogris Jul 02 '16 at 12:38

1 Answers1

0

Enable Location Permission.

The code works absolutely fine. Tested on Nexus 5. I just added following permissions on Manifest file

<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_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

If it is still not showing any result. Just make sure that your User has allowed Location permission to your app.
You can do this two ways

Manually

Go to settings in your phone and hit Enable option for the app. (In my case my app name is WifiSolution).

enter image description here

Programmatically

You can also use Android Runtime Permission to do it programmatically.

Community
  • 1
  • 1
Rohit Singh
  • 16,950
  • 7
  • 90
  • 88