Atahar Hossain
Atahar Hossain

Reputation: 336

wifiManager.getScanResults() returns 0 in Android 6.0.1, Samsung S5

In Android 6.0.1, Samsung S5, WifiManager.getScanResults() returns 0. But, in Android 6.0.1, Nexus phone, it works pretty well. If I turn on the GPS on Samsung S5 , then it also works and return the valid scan result list.

But, i think, its not a good idea to turn on GPS. So, I want to know the right way to use getScanResults for obtaining the scan results.

N.B. I gave ACCESS_COARSE_LOCATION , ACCESS_FINE_LOCATION permission as instructed on developer site.

Upvotes: 1

Views: 5829

Answers (1)

Rahul Khurana
Rahul Khurana

Reputation: 8835

You can see at google docs , As of Android 6.0, permission behaviour has changed to runtime. To use a feature that requires a permission, one should check first if the permission is granted previously. Using checkSelfPermission(permissionString) method a result is returned, wither ther permission is PERMISSION_GRANTED or PERMISSION_DENIED.

If permission isn't granted or it is first time, a request for permission should be made. Giving a user an option to grant or deny.

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
   requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                 PERMISSIONS_REQUEST_CODE_ACCESS_COARSE_LOCATION);
    //After this point you wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method

}else{
    getScanningResults();
   //do something, permission was previously granted; or legacy device
}

If your code is running on device prior to M, you proceed with your code, permission was granted using legacy method.

Once requested for permission, dialog will be shown to user. His/her response will be delivered as:

@Override
 public void onRequestPermissionsResult(int requestCode, String[] permissions,
         int[] grantResults) {
     if (requestCode == PERMISSIONS_REQUEST_CODE_ACCESS_COARSE_LOCATION
             && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
         // Do something with granted permission
        mWifiListener.getScanningResults();
     }
 }

After that, you can check if the Location Services is ON, using LocationServices.SettingsApi and request the user to enable if this options is disabled. This is possible with Play Services LocationSettingsStatusCodes.RESOLUTION_REQUIRED callback.

Upvotes: 6

Related Questions