Wifimanager Startscan Deprecated. Alternative?
I need to scan all the available wifi networks but the method 'wifiManager.startScan()' is deprecated and I don't find an alternative. Does anyone know a real alternative? I tried
Solution 1:
It is marked deprecated with description: "The ability for apps to trigger scan requests will be removed in a future release." Currently this method works with some restrictions. In fact if you take a closer look at Wi-Fi scanning overview restrictions you can see that you can achieve your goal by meeting the conditions explained under Restrictions.
One more thing, if you are developing a system-privileged app or wondering how those apps get a wifi list even with location service turned off, they use android.Manifest.permission.NETWORK_SETUP_WIZARD
or android.Manifest.permission.NETWORK_SETTINGS
which are system|signature level permissions. Read WifiPermissionsUtil.java:
/**
* API to determine if the caller has permissions to get scan results. Throws SecurityException
* if the caller has no permission.
* @param pkgName package name of the application requesting access
* @param uid The uid of the package
*/publicvoid enforceCanAccessScanResults(String pkgName, int uid) throws SecurityException {
mAppOps.checkPackage(uid, pkgName);
// Apps with NETWORK_SETTINGS & NETWORK_SETUP_WIZARD are granted a bypass.if (checkNetworkSettingsPermission(uid) || checkNetworkSetupWizardPermission(uid)) {
return;
}
// Location mode must be enabledif (!isLocationModeEnabled()) {
// Location mode is disabled, scan results cannot be returnedthrownew SecurityException("Location mode is disabled for the device");
}
// Check if the calling Uid has CAN_READ_PEER_MAC_ADDRESS permission.boolean canCallingUidAccessLocation = checkCallerHasPeersMacAddressPermission(uid);
// LocationAccess by App: caller must have// Coarse Location permission to have access to location information.boolean canAppPackageUseLocation = checkCallersLocationPermission(pkgName, uid);
// If neither caller or app has location access, there is no need to check// any other permissions. Deny access to scan results.if (!canCallingUidAccessLocation && !canAppPackageUseLocation) {
thrownew SecurityException("UID " + uid + " has no location permission");
}
// Check if Wifi Scan request is an operation allowed for this App.if (!isScanAllowedbyApps(pkgName, uid)) {
thrownew SecurityException("UID " + uid + " has no wifi scan permission");
}
// If the User or profile is current, permission is granted// Otherwise, uid must have INTERACT_ACROSS_USERS_FULL permission.if (!isCurrentProfile(uid) && !checkInteractAcrossUsersFull(uid)) {
thrownew SecurityException("UID " + uid + " profile not permitted");
}
}
Post a Comment for "Wifimanager Startscan Deprecated. Alternative?"