Android WifiManager getScanResult complains Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission although declared permission

早过忘川 提交于 2019-12-05 09:51:25

In Android M, you need to ask for the permission which is defined as dangerous in PermissionModel to the user before start using each time, it as such:

private boolean mayRequestLocation() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return true;
        }
        if (checkSelfPermission(ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            return true;
        }
        if (shouldShowRequestPermissionRationale(ACCESS_FINE_LOCATION)) {
            Snackbar.make(mView, R.string.permission_rationale, Snackbar.LENGTH_INDEFINITE)
                    .setAction(android.R.string.ok, new View.OnClickListener() {
                        @Override
                        @TargetApi(Build.VERSION_CODES.M)
                        public void onClick(View v) {
                            requestPermissions(new String[]{ACCESS_FINE_LOCATION}, REQUEST_FINE_LOCATION);
                        }
                    });
        } else {
            requestPermissions(new String[]{ACCESS_FINE_LOCATION}, REQUEST_FINE_LOCATION);
        }
        return false;
    }

Add this to your Activity:

private static final int REQUEST_FINE_LOCATION=0

and load it during runtime with:

loadPermissions(Manifest.permission.ACCESS_FINE_LOCATION,REQUEST_FINE_LOCATION);

To evaluate the results of your permission request, you can override onRequestPermissionsResult method:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case REQUEST_FINE_LOCATION: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // The requested permission is granted.
            }
            else{
                // The user disallowed the requested permission.
            }
            return;
        }

}
Antonios Vortis

MADAO is right: you should turn on GPS to get the WIFI access point list.

But I'm not sure about PEERS_MAC_ADDRESS. If you look at the source code (line 957):

/**
 * Return the results of the most recent access point scan, in the form of
 * a list of {@link ScanResult} objects.
 * @return the list of results
 */
public List<ScanResult> getScanResults(String callingPackage) {
    enforceAccessPermission();
    int userId = UserHandle.getCallingUserId();
    int uid = Binder.getCallingUid();
    boolean canReadPeerMacAddresses = checkPeersMacAddress();
    boolean isActiveNetworkScorer =
            NetworkScorerAppManager.isCallerActiveScorer(mContext, uid);
    boolean hasInteractUsersFull = checkInteractAcrossUsersFull();
    long ident = Binder.clearCallingIdentity();
    try {
        if (!canReadPeerMacAddresses && !isActiveNetworkScorer
                && !isLocationEnabled()) {
            return new ArrayList<ScanResult>();
        }
        if (!canReadPeerMacAddresses && !isActiveNetworkScorer
                && !checkCallerCanAccessScanResults(callingPackage, uid)) {
            return new ArrayList<ScanResult>();
        }
        if (mAppOps.noteOp(AppOpsManager.OP_WIFI_SCAN, uid, callingPackage)
                != AppOpsManager.MODE_ALLOWED) {
            return new ArrayList<ScanResult>();
        }
        if (!isCurrentProfile(userId) && !hasInteractUsersFull) {
            return new ArrayList<ScanResult>();
        }
        return mWifiStateMachine.syncGetScanResultsList();
    } finally {
        Binder.restoreCallingIdentity(ident);
    }
}

The first if is checking canReadPeerMacAddresses which the code for checkPeersMacAddress() is:

/**
 * Returns true if the caller holds PEERS_MAC_ADDRESS.
 */
private boolean checkPeersMacAddress() {
    return mContext.checkCallingOrSelfPermission(
            android.Manifest.permission.PEERS_MAC_ADDRESS) == PackageManager.PERMISSION_GRANTED;
}

If you add the permission you can bypass if (!canReadPeerMacAddresses && !isActiveNetworkScorer && !isLocationEnabled()) {. I've tested but I cannot get WIFI MAC list by just using the permission and disabling location.

MADAO

ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION is necessary. To get a valid result, you also have to turn on GPS or get a PEERS_MAC_ADDRESS permission like Setting.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!