Example usage for android.net.wifi WifiManager getWifiState

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

Introduction

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

Prototype

public int getWifiState() 

Source Link

Document

Gets the Wi-Fi enabled state.

Usage

From source file:Main.java

public static boolean isWifiEnabled(Context mContext) {
    WifiManager wifi_manager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
    if (wifi_manager != null)
        return (WifiManager.WIFI_STATE_ENABLED == wifi_manager.getWifiState())
                || (WifiManager.WIFI_STATE_ENABLING == wifi_manager.getWifiState());
    return false;
}

From source file:org.openremote.android.console.net.ORNetworkCheck.java

/**
 * Detects the current WiFi status.//ww w . ja  v a 2s .  c  o  m
 *
 * @param ctx     global Android application context
 *
 * @return true if WiFi is available, false otherwise
 */
private static boolean canReachWifiNetwork(Context ctx) {

    WifiManager wifiManager = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
    ConnectivityManager connectivityManager = (ConnectivityManager) ctx
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    if (!wifiManager.isWifiEnabled() || wifiManager.getWifiState() != WifiManager.WIFI_STATE_ENABLED) {
        Log.i(LOG_CATEGORY, "WiFi not enabled or WiFi network not detected.");

        return false;
    }

    NetworkInfo wifiNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (!wifiNetworkInfo.isAvailable()) {
        Log.i(LOG_CATEGORY, "Wifi network detected but wasn't available.");

        return false;
    }

    else {
        return true;
    }
}

From source file:org.deviceconnect.android.deviceplugin.hvcc2w.setting.fragment.HVCC2WPairingFragment.java

/**
 * Check Wifi.//from w w w  .j a  va  2  s  .  co m
 */
private void searchWifi() {
    final WifiManager wifiManager = getWifiManager();
    if (wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLED) {
        mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                List<ScanResult> results = wifiManager.getScanResults();
                if (results.size() == 0) {
                    return;
                }
                mSSID.setText(results.get(0).SSID);
            }
        };
        getActivity().registerReceiver(mReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    }
    wifiManager.startScan();
}

From source file:com.sdsoft.motoactv_export.WebActivity.java

public boolean promptforWifi() {
    if (!isOnline(false)) {
        WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        if (wifiManager.getWifiState() != 3) {
            new AlertDialog.Builder(this).setTitle("Enable WiFi.")
                    .setMessage("Internet connection needed, would you like to enable WiFi?")
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int whichButton) {
                            WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                            wifiManager.setWifiEnabled(true);
                            //UpdateUtil.EnableWifi(getBaseContext());
                        }/*  ww  w . j  av a 2  s . com*/
                    }).setNegativeButton(android.R.string.no, null).show();
        }
    }
    return true;
}

From source file:net.micode.fileexplorer.ServerControlActivity.java

/**
 * This will be called by the static UiUpdater whenever the service has
 * changed state in a way that requires us to update our UI. We can't use
 * any myLog.l() calls in this function, because that will trigger an
 * endless loop of UI updates.//from   www.j  a  v a2  s  .c  o  m
 */
public void updateUi() {
    myLog.l(Log.DEBUG, "Updating UI", true);

    WifiManager wifiMgr = (WifiManager) mActivity.getSystemService(Context.WIFI_SERVICE);
    int wifiState = wifiMgr.getWifiState();
    WifiInfo info = wifiMgr.getConnectionInfo();
    String wifiId = info != null ? info.getSSID() : null;
    boolean isWifiReady = FTPServerService.isWifiEnabled();

    setText(R.id.wifi_state, isWifiReady ? wifiId : getString(R.string.no_wifi_hint));
    ImageView wifiImg = (ImageView) mRootView.findViewById(R.id.wifi_state_image);
    wifiImg.setImageResource(isWifiReady ? R.drawable.wifi_state4 : R.drawable.wifi_state0);

    boolean running = FTPServerService.isRunning();
    if (running) {
        myLog.l(Log.DEBUG, "updateUi: server is running", true);
        // Put correct text in start/stop button
        // Fill in wifi status and address
        InetAddress address = FTPServerService.getWifiIp();
        if (address != null) {
            String port = ":" + FTPServerService.getPort();
            ipText.setText(
                    "ftp://" + address.getHostAddress() + (FTPServerService.getPort() == 21 ? "" : port));

        } else {
            // could not get IP address, stop the service
            Context context = mActivity.getApplicationContext();
            Intent intent = new Intent(context, FTPServerService.class);
            context.stopService(intent);
            ipText.setText("");
        }
    }

    startStopButton.setEnabled(isWifiReady);
    TextView startStopButtonText = (TextView) mRootView.findViewById(R.id.start_stop_button_text);
    if (isWifiReady) {
        startStopButtonText.setText(running ? R.string.stop_server : R.string.start_server);
        startStopButtonText.setCompoundDrawablesWithIntrinsicBounds(
                running ? R.drawable.disconnect : R.drawable.connect, 0, 0, 0);
        startStopButtonText.setTextColor(running ? getResources().getColor(R.color.remote_disconnect_text)
                : getResources().getColor(R.color.remote_connect_text));
    } else {
        if (FTPServerService.isRunning()) {
            Context context = mActivity.getApplicationContext();
            Intent intent = new Intent(context, FTPServerService.class);
            context.stopService(intent);
        }

        startStopButtonText.setText(R.string.no_wifi);
        startStopButtonText.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        startStopButtonText.setTextColor(Color.GRAY);
    }

    ipText.setVisibility(running ? View.VISIBLE : View.INVISIBLE);
    instructionText.setVisibility(running ? View.VISIBLE : View.GONE);
    instructionTextPre.setVisibility(running ? View.GONE : View.VISIBLE);
}

From source file:riddimon.android.asianetautologin.HttpUtils.java

/**
 * Uses TelephonyManager and WifiManager to check for network connectivity.
 * Also incorporates CONNECTING state for retry scenarios.
 * @param context//from   ww w  . j  a v  a  2  s .  com
 * @return ConnectionStatus
 */
public ConnectionStatus isConnectedOLD(Context context) {
    boolean data = false, wifi = false;
    boolean data_connecting = false, wifi_connecting = false;
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    int ds = tm.getDataState();
    int ws = wm.getWifiState();
    switch (ds) {
    case TelephonyManager.DATA_CONNECTED:
        data = true;
        break;
    case TelephonyManager.DATA_CONNECTING:
        data_connecting = true;
    default:
        data = false;
        data_connecting = false;
    }

    switch (ws) {
    case WifiManager.WIFI_STATE_ENABLING:
        wifi_connecting = true;
    case WifiManager.WIFI_STATE_DISABLING:
    case WifiManager.WIFI_STATE_DISABLED:
    case WifiManager.WIFI_STATE_UNKNOWN:
        wifi = false;
        break;
    case WifiManager.WIFI_STATE_ENABLED:
        WifiInfo wi = wm.getConnectionInfo();
        if (wi != null)
            wifi = true;
        break;
    }

    if (wifi && data)
        return ConnectionStatus.BOTH_CONNECTED;
    else if (wifi && data_connecting)
        return ConnectionStatus.WIFI_CONNECTED;
    else if (data && wifi_connecting)
        return ConnectionStatus.DATA_CONNECTED;
    else if (wifi_connecting || data_connecting)
        return ConnectionStatus.CONNECTING;
    return ConnectionStatus.NO_CONNECTION;
}

From source file:org.geometerplus.fbreader.plugin.local_opds_scanner.ScanLocalNetworkActivity.java

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    setContentView(R.layout.scan_local_network);

    setListAdapter(new ItemAdapter());

    setTitle(R.string.scan_local_network_window_title);

    final View buttonView = findViewById(R.id.scan_local_network_buttons);

    final Button cancelButton = (Button) buttonView.findViewById(R.id.cancel_button);
    cancelButton.setText(R.string.button_cancel);
    cancelButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            finish();/*from  w  ww. ja  v  a2 s  .  c  o  m*/
        }
    });

    final WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    final int state = wifiManager.getWifiState();
    if (state != WifiManager.WIFI_STATE_ENABLED && state != WifiManager.WIFI_STATE_ENABLING) {
        setTitle(R.string.wifi_is_turned_off);
        final View listView = findViewById(android.R.id.list);
        final TextView errorView = (TextView) findViewById(R.id.scan_local_network_error);
        listView.setVisibility(View.GONE);
        errorView.setVisibility(View.VISIBLE);
        errorView.setText(R.string.turn_wifi_on);

        final Button turnOnButton = (Button) buttonView.findViewById(R.id.ok_button);
        turnOnButton.setText(R.string.button_turn_on);
        turnOnButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                wifiManager.setWifiEnabled(true);
                finish();
            }
        });

        myLock = null;
    } else {
        final Button rescanButton = (Button) buttonView.findViewById(R.id.ok_button);
        rescanButton.setText(R.string.button_rescan);
        rescanButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                runOnUiThread(new Runnable() {
                    public void run() {
                        clear();
                        scan();
                    }
                });
            }
        });

        myLock = wifiManager.createMulticastLock("FBReader_lock");
        myLock.setReferenceCounted(true);
        myLock.acquire();

        scan();
    }
}

From source file:com.appnexus.opensdkapp.MainActivity.java

/**
 * Upload log file to server every 24 hours
 */// w w  w  .j  a v a2 s  .  co m

private void checkToUploadLogFile() {
    long lastLogUploadTime = Prefs.getLastLogUpload(getBaseContext());
    long currentTime = System.currentTimeMillis();
    long oneDayInMillis = 86400000;
    if (currentTime - lastLogUploadTime > oneDayInMillis) {
        Clog.d(Constants.BASE_LOG_TAG, "Last log upload was more than a day ago. Check wifi");

        WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        if (wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLED) {
            Clog.d(Constants.BASE_LOG_TAG, "Wifi is available. Upload log file.");

            //TODO: implement file upload

            boolean fileUploadSuccessful = false;

            if (fileUploadSuccessful) {
                Prefs prefs = new Prefs(getBaseContext());
                prefs.writeLong(Prefs.KEY_LAST_LOG_UPLOAD, System.currentTimeMillis());
                prefs.applyChanges();

                clearLogFile();
            }
        }
    }
}

From source file:com.sonymobile.dronecontrol.activity.MainActivity.java

private boolean verifyWifi() {
    boolean result = false;
    WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    if (wifiManager.getWifiState() == WifiManager.WIFI_STATE_DISABLED) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("WiFi").setMessage(R.string.DG_CONNECT_TO_WIFI)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override/* w w  w.j a va 2  s.c  om*/
                    public void onClick(DialogInterface dialog, int which) {
                        startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
                        dialog.dismiss();
                    }
                }).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();
    } else {
        result = true;
    }
    return result;
}

From source file:com.sentaroh.android.TaskAutomation.TaskExecutor.java

final static public void setWifiOff(TaskManagerParms taskMgrParms, EnvironmentParms envParms,
        CommonUtilities util, TaskResponse task_response, ActionResponse ar) {
    WifiManager wm = (WifiManager) taskMgrParms.context.getSystemService(Context.WIFI_SERVICE);
    int ws = -1;/*from   ww  w  .  ja va  2 s. co  m*/
    if (wm != null)
        ws = wm.getWifiState();
    if (envParms.settingDebugLevel >= 1)
        util.addDebugMsg(1, "I",
                "setWifiOff WIFI Off entered, wifiIsActive=" + envParms.wifiIsActive + ", wifiMgrStatus=" + ws);
    if (envParms.wifiIsActive) {
        if (wm != null) {
            wm.setWifiEnabled(false);
            ar.action_resp = ActionResponse.ACTION_SUCCESS;
            ar.resp_msg_text = "setWifiOff WIFI Off Success";
        } else {
            if (envParms.settingDebugLevel >= 1)
                util.addDebugMsg(1, "I", "setWifiOff WifiManager not available, WIFI Off ignored");
            ar.action_resp = ActionResponse.ACTION_WARNING;
            ar.resp_msg_text = "setWifiOff WIFI not available";
        }
    } else {
        ar.action_resp = ActionResponse.ACTION_WARNING;
        ar.resp_msg_text = "setWifiOff WIFI already off";
    }
}