如何在android marshmallow上获得wifi扫描结果?

时间:2017-08-22 01:55:37

标签: android

我想在我的棉花糖设备上获得wifi扫描结果 首先,我的地区有很多无线网络 如果在棉花糖设备上获得wifi扫描结果。添加ACCESS_COARSE_LOCATION权限。

我的来源

@Override
public void onClick(View v) { 

        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
            int permissionResult = checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION);

            if (permissionResult == PackageManager.PERMISSION_DENIED) {

                if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_COARSE_LOCATION)) {
                    AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                    dialog.setTitle("need permission")
                            .setMessage("permission grant?")
                            .setPositiveButton("yes", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                        requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1000);
                                    }
                                }
                            }).setNegativeButton("no", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(MainActivity.this, "cancel", Toast.LENGTH_LONG).show();
                        }
                    }).create().show();

                } else {
                    requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1000);
                }
            } else {
                result = getScanWifiResult();
            }

    }
}


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode == 1000) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                Log.d(TAG, "request permission");
                result = mUtils.getScanWifiResult();
            }
        } else {
            Toast.makeText(MainActivity.this, "permission denied", Toast.LENGTH_LONG).show();
        }
    }
}



public List<String> getScanWifiResult() {
    List<ScanResult> result;
    mWifiManager.startScan(); // service register
    result = mWifiManager.getScanResults();
    ArrayList<String> ssid = new ArrayList<String>();
    if (result != null) {
        for (ScanResult scan : result) {
             ssid.add(scan.SSID);
        }
    } 
    return ssid;
  }

将此来源放在棉花糖设备或gradle targetSdkVersion以上23。 wifi扫描结果为null,但是对于21以下的gradle targetSdkVersion或kitkat设备 wifi扫描结果很多wifi ap。

为什么在棉花糖设备上wifi扫描结果为空?

1 个答案:

答案 0 :(得分:0)

因为您在else

if(permission == denied)声明中进行了调用

您的result = getScanWifiResult()应该在权限if-else语句之外。