Even with all the permissions granted the startScan() function still returns false. So my question is: is there a way to scan access points on android 29 or higher?
The problem was in asking the user for permission. My working code
-Manifest:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
-And for asking user for permissions I used EasyPermissions:
boolean scanWifi(WifiManager wifiManagerToScan) {
String[] perm = {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.CHANGE_WIFI_STATE};
if (EasyPermissions.hasPermissions(this, perm)) {
registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
boolean scanStarted = wifiManagerToScan.startScan();
if (scanStarted) {
Toast.makeText(this, "Scanning...", Toast.LENGTH_SHORT).show();
return true;
} else {
Toast.makeText(this, "ERROR", Toast.LENGTH_SHORT).show();
return false;
}
}
else {
EasyPermissions.requestPermissions(this, "WE REQUIRE THESE PERMISSIONS FOR THE APP TO FUNCTION", 123, perm);
return false;
}
}'