Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 6.0 bug ? Have permission but getScanResults() still return empty list in Android 6.0

I have request the permission in android version 6.0 - Marshmallow,But it still return empty list when using getScanResults().

 private boolean checkPermission() {

    List<String> permissionsList = new ArrayList<String>();

    if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        permissionsList.add(Manifest.permission.ACCESS_FINE_LOCATION);
    }
    if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        permissionsList.add(Manifest.permission.ACCESS_COARSE_LOCATION);
    }

    if (permissionsList.size() > 0) {
        ActivityCompat.requestPermissions((Activity) mContext, permissionsList.toArray(new String[permissionsList.size()]),
                REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
        return false;
    }
    return true;
}

After request permission, then in the onRequestPermissionsResult method,I have get the permission of ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION, But I still can not the scan result

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                       int[] grantResults) {
    switch (requestCode) {
        case REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS:
            if (permissions.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED ||
                    (permissions.length == 2 && grantResults[0] == PackageManager.PERMISSION_GRANTED &&
                            grantResults[1] == PackageManager.PERMISSION_GRANTED)){
                List<ScanResult> scanResults = mWifi.getScanResults();
                //list is still empty
            }
             else {
                // Permission Denied
                Toast.makeText(mContext, getString(R.string.permission_deny), Toast.LENGTH_LONG).show();
            }
            break;
    }
}

Is this a bug of android M?

like image 951
shaotine Avatar asked Dec 15 '15 11:12

shaotine


2 Answers

You still need to enable WIFI after you request the permission. So in short, you have to do this in sequence for scanning the perimeter:

  1. Request the necessary permissions (ACCESS_WIFI_STATE, CHANGE_WIFI_STATE, ACCESS_COARSE_LOCATION). Additionally, on MM you need to request this at run-time, as you stated.
  2. Enable WIFI with WifiManager#setWifiEnabled(true);
  3. You don't have to enable location access programatically that I know off. But read the note below.
  4. You have to register a BrodcastReceiver for SCAN_RESULTS_AVAILABLE_ACTION. This is where you get the signal that the scans are ready. Doesn't matter if you register through AndroidManifest or dynamically at run-time, as long as it's done before the next step.
  5. You have to WifiManager#startScan() in order to request exactly ONE update for network scans. If you want more, set up a timer/timertask (recommended) or reschedule when you receive the previous one (which might never come)
  6. Only on the BroadcastReceiver onReceive will you be able to call WifiManager#getScanResults() with plausible results.

Note: On some phones (Moto X 2014), I noticed you need basic location enabled to get any results, which only the user (system UI) seems to be able to do trigger on/off. If the user has location completely off, I can't seem to get a non-empty result list, even though the system UI can. This is likely due to Marshmallow needs to have location for Bluetooth and WiFi scans in user apps, and a bad implementation by Motorola, or a defect already fixed in latest Marshmallow bug tracker but not in Motorola's latest OTA, because this doesn't happen in a Nexus 5 or a Galaxy S6.

like image 55
leRobot Avatar answered Sep 30 '22 04:09

leRobot


on nexus 5, with M update, it appears to me I also need to have GPS location turned on to get this working.

like image 38
Jeffrey Liu Avatar answered Sep 30 '22 02:09

Jeffrey Liu