Example usage for android.content Context WIFI_SERVICE

List of usage examples for android.content Context WIFI_SERVICE

Introduction

In this page you can find the example usage for android.content Context WIFI_SERVICE.

Prototype

String WIFI_SERVICE

To view the source code for android.content Context WIFI_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.net.wifi.WifiManager for handling management of Wi-Fi access.

Usage

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);
    }/*  w  w w .j  ava 2s  .c om*/
}

From source file:Main.java

public static boolean isWiFiEnabled(final Context context) {
    final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    return wifiManager.isWifiEnabled();
}

From source file:Main.java

/**
 * Requires {@link android.Manifest.permission#ACCESS_WIFI_STATE}
 * and {@link android.Manifest.permission#CHANGE_WIFI_STATE} permissions.
 *
 * @param context//from  w  w  w  . j a v  a2s  .c  o  m
 * @return
 */
@SuppressLint("MissingPermission")
public static void reconnectWifi(Context context, int networkId) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (ni != null) {
        WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (networkId > -1) {
            wm.enableNetwork(networkId, true);
        }
    }
}

From source file:Main.java

public static String getMac(Context context) {
    WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = wifi.getConnectionInfo();
    if (info.getMacAddress() == null) {
        return null;
    } else {//w  w w .  j  av  a 2s  .  c o  m
        return info.getMacAddress();
    }
}

From source file:Main.java

public static String getSSID(Context context) {

    WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = wifi.getConnectionInfo();
    if (info.getSSID() == null) {
        return null;
    } else {/*from ww  w . ja va  2s . c  o  m*/
        return info.getSSID();
    }
}

From source file:Main.java

public static WifiInfo getWifiConnectionInfo(Context context) {
    WifiManager mainWifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    ;//from   w w w. j  av a 2s. c o  m
    WifiInfo wifiInfo = mainWifi.getConnectionInfo();
    return wifiInfo;
}

From source file:Main.java

public static void setWifiEnabled(Context context, boolean enabled) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    wifiManager.setWifiEnabled(enabled);
}

From source file:Main.java

public static void acquireTemporaryWakelocks(Context context, long timeout) {
    if (wakeLock == null) {
        PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP
                | PowerManager.ON_AFTER_RELEASE, "PushSMS");
    }/* w ww. ja v  a 2s . c om*/
    wakeLock.acquire(timeout);

    if (wifiLock == null) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        wifiLock = wifiManager.createWifiLock("PushSMS");
    }

    wifiLock.acquire();
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            wifiLock.release();
        }
    }, timeout);
}

From source file:Main.java

/**
 * Requires {@link android.Manifest.permission#ACCESS_WIFI_STATE}
 * and {@link android.Manifest.permission#CHANGE_WIFI_STATE} permissions.
 *
 * @param context/*w  w  w  .jav a  2 s . com*/
 * @return
 */
@SuppressLint("MissingPermission")
public static int disconnectWifi(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (ni != null && ni.isConnectedOrConnecting()) {
        WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        int id = wm.getConnectionInfo().getNetworkId();
        return wm.disconnect() ? id : -1;
    }

    return -1;
}

From source file:Main.java

public static WifiInfo getWifiConnectionInfo(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    return wifiManager.getConnectionInfo();
}