Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio: getScanResults returns empty but permission is given

Tags:

android

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!

like image 935
Silver Boy Avatar asked Oct 14 '16 20:10

Silver Boy


2 Answers

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

like image 169
Chris Avatar answered Nov 05 '22 10:11

Chris


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.

like image 33
Sagar Avatar answered Nov 05 '22 09:11

Sagar