Java Code Examples for android.net.wifi.WifiManager#disableNetwork()

The following examples show how to use android.net.wifi.WifiManager#disableNetwork() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConnectorUtils.java    From WifiUtils with Apache License 2.0 6 votes vote down vote up
private static boolean disableAllButOne(@Nullable final WifiManager wifiManager, @Nullable final WifiConfiguration config) {
    if (wifiManager == null) {
        return false;
    }
    @Nullable
    final List<WifiConfiguration> configurations = wifiManager.getConfiguredNetworks();
    if (configurations == null || config == null || configurations.isEmpty()) {
        return false;
    }
    boolean result = false;

    for (WifiConfiguration wifiConfig : configurations) {
        if (wifiConfig == null) {
            continue;
        }
        if (wifiConfig.networkId == config.networkId) {
            result = wifiManager.enableNetwork(wifiConfig.networkId, true);
        } else {
            wifiManager.disableNetwork(wifiConfig.networkId);
        }
    }
    wifiLog("disableAllButOne " + result);
    return result;
}
 
Example 2
Source File: ConnectorUtils.java    From WifiUtils with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("UnusedReturnValue")
private static boolean disableAllButOne(@Nullable final WifiManager wifiManager, @Nullable final ScanResult scanResult) {
    if (wifiManager == null) {
        return false;
    }
    @Nullable
    final List<WifiConfiguration> configurations = wifiManager.getConfiguredNetworks();
    if (configurations == null || scanResult == null || configurations.isEmpty()) {
        return false;
    }
    boolean result = false;
    for (WifiConfiguration wifiConfig : configurations) {
        if (wifiConfig == null) {
            continue;
        }
        if (Objects.equals(scanResult.BSSID, wifiConfig.BSSID) && Objects.equals(scanResult.SSID, trimQuotes(wifiConfig.SSID))) {
            result = wifiManager.enableNetwork(wifiConfig.networkId, true);
        } else {
            wifiManager.disableNetwork(wifiConfig.networkId);
        }
    }
    return result;
}
 
Example 3
Source File: WiFiUtils.java    From FimiX8-RE with MIT License 5 votes vote down vote up
public static void disConnectCurWifi(Context context) {
    try {
        WifiManager wifiManager = (WifiManager) context.getSystemService("wifi");
        wifiManager.disableNetwork(wifiManager.getConnectionInfo().getNetworkId());
        wifiManager.disconnect();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
 
Example 4
Source File: OBConnectionManager.java    From GLEXP-Team-onebillion with Apache License 2.0 4 votes vote down vote up
public void connectToNetwork_connectToWifi (final String ssid, final String password, final OBUtils.RunLambdaWithSuccess block)
{
    if (ssid == null)
        return;
    //
    final WifiManager wfMgr = (WifiManager) MainActivity.mainActivity.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    //
    String connectionSSID = wfMgr.getConnectionInfo().getSSID();
    if (connectionSSID.startsWith("\"") && connectionSSID.endsWith("\""))
    {
        connectionSSID = connectionSSID.substring(1, connectionSSID.length() - 1);
    }
    //
    SupplicantState connectionState = wfMgr.getConnectionInfo().getSupplicantState();
    //
    MainActivity.log("OBConnectionManager.connectToNetwork_connectToWifi. [" + connectionSSID + "] - [" + connectionState.toString() + "]");
    //
    if (connectionSSID.equals(ssid) && connectionState == SupplicantState.COMPLETED)
    {
        MainActivity.log("OBConnectionManager.connectToNetwork_connectToWifi. already connected to the network");
        connectToNetWork_complete(true, block);
    }
    else if (!ssid.equals(connectionSSID) && connectionState == SupplicantState.COMPLETED)
    {
        MainActivity.log("OBConnectionManager.connectToNetwork_connectToWifi. Connected to OTHER Wifi. Disconnecting current WiFi");
        //
        int currentNetworkID = wfMgr.getConnectionInfo().getNetworkId();
        //
        boolean networkDisabled = wfMgr.disableNetwork(currentNetworkID);
        boolean configurationSaved = wfMgr.saveConfiguration();
        boolean disconnected = wfMgr.disconnect();
        //
        if (!networkDisabled || !configurationSaved || !disconnected)
        {
            MainActivity.log("OBConnectionManager.connectToNetwork_connectToWifi. FAILED to disconnect from current WiFi. Aborting operation");
        }
        else
        {
            connectToNetwork_disableAirplaneMode(ssid, password, block);
        }
    }
    else
    {
        MainActivity.log("OBConnectionManager.connectToNetwork_connectToWifi. Wifi not connected. Go to check airplane, enable wifi and scan");
        connectToNetwork_disableAirplaneMode(ssid, password, block);
        //connectToNetwork_scanForWifi(ssid, password, block);
    }
}