8

I am trying to create a code that returns me available Wi-Fi networks. But I am facing a problem. getScanResults returns empty, though I have given all possible network permissions.

public class MainActivity extends Activity {

    private static final String  TAG = "My Activity";
    private StringBuilder sb = new StringBuilder();
    private TextView tv;
    List<ScanResult> scanList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        final String CoarseLocation = Manifest.permission.ACCESS_COARSE_LOCATION;
        final String AccessWifi = Manifest.permission.ACCESS_WIFI_STATE;
        final String ChangeWifi = Manifest.permission.CHANGE_WIFI_STATE;

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.textView2);
        Button btn = (Button) findViewById(R. id. button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                if (checkSelfPermission(CoarseLocation) != PackageManager.PERMISSION_GRANTED)
                {
                    requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 123);
                }

                if (checkSelfPermission(AccessWifi) != PackageManager.PERMISSION_GRANTED)
                {
                    requestPermissions(new String[]{Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.ACCESS_WIFI_STATE}, 123);
                }

                if (checkSelfPermission(ChangeWifi) != PackageManager.PERMISSION_GRANTED)
                {
                    requestPermissions(new String[]{Manifest.permission.CHANGE_WIFI_STATE, Manifest.permission.CHANGE_WIFI_STATE}, 123);
                }

                LocationManager lman = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
                boolean network_enabled = false;

                try
                {
                    network_enabled = lman.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
                } catch (Exception ex) {}

                if (!network_enabled)
                {
                    startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
                }

                final WifiManager mWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
                mWifiManager.setWifiEnabled(true);

                IntentFilter filter = new IntentFilter();
                filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);

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

                        List<ScanResult> results = mWifiManager.getScanResults();
                        final int Amount = results.size();

                        Log.v(TAG, "Wifi Scan Results Count: " + Amount);

                        int num = 1;

                        while (num <= Amount)
                        {
                            Log.v(TAG, "SSID  =  " + results.get(num).SSID);

                            num = num+1;
                        }
                    }
                }, filter);

                mWifiManager.startScan();
            }
        });
    }
}

Update:

Here is the error that I get when I try to scan Wi-Fi when location services are enabled. I am into android programming only for a month so can anyone explain what that error means?

10-15 21:55:21.843 21441-21441/com.example.gytis.imlost E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.gytis.imlost, PID: 21441
      java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS flg=0x4000010 (has extras) } in com.example.gytis.imlost.MainActivity$1$1@a300383
          at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:935)
          at android.os.Handler.handleCallback(Handler.java:739)
          at android.os.Handler.dispatchMessage(Handler.java:95)
          at android.os.Looper.loop(Looper.java:148)
          at android.app.ActivityThread.main(ActivityThread.java:5527)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
       Caused by: java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
          at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
          at java.util.ArrayList.get(ArrayList.java:308)
          at com.example.gytis.imlost.MainActivity$1$1.onReceive(MainActivity.java:110)
          at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:925)
          at android.os.Handler.handleCallback(Handler.java:739) 
          at android.os.Handler.dispatchMessage(Handler.java:95) 
          at android.os.Looper.loop(Looper.java:148) 
          at android.app.ActivityThread.main(ActivityThread.java:5527) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 

Please help. Thanks!

Komal12
  • 3,340
  • 4
  • 16
  • 25
Silver Boy
  • 113
  • 1
  • 1
  • 8

3 Answers3

20

If you are using it on a device with Android 6 or higher you have to enable location services to get the scan results, even if you have the right permissions.

For more information see here: https://stackoverflow.com/a/32151901/6951450

Community
  • 1
  • 1
Chris
  • 406
  • 3
  • 8
  • OK, so now I have edited the code. But when it comes to this part, the application crashes. What do I have to do now? – Silver Boy Oct 15 '16 at 16:31
  • I meant this part if (!network_enabled) { Intent myIntent = new Intent( Settings.ACTION_NETWORK_OPERATOR_SETTINGS); getApplicationContext().startActivity(myIntent); } – Silver Boy Oct 15 '16 at 16:32
  • Like desribed [here](http://msdx.github.io/androiddoc/docs/reference/android/provider/Settings.html#ACTION_NETWORK_OPERATOR_SETTINGS), it is possible that your device doesn't have an activity for that. Because you are trying to enable location services use ACTION_LOCATION_SOURCE_SETTINGS. – Chris Oct 15 '16 at 17:19
  • I know you already helped a lot, but one more question. I am new to Android programming so please forgive me if this is a dumb question. What does that error mean? (look edited question) – Silver Boy Oct 15 '16 at 19:02
  • The error means you should change the while loop in the onReceive() method to: `while (num < Amount)` – ya man Oct 15 '16 at 19:31
2

Starting from Android 6, you have to enable location services in order to get desired results. Granting permissions is just the half work done.

You can also fire an intent to redirect user to this setting:

Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(myIntent);

Location services are necessary because when you access the scan results, you can access the information like BSSID (address of the access point). This information can also be used to detect the location of device. By requiring location services, OS ensures that user understands their location information is being accessed when they use your app.

Sagar
  • 23,903
  • 4
  • 62
  • 62
0

I also faced same problem, one day app was working fine, next morning didnt work, getScanResults() was returning 0 although all permission were set in manifest and I seemed to be checking in Activity. Turns out you now have to check for ACCESS_FINE_LOCATION.

So change the code in your onclick to as follows

@Override
        public void onClick(View v)
        { if ( Build.VERSION.SDK_INT >= 23){
        if (ActivityCompat.checkSelfPermission(context, Manifest.
                permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(context,
                Manifest.permission.ACCESS_COARSE_LOCATION) !=
                PackageManager.PERMISSION_GRANTED ){
            
            requestPermissions(new String[]{
                            Manifest.permission.ACCESS_FINE_LOCATION},
                    REQUEST_CODE_ASK_PERMISSIONS);
            Log.i(TAG, "User location NOT ENABLED, waiting for permission");

        }else{
            //Start scanning for wifi
        }
    }}
  

My manifest permissions are:

<uses-permission android:name = "android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name = "android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name = "android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name = "android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

Hope this helps you or someone in the future :)

Robby Lebotha
  • 1,211
  • 11
  • 9