Scan_Results_Available_Action Return Empty List in Android 6.0

SCAN_RESULTS_AVAILABLE_ACTION return empty list in Android 6.0

As of Android 6.0, permission behaviour has changed to runtime. To use a feature that requires a permission, one should check first if the permission is granted previously. Using checkSelfPermission(permissionString) method a result is returned, wither ther permission is PERMISSION_GRANTED or PERMISSION_DENIED.

If permission isn't granted or it is first time, a request for permission should be made. Giving a user an option to grant or deny.

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
PERMISSIONS_REQUEST_CODE_ACCESS_COARSE_LOCATION);
//After this point you wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method

}else{
getScanningResults();
//do something, permission was previously granted; or legacy device
}

If your code is running on device prior to M, you proceed with your code, permission was granted using legacy method.

Once requested for permission, dialog will be shown to user. His/her response will be delivered as:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
if (requestCode == PERMISSIONS_REQUEST_CODE_ACCESS_COARSE_LOCATION
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Do something with granted permission
mWifiListener.getScanningResults();
}
}

After that, you can check if the Location Services is ON, using LocationServices.SettingsApi and request the user to enable if this options is disabled. This is possible with Play Services LocationSettingsStatusCodes.RESOLUTION_REQUIRED callback.

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

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.

getScanResults with Android 6.0 returns empty List

Does your app has the location permission enabled? Android 6 introduced inAPP permission which means that when you are installing the app, there is no question about permissions. The permissions is requesting when needed in runtime. But if you check Settings -> Applications -> (YOUR APP) -> Permissions and the location permission is off, then you are not allowed to get location.

Android Pie and WiFiManger.getScanResults() returns empty list or Android Oreo WiFiManger.startScan() returns false

Starting with Android 6, accessing scan results for either Wi-Fi or Bluetooth requires you to hold either ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION. I see that you have declared ACCESS_COARSE_LOCATION in your manifest, but you will also need to modify your Activity to request that permission at runtime for Android 6.0 and above. For example:

private static final String RC_LOCATION = 1;

@Override
protected void onResume() {
super.onResume();

String location = android.Manifest.permission.ACCESS_COARSE_LOCATION;
if (ActivityCompat.checkSelfPermission(this, location) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(new String[] { locationPermission }, RC_LOCATION);
} else {
startWifiScan();
}
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] results) {
if (requestCode == RC_LOCATION) {
if (results[0] == PackageManager.PERMISSION_GRANTED) {
startWifiScan();
} else {
// user rejected permission request
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, results);
}
}

private void startWifiScan() {
// do wi-fi scanning
}

Android WifiManager::getScanResults() still returns empty list

Starting from Android 6, you have to enable location services in order to get desired results. Granting permissions is just the half work done.

You can also fire an intent to redirect user to this setting:

Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(myIntent);

Location services are necessary because when you access the scan results, you can access the information like BSSID (address of the access point). This information can also be used to detect the location of device. By requiring location services, OS ensures that user understands their location information is being accessed when they use your app.

WifiManger.getScanResults() returns empty list on android

This worked. I just had to give Access_Fine_Location permission.

WifiManager returns an empty list on my Xperia Z3+ but works as expected on my Galaxy S4

As of Android 6.0 permission behaviour has moved to runtime. This means we need to handle permissions differently.

See this answer here:

https://stackoverflow.com/a/32151901/571875

Android Studio: getScanResults returns empty but permission is given

If you are using it on a device with Android 6 or higher you have to enable location services to get the scan results, even if you have the right permissions.

For more information see here: https://stackoverflow.com/a/32151901/6951450

No scan results received

As of Android 6.0 you have to request permissions in runtime, this answer helped me: Answer



Related Topics



Leave a reply



Submit