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:com.google.android.apps.iosched.util.WiFiUtils.java

public static void installConferenceWiFi(final Context context) {
    // Create config
    WifiConfiguration config = new WifiConfiguration();
    // Must be in double quotes to tell system this is an ASCII SSID and passphrase.
    config.SSID = String.format("\"%s\"", Config.WIFI_SSID);
    config.preSharedKey = String.format("\"%s\"", Config.WIFI_PASSPHRASE);

    // Store config
    final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    int netId = wifiManager.addNetwork(config);
    if (netId != -1) {
        wifiManager.enableNetwork(netId, false);
        boolean result = wifiManager.saveConfiguration();
        if (!result) {
            Log.e(TAG, "Unknown error while calling WiFiManager.saveConfiguration()");
            Toast.makeText(context, context.getResources().getString(R.string.wifi_install_error_message),
                    Toast.LENGTH_SHORT).show();
        }//from  www.  ja va2s  . c  om
    } else {
        Log.e(TAG, "Unknown error while calling WiFiManager.addNetwork()");
        Toast.makeText(context, context.getResources().getString(R.string.wifi_install_error_message),
                Toast.LENGTH_SHORT).show();
    }
}

From source file:com.conferenceengineer.android.iosched.util.WiFiUtils.java

public static void installConferenceWiFi(final Context context) {
    // Create config
    WifiConfiguration config = new WifiConfiguration();
    // Must be in double quotes to tell system this is an ASCII SSID and passphrase.
    config.SSID = String.format("\"%s\"", Config.WIFI_SSID);
    config.preSharedKey = String.format("\"%s\"", Config.WIFI_PASSPHRASE);

    // Store config
    final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    int netId = wifiManager.addNetwork(config);
    if (netId != -1) {
        wifiManager.enableNetwork(netId, false);
        boolean result = wifiManager.saveConfiguration();
        if (!result) {
            Log.e(TAG, "Unknown error while calling WiFiManager.saveConfiguration()");
            Toast.makeText(context, context.getResources().getString(R.string.wifi_install_error_message),
                    Toast.LENGTH_SHORT).show();
        } else {//from w  w  w. j a v  a2s . co m
            Toast.makeText(context, context.getResources().getString(R.string.wifi_install_success_message),
                    Toast.LENGTH_SHORT).show();
        }
    } else {
        Log.e(TAG, "Unknown error while calling WiFiManager.addNetwork()");
        Toast.makeText(context, context.getResources().getString(R.string.wifi_install_error_message),
                Toast.LENGTH_SHORT).show();
    }
}

From source file:com.njlabs.amrita.aid.util.Identifier.java

public static boolean isConnectedToAmrita(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    if (wifiManager != null) {
        String SSID = wifiManager.getConnectionInfo().getSSID().replace("\"", "").trim();
        return SSID.contentEquals("Amrita");
    }/* www  . jav a2  s.co  m*/
    return false;
}

From source file:com.ionicsdk.discovery.Discovery.java

InetAddress getBroadcastAddress() throws Exception {
    WifiManager wifi = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
    DhcpInfo dhcp = wifi.getDhcpInfo();//from   www  .ja  va  2s .com
    // handle null somehow

    int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
    byte[] quads = new byte[4];
    for (int k = 0; k < 4; k++)
        quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
    InetAddress addr = InetAddress.getByAddress(quads);

    Log.d(TAG, "Got broadcast addr:" + addr);
    return addr;
}

From source file:com.jcn.dlna.sdk.dms.httpserver.HttpServerServiceImpl.java

@Override
public void onCreate() {
    super.onCreate();
    final WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    HttpServer.init(wifiManager);//  w  ww .  ja v  a 2  s.co m
    httpServer = HttpServer.getInstance();
}

From source file:com.ultrafunk.network_info.receiver.WifiStatusReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();

    //   Log.e(this.getClass().getSimpleName(), "onReceive(): " + action);

    updateWifiViews = true;/*  w ww  .j av a  2  s  .  c  o  m*/

    wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    wifiState = wifiManager.getWifiState();
    wifiInfo = wifiManager.getConnectionInfo();

    if ((wifiState == WifiManager.WIFI_STATE_ENABLED) && (wifiInfo.getIpAddress() == 0))
        detailedState = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());

    if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
        if (isConnectionReady(intent)) {
            String securityString = WifiUtils.getSecurityString(context, wifiManager, wifiInfo.getBSSID());
            NetworkStateService.setWifiSecurityString(securityString);
            detailsString = context.getString(R.string.security) + ": " + securityString;

            Intent serviceIntent = new Intent(context, NetworkStateService.class);
            serviceIntent.setAction(Constants.ACTION_WIFI_CONNECTED);
            context.startService(serviceIntent);
        }

        partiallyUpdateWidgets(context);
    } else if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)
            || Constants.ACTION_WIFI_SCANNING.equals(action)) {
        partiallyUpdateWidgets(context);
    } else if (Intent.ACTION_SCREEN_ON.equals(action) || Constants.ACTION_WIFI_LINK_SPEED.equals(action)) {
        if (isConnected()) {
            setDetailsString(context);
            partiallyUpdateWidgets(context);
        }
    } else if (Constants.ACTION_UPDATE_WIDGET.equals(action)) {
        if (isConnected())
            setDetailsString(context);

        partiallyUpdateWidget(context, AppWidgetManager.getInstance(context),
                intent.getIntExtra(Constants.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID));
    }
}

From source file:at.tugraz.ist.akm.networkInterface.WifiIpAddress.java

public WifiIpAddress(Context appContext) {
    mConnectivityManager = (ConnectivityManager) appContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    mWifiManager = (WifiManager) appContext.getSystemService(Context.WIFI_SERVICE);

}

From source file:com.tealeaf.AppFinder.java

public AppFinder(final TestAppActivity activity, Context context, final String host, final int port) {
    WifiManager wifi = (WifiManager) context.getSystemService(android.content.Context.WIFI_SERVICE);
    lock = wifi.createMulticastLock("tealeaflock");
    lock.setReferenceCounted(true);/*  w w  w  .j a  v a 2 s.  co m*/
    lock.acquire();

    handler = new Handler();
    // don't block the main thread
    // search /projects of the given host:port for manifests
    new Thread(new Runnable() {
        public void run() {
            handler.post(new Runnable() {
                public void run() {
                    activity.showAppLoadingDialog();
                }
            });

            ArrayList<AppInfo> appInfos = new ArrayList<AppInfo>();
            String url = "http://" + host + ":" + port + "/projects";
            HTTP http = new HTTP();
            logger.log("GETTING BODY", url);
            String body = http.get(URI.create(url));
            logger.log("BODY", body);

            try {
                JSONObject json = new JSONObject(body);
                JSONArray jsonNames = json.names();
                //loop through and add to list
                for (int i = 0; i < jsonNames.length(); i++) {

                    try {
                        String projectName = jsonNames.get(i).toString();
                        JSONObject curObj = json.getJSONObject(projectName);
                        boolean isExample = false;
                        //id is used for serving
                        if (!curObj.has("id"))
                            continue;
                        String id = (String) curObj.get("id");
                        if (!curObj.has("manifest"))
                            continue;
                        JSONObject manifest = curObj.getJSONObject("manifest");
                        if (!manifest.has("title"))
                            continue;
                        String title = (String) manifest.get("title");
                        if (!manifest.has("appID"))
                            continue;
                        String appid = (String) manifest.get("appID");
                        if (manifest.has("group")) {
                            String group = (String) manifest.get("group");
                            isExample = group.equals("examples");
                        }
                        //try and fail gracefully if no icons exist, use default
                        JSONObject androidObj = manifest.getJSONObject("android");
                        JSONObject icons = androidObj.has("icons") ? androidObj.getJSONObject("icons")
                                : new JSONObject();
                        JSONArray iconNames = null;
                        int iconIndex = 0;
                        if (icons.names() != null) {
                            iconNames = icons.names();
                            //hopefully get the url for the largest icon
                            int lastNum = 0;
                            for (int j = 0; j < iconNames.length(); j++) {
                                try {
                                    int num = Integer.parseInt(iconNames.get(j).toString());
                                    if (num > lastNum) {
                                        lastNum = num;
                                        iconIndex = j;
                                    }
                                } catch (Exception e) {
                                    //some number can't parse exception
                                    logger.log(e);
                                }
                            }
                        }
                        String iconURL = "default";
                        if (icons.names() != null) {
                            iconURL = (String) icons.get(iconNames.get(iconIndex).toString());
                        }
                        if (!manifest.has("supportedOrientations"))
                            continue;
                        String orientation = (String) ((JSONArray) manifest
                                .getJSONArray("supportedOrientations")).get(0);
                        boolean isPortrait = orientation.equalsIgnoreCase("portrait");
                        AppInfo app = new AppInfo(title, appid, isPortrait, id, iconURL);
                        app.isExample = isExample;
                        appInfos.add(app);
                    } catch (Exception e) {
                        logger.log(e);
                        continue;
                    }

                }
            } catch (Exception e) {
                logger.log(e);
            }
            handler.post(new Runnable() {
                public void run() {
                    activity.dismissAppLoadingDialog();
                }
            });
            Collections.sort(appInfos, new Comparator<AppInfo>() {
                @Override
                public int compare(AppInfo lhs, AppInfo rhs) {
                    //If the apps are in the same category compare by name
                    if (lhs.isExample == rhs.isExample) {
                        return lhs.name.compareTo(rhs.name);
                    }
                    //otherwise an example app is considered "greater" than
                    //a non example app and is moved to the bottom of the list
                    return lhs.isExample ? 1 : -1;
                }

            });
            createAdapter(activity, appInfos, host, port);
        }
    }).start();
}

From source file:com.ilearnrw.reader.tasks.AddToLibraryTask.java

public void run(String... params) {

    wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL, "wifiLock");
    wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "wakeLock");

    wifiLock.acquire();/*  ww w. j  av a2  s  . c  o m*/
    wakeLock.acquire();
    this.execute(params);
}

From source file:am.project.x.utils.ContextUtils.java

/**
 * WIFI?/*w  w  w .ja  v a2 s.  co m*/
 *
 * @param context Context
 * @return true: false:
 */
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
public static boolean isWifiConnected(Context context) {
    final WifiManager manager = (WifiManager) context.getApplicationContext()
            .getSystemService(Context.WIFI_SERVICE);
    if (manager == null)
        return false;
    final DhcpInfo info = manager.getDhcpInfo();
    return info != null && info.ipAddress != 0;
}