Android 6.0错误?有权限,但在Android 6.0中getScanResults()仍返回空列表 [英] Android 6.0 bug ? Have permission but getScanResults() still return empty list in Android 6.0

查看:79
本文介绍了Android 6.0错误?有权限,但在Android 6.0中getScanResults()仍返回空列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Android 6.0版-棉花糖中请求了权限,但是使用getScanResults()时它仍然返回空列表.

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;
}

获得请求权限后,在onRequestPermissionsResult方法中,我获得了ACCESS_FINE_LOCATION和ACCESS_COARSE_LOCATION的许可,但是我仍然无法获得扫描结果

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;
    }
}

这是android M的错误吗?

Is this a bug of android M?

推荐答案

请求权限后,您仍然需要启用WIFI.简而言之,您必须按顺序执行此操作以扫描外围:

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. 请求必要的权限( ACCESS_WIFI_STATE,CHANGE_WIFI_STATE,ACCESS_COARSE_LOCATION ).另外,如您所述,在MM上,您需要在运行时提出要求.
  2. 使用WifiManager#setWifiEnabled(true)启用WIFI
  3. 不必必须以编程方式启用我知道的位置信息访问权限. 但是请阅读下面的注释 .
  4. 您必须为 SCAN_RESULTS_AVAILABLE_ACTION 注册BrodcastReceiver.您可以在此处获得扫描已准备就绪的信号.不管您是通过AndroidManifest注册还是在运行时动态注册,只要在下一步之前完成注册即可.
  5. 您必须使用WifiManager# startScan()才能请求精确到一个网络扫描更新.如果您想要更多,请设置计时器/计时器任务(推荐)或在收到上一个计时器(可能永远不会出现)时重新安排时间
  6. 只有在BroadcastReceiver onReceive上,您才能调用WifiManager#getScanResults() 有可能的结果 .
  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.

注意:在某些手机(Moto X 2014)上,我注意到您需要启用基本位置才能获得任何结果,只有用户(系统用户界面)似乎能够在/离开.如果用户完全不在位置上,即使系统UI可以,我似乎也无法获得非空的结果列表.这可能是由于棉花糖需要在用户应用程序中进行蓝牙和WiFi扫描的位置,以及摩托罗拉的错误实施,或者是最新的棉花糖错误跟踪器中已修复的缺陷,而摩托罗拉的最新OTA中未修复该缺陷,因为这在以下情况中不会发生Nexus 5或Galaxy S6.

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.

这篇关于Android 6.0错误?有权限,但在Android 6.0中getScanResults()仍返回空列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆