Android 6.0 bug? Allow but getScanResults () still returns an empty list in Android 6.0

I am asking for permission in android version 6.0 - Marshmallow, but it still returns an 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 resolving the request, then in the onRequestPermissionsResult method, I got permission ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION, but I still can not get 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 an android M error?

+5
source share
4 answers

You still need to enable WIFI after requesting permission. In short, you must do this sequentially to scan the perimeter:

  • Request required permissions ( ACCESS_WIFI_STATE, CHANGE_WIFI_STATE, ACCESS_COARSE_LOCATION ). Also, on MM, you need to request this at runtime, as you stated.
  • Enable WIFI with WifiManager # setWifiEnabled (true);
  • You should not enable location access programmatically, which I know. But read the note below .
  • You need to register a BrodcastReceiver for SCAN_RESULTS_AVAILABLE_ACTION . Here you get a signal that the scan is ready . It doesn’t matter if you register through AndroidManifest or dynamically at runtime if this is done before the next step.
  • You need WifiManager # startScan () to request an ONE update only for network scanning. If you want more, set a timer / timertask (recommended) or reschedule when you get the previous one (which will never appear)
  • Only in BroadcastReceiver onReceive can you call WifiManager # getScanResults () with plausible results .

Note. On some phones (Moto X 2014), I noticed that you need to enable the base location in order to get any results that only the user interface (system interface) can apparently turn off. If the user is completely disconnected, I cannot get a non-empty list of results , although the system interface can . This is probably due to the fact that Marshmallow needs a location for scanning Bluetooth and WiFi in user applications and a poor implementation of Motorola, or a defect already fixed in the latest Marshmallow tracker, but not in the latest version of OTA from Motorola, because this does not happen in Nexus 5 or Galaxy S6.

+6
source

on nexus 5, with the M update, it seems to me that I also need to enable GPS coordination to make this work.

+4
source

Perhaps this is caused by the Android runtime permissions management for Android, you can set targetSdkVersion 19 (less than 21 or 23) to try again. It works for me

+1
source

In Android Marshmallow 6.0.1, go to Settings> Connections and click on Storage Location. Click "Location Method" and you will see three options:

  • High accuracy: uses GPS, Wi-Fi and mobile networks.
  • Battery Saver: Uses Wi-Fi and mobile networks.
  • Device only: uses GPS.

Select Battery to get Wi-Fi scan results without turning on GPS.

0
source

Source: https://habr.com/ru/post/1238254/


All Articles