Search code examples
javaandroidpermissionsandroid-permissionsandroid-wifi

Android6.0 WifiManager.getScanResults()


The size of list is 0, which is the return of WifiManager.getScanResults() in Android6.0 I had given the permission(ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION) to him, but the size of list is still 0. I sure the app has these permission in my phone Setting -> Apps. Do you know the reson? Any Solutions?


Solution

  • As of Android 6.0, permission behaviour has changed to runtime

    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
    }
    
    
    
    @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();
         }
     }