0

I am trying to show the list of all the wi-fi networks in the vicinity. The code that I have written, gives an error, as soon as the Wi-fi is turned on in the application, and if the wi-fi is already on, the application, does not even launch. The logcat is below the attached code.

EDIT - The new code that I have written, is shown below: CODE:

package com.example.wifiapplication;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiConfiguration.AuthAlgorithm;
import android.net.wifi.WifiConfiguration.GroupCipher;
import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.net.wifi.WifiConfiguration.PairwiseCipher;
import android.net.wifi.WifiConfiguration.Protocol;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener, OnCheckedChangeListener{

    Button search;
    Switch change;
    ListView list;
    int size=0;
    List <ScanResult> results;
    ArrayList <HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
    SimpleAdapter adapter;
    WifiManager manage;
    String ITEM_KEY = "key";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
        search = (Button) findViewById(R.id.bSearch); 
        change = (Switch) findViewById(R.id.wifi);
        list = (ListView) findViewById(R.id.lv);
        change.setOnCheckedChangeListener(this);
        search.setOnClickListener(this);
        manage = (WifiManager) getSystemService(Context.WIFI_SERVICE);
            change.setChecked(manage.isWifiEnabled());
    }


    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
         WifiConfiguration config = new WifiConfiguration();
         arraylist.clear();          
         if(manage.startScan()){

            Toast.makeText(this, "Scanning...." + size, Toast.LENGTH_SHORT).show();
            try 
            {
                size = size - 1;
                while (size >= 0) 
                {   
                    HashMap<String, String> item = new HashMap<String, String>();                       
                    item.put(ITEM_KEY, results.get(size).SSID + "  " + results.get(size).capabilities);

                    arraylist.add(item);
                    size--;
                    adapter.notifyDataSetChanged();                 
                } 
            }
            catch (Exception e)
            { }         
         }
       }    


    @Override
    public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
        if(arg1==true)
        {
        manage.setWifiEnabled(true);
        //search.setText("Start Searching");
        this.adapter = new SimpleAdapter(MainActivity.this, arraylist, R.layout.row, new String[] { ITEM_KEY }, new int[] { R.id.list_value });
        list.setAdapter(adapter);

         registerReceiver(new BroadcastReceiver(){

            @Override
            public void onReceive(Context arg0, Intent arg1) {
                // TODO Auto-generated method stub
                results = manage.getScanResults();
                size = results.size();
            }

         }, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

        //Search for available Wi-Fi networks;
           }
        else{
            Toast.makeText(getApplicationContext(), "Wi-Fi is disabled", Toast.LENGTH_LONG).show();;
            manage.setWifiEnabled(false);
            search.setText("Turn on Wi-Fi");

            }
        }

        // TODO Auto-generated method stub

}

LOGCAT:

06-23 10:04:18.200: E/AndroidRuntime(1173): FATAL EXCEPTION: main
06-23 10:04:18.200: E/AndroidRuntime(1173): Process: com.example.wifiapplication, PID: 1173
06-23 10:04:18.200: E/AndroidRuntime(1173): java.lang.NullPointerException
06-23 10:04:18.200: E/AndroidRuntime(1173):     at com.example.wifiapplication.MainActivity.onCheckedChanged(MainActivity.java:94)
06-23 10:04:18.200: E/AndroidRuntime(1173):     at android.widget.CompoundButton.setChecked(CompoundButton.java:127)
06-23 10:04:18.200: E/AndroidRuntime(1173):     at android.widget.Switch.setChecked(Switch.java:666)
06-23 10:04:18.200: E/AndroidRuntime(1173):     at android.widget.CompoundButton.toggle(CompoundButton.java:87)
06-23 10:04:18.200: E/AndroidRuntime(1173):     at android.widget.CompoundButton.performClick(CompoundButton.java:99)
06-23 10:04:18.200: E/AndroidRuntime(1173):     at android.view.View$PerformClick.run(View.java:18422)
06-23 10:04:18.200: E/AndroidRuntime(1173):     at android.os.Handler.handleCallback(Handler.java:733)
06-23 10:04:18.200: E/AndroidRuntime(1173):     at android.os.Handler.dispatchMessage(Handler.java:95)
06-23 10:04:18.200: E/AndroidRuntime(1173):     at android.os.Looper.loop(Looper.java:136)
06-23 10:04:18.200: E/AndroidRuntime(1173):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-23 10:04:18.200: E/AndroidRuntime(1173):     at java.lang.reflect.Method.invokeNative(Native Method)
06-23 10:04:18.200: E/AndroidRuntime(1173):     at java.lang.reflect.Method.invoke(Method.java:515)
06-23 10:04:18.200: E/AndroidRuntime(1173):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-23 10:04:18.200: E/AndroidRuntime(1173):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-23 10:04:18.200: E/AndroidRuntime(1173):     at dalvik.system.NativeStart.main(Native Method)

Can someone please help me out, where am I erring, and what can be done to solve this issue out ?

BumbleBee
  • 103
  • 1
  • 3
  • 11

1 Answers1

1

The result of scanning is not synchronous. You have to register a BroadcastReceiver to listen for Intent with action WifiManager.SCAN_RESULTS_AVAILABLE_ACTION and then, to show the results, use:

List <ScanResult> results = manage.getScanResults();
Sufian
  • 6,405
  • 16
  • 66
  • 120
hoomi
  • 1,882
  • 2
  • 16
  • 17
  • Can you please elaborate a bit? With some example, or maybe some reference ? – BumbleBee Jun 23 '14 at 12:32
  • There are plenty of examples out there here is one example http://stackoverflow.com/questions/5452940/how-can-i-get-android-wifi-scan-results-into-a-list – hoomi Jun 23 '14 at 12:37
  • A scan is not instant. Therefore you get the results back via a Broadcast Intent when it done scanning. – RvdK Jun 23 '14 at 14:30
  • I have updated the question, can you please tell me what the error is, by looking at the current code, and the LOGCAT ? – BumbleBee Jun 24 '14 at 04:36
  • What is at line 94? Exception is poitinting to that – hoomi Jun 24 '14 at 05:28