Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android M: Unable to remove WIFI AP programmatically

In Android M: I am using below code to remove current connected WIFI AP.

void RemoveConnectedNetwork(){
    int ID=_wifiManager.getConnectionInfo().getNetworkId();
    Log.d("test", "network id = ["+ID+"]");
    boolen ret =_wifiManager.removeNetwork(ID);
    Log.d("test", "removeNetwork return ="+ret);
    _wifiManager.saveConfiguration();
}

but RemoveConnectedNetwork() always returns false.

Although this API was working well in previous releases.

Any solution that can be achieved on this using any other API in Android M?

Thanks.

like image 890
Mohammad Khalid Ahmad Avatar asked Sep 24 '15 08:09

Mohammad Khalid Ahmad


People also ask

How do I permanently delete WiFi networks on Android?

From Settings, tap Network and Wireless, then WiFI to access wireless network options. Tap and hold the WiFi network you want to delete, then select Delete from the menu that appears.


2 Answers

There are some changes in the Wifi Manager in Android 6.0.

Any Wi-Fi configuration created by an active Device Owner can no longer be modified or deleted by the user if WIFI_DEVICE_OWNER_CONFIGS_LOCKDOWN is non-zero.

The user can still create and modify their own Wi-Fi configurations.

Active Device Owners have the privilege of editing or removing any Wi-Fi configurations, including those not created by them.

Please refer to this link for further details: https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html

like image 58
Megha Avatar answered Oct 24 '22 15:10

Megha


As of Android M apps are not allowed to modify networks that they did not create. Any network can be removed from an app if it is configured by that app itself. Check the log from "WifiConfigManager" after calling removeNetwork(int), you will get an error like this UID (app UID) does not have permission to delete configuration ("wifi SSID"capabilities)

There are so many reasons for this, refer to the following code and link for further details. https://android.googlesource.com/platform/frameworks/opt/net/wifi/+/master/service/java/com/android/server/wifi/WifiConfigManager.java

/**
 * Checks if |uid| has permission to modify the provided configuration.
 *
 * @param config         WifiConfiguration object corresponding to the network to be modified.
 * @param uid            UID of the app requesting the modification.
 * @param ignoreLockdown Ignore the configuration lockdown checks for connection attempts.
 */
private boolean canModifyNetwork(WifiConfiguration config, int uid, boolean ignoreLockdown) {
    // System internals can always update networks; they're typically only
    // making meteredHint or meteredOverride changes
    if (uid == Process.SYSTEM_UID) {
        return true;
    }
    // Passpoint configurations are generated and managed by PasspointManager. They can be
    // added by either PasspointNetworkEvaluator (for auto connection) or Settings app
    // (for manual connection), and need to be removed once the connection is completed.
    // Since it is "owned" by us, so always allow us to modify them.
    if (config.isPasspoint() && uid == Process.WIFI_UID) {
        return true;
    }
    // EAP-SIM/AKA/AKA' network needs framework to update the anonymous identity provided
    // by authenticator back to the WifiConfiguration object.
    // Since it is "owned" by us, so always allow us to modify them.
    if (config.enterpriseConfig != null
            && uid == Process.WIFI_UID
            && TelephonyUtil.isSimEapMethod(config.enterpriseConfig.getEapMethod())) {
        return true;
    }
    final DevicePolicyManagerInternal dpmi = LocalServices.getService(
            DevicePolicyManagerInternal.class);
    final boolean isUidDeviceOwner = dpmi != null && dpmi.isActiveAdminWithPolicy(uid,
            DeviceAdminInfo.USES_POLICY_DEVICE_OWNER);
    // If |uid| corresponds to the device owner, allow all modifications.
    if (isUidDeviceOwner) {
        return true;
    }
    final boolean isCreator = (config.creatorUid == uid);
    // Check if the |uid| holds the |NETWORK_SETTINGS| permission if the caller asks us to
    // bypass the lockdown checks.
    if (ignoreLockdown) {
        return mWifiPermissionsUtil.checkNetworkSettingsPermission(uid);
    }
    // Check if device has DPM capability. If it has and |dpmi| is still null, then we
    // treat this case with suspicion and bail out.
    if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN)
            && dpmi == null) {
        Log.w(TAG, "Error retrieving DPMI service.");
        return false;
    }
    // WiFi config lockdown related logic. At this point we know uid is NOT a Device Owner.
    final boolean isConfigEligibleForLockdown = dpmi != null && dpmi.isActiveAdminWithPolicy(
            config.creatorUid, DeviceAdminInfo.USES_POLICY_DEVICE_OWNER);
    if (!isConfigEligibleForLockdown) {
        return isCreator || mWifiPermissionsUtil.checkNetworkSettingsPermission(uid);
    }
    final ContentResolver resolver = mContext.getContentResolver();
    final boolean isLockdownFeatureEnabled = Settings.Global.getInt(resolver,
            Settings.Global.WIFI_DEVICE_OWNER_CONFIGS_LOCKDOWN, 0) != 0;
    return !isLockdownFeatureEnabled
            && mWifiPermissionsUtil.checkNetworkSettingsPermission(uid);
}
like image 30
Akshay Kumar S Avatar answered Oct 24 '22 14:10

Akshay Kumar S