Example usage for android.net.wifi WifiInfo getSupplicantState

List of usage examples for android.net.wifi WifiInfo getSupplicantState

Introduction

In this page you can find the example usage for android.net.wifi WifiInfo getSupplicantState.

Prototype

public SupplicantState getSupplicantState() 

Source Link

Document

Return the detailed state of the supplicant's negotiation with an access point, in the form of a SupplicantState SupplicantState object.

Usage

From source file:Main.java

/**
 * Checks if is wifi.//from   w ww.j a v a  2s  . co  m
 * 
 * @param ctx
 *            the ctx
 * @return true, if is wifi
 */
public static boolean isWifi(final Context ctx) {
    WifiManager wm = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wi = wm.getConnectionInfo();
    if (wi != null && (WifiInfo.getDetailedStateOf(wi.getSupplicantState()) == DetailedState.OBTAINING_IPADDR
            || WifiInfo.getDetailedStateOf(wi.getSupplicantState()) == DetailedState.CONNECTED)) {
        return true;
    }
    return false;
}

From source file:com.wiyun.engine.network.Network.java

static boolean isWifiConnected() {
    Context context = Director.getInstance().getContext();
    if (context.checkCallingOrSelfPermission(
            android.Manifest.permission.ACCESS_WIFI_STATE) == PackageManager.PERMISSION_GRANTED) {
        WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (wm != null) {
            WifiInfo info = wm.getConnectionInfo();
            return info != null && info.getSupplicantState() == SupplicantState.COMPLETED;
        } else {//  w  w  w.j  av a  2 s .  c om
            return false;
        }
    } else {
        Log.w("libwiengine", "you need add ACCESS_WIFI_STATE permission");
        return false;
    }
}

From source file:ca.frozen.curlingtv.classes.Utils.java

public static String getWifiName() {
    String ssid = "";
    WifiManager manager = (WifiManager) App.getContext().getSystemService(Context.WIFI_SERVICE);
    if (manager.isWifiEnabled()) {
        WifiInfo wifiInfo = manager.getConnectionInfo();
        if (wifiInfo != null) {
            NetworkInfo.DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());
            if (state == NetworkInfo.DetailedState.CONNECTED
                    || state == NetworkInfo.DetailedState.OBTAINING_IPADDR) {
                ssid = wifiInfo.getSSID();
                if (ssid == null)
                    ssid = "";
                ssid = ssid.replaceAll("^\"|\"$", "");
            }/*from  ww  w  .  ja  va  2 s.com*/
        }
    }
    return ssid;
}

From source file:com.googlecode.android_scripting.jsonrpc.JsonBuilder.java

private static JSONObject buildJsonWifiInfo(WifiInfo data) throws JSONException {
    JSONObject result = new JSONObject();
    result.put("hidden_ssid", data.getHiddenSSID());
    result.put("ip_address", data.getIpAddress());
    result.put("link_speed", data.getLinkSpeed());
    result.put("network_id", data.getNetworkId());
    result.put("rssi", data.getRssi());
    result.put("bssid", data.getBSSID());
    result.put("mac_address", data.getMacAddress());
    result.put("ssid", data.getSSID());
    String supplicantState = "";
    switch (data.getSupplicantState()) {
    case ASSOCIATED:
        supplicantState = "associated";
        break;//from   w w w .  j a v a2  s . c  o m
    case ASSOCIATING:
        supplicantState = "associating";
        break;
    case COMPLETED:
        supplicantState = "completed";
        break;
    case DISCONNECTED:
        supplicantState = "disconnected";
        break;
    case DORMANT:
        supplicantState = "dormant";
        break;
    case FOUR_WAY_HANDSHAKE:
        supplicantState = "four_way_handshake";
        break;
    case GROUP_HANDSHAKE:
        supplicantState = "group_handshake";
        break;
    case INACTIVE:
        supplicantState = "inactive";
        break;
    case INVALID:
        supplicantState = "invalid";
        break;
    case SCANNING:
        supplicantState = "scanning";
        break;
    case UNINITIALIZED:
        supplicantState = "uninitialized";
        break;
    default:
        supplicantState = null;
    }
    result.put("supplicant_state", build(supplicantState));
    return result;
}

From source file:org.peterbaldwin.vlcremote.app.PickServerActivity.java

private WifiInfo getConnectionInfo() {
    Object service = getSystemService(WIFI_SERVICE);
    WifiManager manager = (WifiManager) service;
    WifiInfo info = manager.getConnectionInfo();
    if (info != null) {
        SupplicantState state = info.getSupplicantState();
        if (state.equals(SupplicantState.COMPLETED)) {
            return info;
        }//from   w  w  w .  j  a  v a 2s. c o m
    }
    return null;
}

From source file:nacho.tfg.blepresencetracker.MyFirebaseInstanceIDService.java

/**
 * Persist token to third-party servers.
 *
 * Modify this method to associate the user's FCM InstanceID token with any server-side account
 * maintained by your application./*from   w w  w  .j  a  v  a 2 s. co  m*/
 *
 * @param token The new token.
 */
private void sendRegistrationToServer(String token) {
    // Add custom implementation, as needed.
    Intent intent = new Intent(ACTION_SHOW_TOKEN);
    intent.putExtra("token", token);
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

    AsyncTask<String, Void, Boolean> asyncTask = new AsyncTask<String, Void, Boolean>() {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected Boolean doInBackground(String... params) {
            String data = params[0];
            String ip = "192.168.4.1";
            int port = 7777;
            try {
                Socket socket = new Socket(ip, port);
                while (!socket.isConnected()) {

                }

                OutputStream out = socket.getOutputStream();
                PrintWriter output = new PrintWriter(out);

                output.println(data);
                output.flush();
                out.close();
                output.close();

                socket.close();
            } catch (SocketException e) {
                e.printStackTrace();
                return false;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            if (result) {
                SharedPreferences settings = PreferenceManager
                        .getDefaultSharedPreferences(MyFirebaseInstanceIDService.this);
                SharedPreferences.Editor editor = settings.edit();
                editor.putBoolean(getString(R.string.registration_id_sent), true);
                editor.commit();
            } else {
                SharedPreferences settings = PreferenceManager
                        .getDefaultSharedPreferences(MyFirebaseInstanceIDService.this);
                SharedPreferences.Editor editor = settings.edit();
                editor.putBoolean(getString(R.string.registration_id_sent), false);
                editor.commit();
            }
        }
    };

    // Change wifi network to "NodeMCU WiFi"

    String ssid = "";
    WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    if (WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState()) == NetworkInfo.DetailedState.CONNECTED) {
        ssid = wifiInfo.getSSID();
    }

    if (!ssid.equals("NodeMCU WiFi")) {
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), getString(R.string.toast_connect_NodeMCU),
                        Toast.LENGTH_LONG).show();

            }
        });

    }
}

From source file:com.ultrafunk.network_info.service.NetworkStateService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if ((intent != null) && (intent.getAction() != null)) {
        final String action = intent.getAction();

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

        if (Constants.ACTION_UPDATE_SERVICE_STATE.equals(action)) {
            initEnabledWidgets(new EnabledWidgets(
                    intent.getBooleanExtra(Constants.EXTRA_ENABLED_WIDGETS_MOBILE_DATA, false),
                    intent.getBooleanExtra(Constants.EXTRA_ENABLED_WIDGETS_WIFI, false)));
        } else {/*from  w w w  . j  av a  2 s  .com*/
            final Handler handler = new Handler();

            if (Constants.ACTION_WIFI_CONNECTING.equals(action)) {
                handler.postDelayed(new Runnable() {
                    public void run() {
                        WifiInfo wifiInfo = wifiManager.getConnectionInfo();

                        if ((wifiInfo != null) && (wifiInfo.getSupplicantState() == SupplicantState.SCANNING))
                            localBroadcastManager.sendBroadcastSync(new Intent(Constants.ACTION_WIFI_SCANNING));
                    }
                }, 5 * 1000);
            } else if (Constants.ACTION_WIFI_CONNECTED.equals(action)) {
                handler.postDelayed(new Runnable() {
                    public void run() {
                        WifiInfo wifiInfo = wifiManager.getConnectionInfo();

                        if ((wifiInfo != null) && (wifiInfo.getLinkSpeed() != -1))
                            localBroadcastManager
                                    .sendBroadcastSync(new Intent(Constants.ACTION_WIFI_LINK_SPEED));
                    }
                }, 3 * 1000);
            } else if (Constants.ACTION_DATA_CONNECTED.equals(action)) {
                handler.postDelayed(new Runnable() {
                    public void run() {
                        isWaitingForDataUsage = false;

                        if (telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED)
                            localBroadcastManager
                                    .sendBroadcastSync(new Intent(Constants.ACTION_DATA_USAGE_UPDATE));
                    }
                }, 500);
            }
        }
    }

    return Service.START_STICKY;
}

From source file:org.gdg.frisbee.android.widget.FeedbackFragment.java

private void buildProperties() {
    addProperty(PROPERTY_MODEL, Build.MODEL);
    addProperty(PROPERTY_ANDROID_VERSION, VERSION.RELEASE);

    try {//from   www .  j a  v  a2 s  .c o  m
        WifiManager cm = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
        WifiInfo e = cm.getConnectionInfo();
        SupplicantState mobileDataEnabled = e.getSupplicantState();
        addProperty(PROPERTY_WI_FI_ENABLED, mobileDataEnabled);
    } catch (Exception e) {
        Timber.d(e, "Wifi Manager problem.");
    }

    boolean mobileDataEnabled1 = false;
    ConnectivityManager cm1 = (ConnectivityManager) getActivity()
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    try {
        Class e1 = Class.forName(cm1.getClass().getName());
        Method resolution = e1.getDeclaredMethod("getMobileDataEnabled");
        resolution.setAccessible(true);
        mobileDataEnabled1 = (Boolean) resolution.invoke(cm1);
    } catch (Exception e) {
        Timber.d(e, "Mobil data problem.");
    }

    addProperty(PROPERTY_MOBILE_DATA_ENABLED, mobileDataEnabled1);

    try {
        LocationManager e2 = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
        boolean resolution1 = e2.isProviderEnabled("gps");
        addProperty(PROPERTY_GPS_ENABLED, resolution1);
    } catch (Exception e) {
        Timber.d(e, "GPS problem.");
    }

    try {
        DisplayMetrics e3 = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(e3);
        String resolution2 = Integer.toString(e3.widthPixels) + "x" + Integer.toString(e3.heightPixels);
        addProperty(PROPERTY_SCREEN_RESOLUTION, resolution2);
    } catch (Exception e) {
        Timber.d(e, "Screen density problem.");
    }

    try {
        String e4 = getActivity().getClass().getSimpleName();
        addProperty(PROPERTY_ACTIVITY, e4);
    } catch (Exception e) {
        Timber.d(e, "Activity name problem.");
    }

    PackageManager manager = getActivity().getPackageManager();
    try {
        PackageInfo e = manager.getPackageInfo(getActivity().getPackageName(), 0);
        addProperty(PROPERTY_APP_VERSION_NAME, e.versionName);
        addProperty(PROPERTY_APP_VERSION_CODE, e.versionCode);
    } catch (PackageManager.NameNotFoundException var7) {
        Timber.d("Problem with PackageManager");
    }
}

From source file:org.sipdroid.sipua.ui.Receiver.java

public static boolean isFast(int i) {
    WifiManager wm = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wi = wm.getConnectionInfo();

    if (PreferenceManager.getDefaultSharedPreferences(mContext)
            .getString(org.sipdroid.sipua.ui.Settings.PREF_USERNAME + (i != 0 ? i : ""), "").equals("")
            || PreferenceManager.getDefaultSharedPreferences(mContext)
                    .getString(org.sipdroid.sipua.ui.Settings.PREF_SERVER + (i != 0 ? i : ""), "").equals(""))
        return false;
    if (wi != null) {
        if (!Sipdroid.release)
            Log.i("SipUA:", "isFastWifi() " + WifiInfo.getDetailedStateOf(wi.getSupplicantState()) + " "
                    + wi.getIpAddress());
        if (wi.getIpAddress() != 0
                && (WifiInfo.getDetailedStateOf(wi.getSupplicantState()) == DetailedState.OBTAINING_IPADDR
                        || WifiInfo.getDetailedStateOf(wi.getSupplicantState()) == DetailedState.CONNECTED)
                || WifiInfo.getDetailedStateOf(wi.getSupplicantState()) == DetailedState.CONNECTING) {
            on_wlan = true;/* www  . ja  va  2 s  .  c om*/
            if (!on_vpn())
                return PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(
                        org.sipdroid.sipua.ui.Settings.PREF_WLAN + (i != 0 ? i : ""),
                        org.sipdroid.sipua.ui.Settings.DEFAULT_WLAN);
            else
                return PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean(
                        org.sipdroid.sipua.ui.Settings.PREF_VPN + (i != 0 ? i : ""),
                        org.sipdroid.sipua.ui.Settings.DEFAULT_VPN);
        }
    }
    on_wlan = false;
    return isFastGSM(i);
}

From source file:tv.matchstick.demo.flingpic.FlingPicActivity.java

private void startWetServer(int port) {
    try {/* w w w. j  a  v  a 2s.  c  o m*/
        WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();

        mIpAddress = intToIp(wifiInfo.getIpAddress());

        if (wifiInfo.getSupplicantState() != SupplicantState.COMPLETED) {
            new AlertDialog.Builder(this).setTitle("Error")
                    .setMessage("Please connect to a WIFI-network for starting the webserver.")
                    .setPositiveButton("OK", null).show();
            throw new Exception("Please connect to a WIFI-network.");
        }

        Log.e(TAG, "Starting server " + mIpAddress + ":" + port + ".");

        List<File> rootDirs = new ArrayList<File>();
        boolean quiet = false;
        Map<String, String> options = new HashMap<String, String>();
        rootDirs.add(new File(mRootDir).getAbsoluteFile());

        // mNanoHTTPD
        try {
            mNanoHTTPD = new SimpleWebServer(mIpAddress, port, rootDirs, quiet);
            mNanoHTTPD.start();
        } catch (IOException ioe) {
            Log.e(TAG, "Couldn't start server:\n" + ioe);
        }
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    }
}