Example usage for android.net.wifi WifiManager isWifiEnabled

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

Introduction

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

Prototype

public boolean isWifiEnabled() 

Source Link

Document

Return whether Wi-Fi is enabled or disabled.

Usage

From source file:Main.java

static public Boolean toggleWifi(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    return toggleWifi(wifiManager, wifiManager.isWifiEnabled());
}

From source file:Main.java

public static String getCurrentWifiIP(Application app) {
    WifiManager wifiManager = (WifiManager) app.getSystemService(Application.WIFI_SERVICE);
    if (!wifiManager.isWifiEnabled()) {
        return null;
    }/*from   w w  w  .j a  va 2 s . co  m*/

    WifiInfo wifiInfo = wifiManager.getConnectionInfo();

    int ipAddress = wifiInfo.getIpAddress();
    String strIPAddess = ((ipAddress >> 0) & 0xFF) + "." + ((ipAddress >> 8) & 0xFF) + "."
            + ((ipAddress >> 16) & 0xFF) + "." + ((ipAddress >> 24) & 0xFF);

    return strIPAddess;
}

From source file:Main.java

/**
 * network tools/*from   ww w  . j av  a 2 s.c om*/
 * */
public static boolean isWifiEnable(Context context) {

    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

    Log.i(TAG, "wifiEnable" + wifiManager.isWifiEnabled());
    return wifiManager.isWifiEnabled();
}

From source file:Main.java

public static String getIP(Context ctx) {
    WifiManager wifiManager = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
    return wifiManager.isWifiEnabled() ? getWifiIP(wifiManager) : getGPRSIP();
}

From source file:Main.java

public static boolean connectToNetwork(Context context, String username, String password) {
    boolean status = false;
    try {//from   ww w  .jav  a 2s.  co m
        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 boolean isOnWifi(Context context) {
    WifiManager lWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    return lWifiManager == null ? false
            : lWifiManager.isWifiEnabled() && lWifiManager.getConnectionInfo() != null
                    && lWifiManager.getConnectionInfo().getIpAddress() != 0;
}

From source file:Main.java

/**
 * @Thach @SK29/*from  w w  w  .j  ava2 s  .  com*/
 * @Init MAC address and save to preference
 * @param context
 * @return
 */
private static String initMacAddress(Context context) {
    // INIT macAddress
    String macAddress;

    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

    if (wifiManager.isWifiEnabled()) {
        // WIFI ALREADY ENABLED. GRAB THE MAC ADDRESS HERE
        WifiInfo info = wifiManager.getConnectionInfo();
        macAddress = info.getMacAddress();

    } else {
        // ENABLE THE WIFI FIRST
        wifiManager.setWifiEnabled(true);

        // WIFI IS NOW ENABLED. GRAB THE MAC ADDRESS HERE
        WifiInfo info = wifiManager.getConnectionInfo();
        macAddress = info.getMacAddress();

        // NOW DISABLE IT AGAIN
        wifiManager.setWifiEnabled(false);
    }

    if (macAddress == null) {
        macAddress = android.os.Build.ID;
    }

    return macAddress;
}

From source file:Main.java

public static String getDhcpIpString(Context mContext) {
    WifiManager mWifiManager;
    String broadcastIp = null;//from w  w  w .  ja  v  a2 s  . c  o  m
    mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
    if (mWifiManager.isWifiEnabled()) {
        DhcpInfo myDhcpInfo = mWifiManager.getDhcpInfo();
        if (myDhcpInfo == null) {
            Toast.makeText(mContext, "can not get dhcp info", Toast.LENGTH_SHORT).show();
            return null;
        } else {
            try {
                broadcastIp = getBroadcastAddress(myDhcpInfo).getHostAddress();
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            }
        }
        return broadcastIp;
    }
    return null;
}

From source file:Main.java

public static void createWifiAccessPoint(Context context, String ntId, String password) {
    try {/*from   www.  j  ava2 s  . c  o m*/
        WifiManager wifiManager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);

        if (wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(false);
        }

        Method[] wmMethods = wifiManager.getClass().getDeclaredMethods(); //Get all declared methods in WifiManager class     
        for (Method method : wmMethods) {
            if (method.getName().equals("setWifiApEnabled")) {
                WifiConfiguration netConfig = new WifiConfiguration();
                netConfig.SSID = ntId;
                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);
                try {
                    boolean apstatus = (Boolean) method.invoke(wifiManager, netConfig, true);

                    for (Method isWifiApEnabledmethod : wmMethods) {
                        if (isWifiApEnabledmethod.getName().equals("isWifiApEnabled")) {
                            while (!(Boolean) isWifiApEnabledmethod.invoke(wifiManager)) {
                            }
                            ;
                            for (Method method1 : wmMethods) {
                                if (method1.getName().equals("getWifiApState")) {
                                    int apstate;
                                    apstate = (Integer) method1.invoke(wifiManager);
                                }
                            }
                        }
                    }
                    if (apstatus) {
                        Log.d("better", "Access Point Created");
                    } else {
                        Log.d("better", "Failed to create Access Point!");
                    }

                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static boolean isNetworkPresent(Context context) {
    boolean isNetworkAvailable = false;
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    try {//from w w  w  .jav  a 2  s .  co  m

        if (cm != null) {
            NetworkInfo netInfo = cm.getActiveNetworkInfo();
            if (netInfo != null) {

                isNetworkAvailable = netInfo.isConnected();
                //                    Toast.makeText(context, "Connecting...", Toast.LENGTH_SHORT).show();
                //Log.d("NETWORK<<","Connecting...."+netInfo.getReason());
            }
        }
    } catch (Exception ex) {
        //Log.e("Network Avail Error", ex.getMessage());

    }
    //        check for wifi also
    if (!isNetworkAvailable) {

        WifiManager connec = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

        State wifi = cm.getNetworkInfo(1).getState();
        if (connec.isWifiEnabled() && wifi.toString().equalsIgnoreCase("CONNECTED")) {
            isNetworkAvailable = true;
            //Log.d("WIFI NETWORK<<","WIFI connected");
        } else {

            isNetworkAvailable = false;
            // Log.d("WIFI Network<<","WIFI not connected");
        }

    }
    return isNetworkAvailable;

}