Example usage for android.net.wifi WifiManager setWifiEnabled

List of usage examples for android.net.wifi WifiManager setWifiEnabled

Introduction

In this page you can find the example usage for android.net.wifi WifiManager setWifiEnabled.

Prototype

public boolean setWifiEnabled(boolean enabled) 

Source Link

Document

Enable or disable Wi-Fi.

Usage

From source file:Main.java

public static String getDevicesMac(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    if (!wifiManager.isWifiEnabled()) {
        wifiManager.setWifiEnabled(true);
    }//from   w  w w .ja  v a  2s . c  o m
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    String mac = wifiInfo.getMacAddress().toString();
    return mac;
}

From source file:Main.java

public static void toggleWifi(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

    if (wifiManager.isWifiEnabled()) {
        wifiManager.setWifiEnabled(false);
    } else {//from  w w  w  .  j  ava 2 s  . c om
        wifiManager.setWifiEnabled(true);
    }
}

From source file:Main.java

public static String getDevicesIP(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    if (!wifiManager.isWifiEnabled()) {
        wifiManager.setWifiEnabled(true);
    }// ww w  . j  av a2  s . c o  m
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int ipAddress = wifiInfo.getIpAddress();
    String ip = (ipAddress & 0xFF) + "." + ((ipAddress >> 8) & 0xFF) + "." + ((ipAddress >> 16) & 0xFF) + "."
            + (ipAddress >> 24 & 0xFF);
    if (ip == null || ip.equals("0.0.0.0")) {

    }
    return ip;
}

From source file:Main.java

public static void tryEnablingWifi(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    if (!wifiManager.isWifiEnabled()) {
        wifiManager.setWifiEnabled(true);
    }//from   www.ja  va 2s .com
}

From source file:Main.java

public static String getWIFIIP(Context context) {
    WifiManager wifimanage = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

    if (!wifimanage.isWifiEnabled()) {
        wifimanage.setWifiEnabled(true);
    }//from  w w w .j  a v a  2 s. c om

    WifiInfo wifiinfo = wifimanage.getConnectionInfo();

    String ip = intToIp(wifiinfo.getIpAddress());
    return ip;
}

From source file:Main.java

public static String getWiFiIP(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    if (!wifiManager.isWifiEnabled()) {
        wifiManager.setWifiEnabled(true);
    }/* w w w  . j  av  a  2  s . co  m*/
    WifiInfo wifiinfo = wifiManager.getConnectionInfo();
    String ip = intToIp(wifiinfo.getIpAddress());

    return ip;
}

From source file:Main.java

private static void setWifi(int wifiVal) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    if (wifiVal == 0 && !wifiManager.isWifiEnabled()) {
        wifiManager.setWifiEnabled(true);
    } else if (wifiVal == 1 && wifiManager.isWifiEnabled()) {
        wifiManager.setWifiEnabled(false);
    }//from w w  w . jav  a  2 s. c  om
}

From source file:Main.java

public static void setWifiEnable(Context context, boolean sw) {
    // <uses-permission
    // android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    // boolean enabled = wifiManager.isWifiEnabled();
    wifiManager.setWifiEnabled(sw);
}

From source file:Main.java

public static boolean connectToNetwork(Context context, String username, String password) {
    boolean status = false;
    try {/*w w  w  .j ava 2  s . com*/
        WifiManager wifi = (WifiManager) context.getSystemService(context.WIFI_SERVICE);

        if (!wifi.isWifiEnabled()) {
            wifi.setWifiEnabled(true);
            Thread.sleep(3000);
        }

        WifiConfiguration netConfig = new WifiConfiguration();
        netConfig.SSID = "\"" + username + "\"";
        netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
        netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        netConfig.preSharedKey = "\"" + password + "\"";
        netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);

        int netId = wifi.addNetwork(netConfig);
        status = wifi.enableNetwork(netId, true);
    } catch (Exception e) {
        status = false;
        e.printStackTrace();
    }

    return status;
}

From source file:Main.java

public static void toggleWifi(Context context) {
    WifiManager wifi_manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    if (wifi_manager != null) {
        switch (wifi_manager.getWifiState()) {
        case WifiManager.WIFI_STATE_DISABLED:
            wifi_manager.setWifiEnabled(true);
            break;
        case WifiManager.WIFI_STATE_ENABLED:
            wifi_manager.setWifiEnabled(false);
            break;
        }/*from   w  w w  . j a  v a  2s  . c  o m*/
    }
}