Java Code Examples for android.net.wifi.WifiManager#getWifiState()

The following examples show how to use android.net.wifi.WifiManager#getWifiState() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: S_MVP.java    From styT with Apache License 2.0 6 votes vote down vote up
public static boolean isWifiEnabled() {
    Context myContext = Globals.getContext();
    if (myContext == null) {
        throw new NullPointerException("Global context is null");
    }
    WifiManager wifiMgr = (WifiManager) myContext.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    if (wifiMgr.getWifiState() == WifiManager.WIFI_STATE_ENABLED) {
        ConnectivityManager connManager = (ConnectivityManager) myContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifiInfo = connManager
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        return wifiInfo.isConnected();
    } else {
        return false;
    }
}
 
Example 2
Source File: MainActivity.java    From RairDemo with Apache License 2.0 6 votes vote down vote up
@SuppressLint("WifiManagerLeak")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // 获取WiFi管理者对象
    mWiFiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);

    if (mWiFiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLED) {
        Toast.makeText(this, "WiFi可用", Toast.LENGTH_SHORT).show();
    }

    listView = (ListView) findViewById(R.id.listView);
    myAdapter = new MyAdapter(this, list, mWiFiManager);
    listView.setAdapter(myAdapter);

    // 创建wifi锁
    wifiLock = mWiFiManager.createWifiLock("wifiLock");

    if (wifiLock != null) {
        // 使wifi锁有效
        wifiLock.acquire();
    }

}
 
Example 3
Source File: S_MVP.java    From stynico with MIT License 6 votes vote down vote up
public static boolean isWifiEnabled() {
    Context myContext = Globals.getContext();
    if (myContext == null) {
        throw new NullPointerException("Global context is null");
    }
    WifiManager wifiMgr = (WifiManager) myContext.getSystemService(Context.WIFI_SERVICE);
    if (wifiMgr.getWifiState() == WifiManager.WIFI_STATE_ENABLED) {
        ConnectivityManager connManager = (ConnectivityManager) myContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifiInfo = connManager
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        return wifiInfo.isConnected();
    } else {
        return false;
    }
}
 
Example 4
Source File: Wifi.java    From batteryhub with Apache License 2.0 6 votes vote down vote up
/**
 * Get Wifi state.
 *
 * @param context The Context
 * @return Wifi state constant, an int value
 */
public static String getState(Context context) {
    WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    int wifiState = manager.getWifiState();

    switch (wifiState) {
        case WifiManager.WIFI_STATE_DISABLED:
            return WIFI_STATE_DISABLED;
        case WifiManager.WIFI_STATE_DISABLING:
            return WIFI_STATE_DISABLING;
        case WifiManager.WIFI_STATE_ENABLED:
            return WIFI_STATE_ENABLED;
        case WifiManager.WIFI_STATE_ENABLING:
            return WIFI_STATE_ENABLING;
        case WifiManager.WIFI_STATE_UNKNOWN:
            return WIFI_STATE_UNKNOWN;
        default:
            return WIFI_STATE_UNKNOWN;
    }
}
 
Example 5
Source File: NetHelper.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
/**
 * 获取mac
 */
public static String getMacString(Context context) {
    String mac = null;
    WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    boolean bOpenWifi = false;
    int state = wifiManager.getWifiState();
    if (state != WifiManager.WIFI_STATE_ENABLED && state != WifiManager.WIFI_STATE_ENABLING) {
        bOpenWifi = wifiManager.setWifiEnabled(true);

    }
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    if (wifiInfo != null) {
        mac = wifiInfo.getMacAddress();
    }
    if (bOpenWifi) {
        wifiManager.setWifiEnabled(false);
    }
    return mac == null ? "" : mac.replace(":", "");
}
 
Example 6
Source File: NetworkUtil.java    From mirror with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("ResourceType")
public static boolean isNetworkAvailable(final Context context) {
    final WifiManager manager = (WifiManager) context.getSystemService(WIFI_SERVICE);
    if (manager != null) {
        if (manager.isWifiEnabled()) {
            return isNetworkConnected(context);
        } else {
            if (manager.getWifiState() == WIFI_STATE_DISABLED) {
                manager.setWifiEnabled(true);
            }
        }
    }
    return false;
}
 
Example 7
Source File: WifiBaseActivity.java    From android-wifi-activity with Creative Commons Zero v1.0 Universal 5 votes vote down vote up
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_ENABLE_WIFI && resultCode == 0) {
        WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
        if (wifi.isWifiEnabled() || wifi.getWifiState() == WifiManager.WIFI_STATE_ENABLING) {
            connectToSpecificNetwork();
        } else {
            finish();
        }
    } else {
        finish();
    }
}
 
Example 8
Source File: WifiBase.java    From android-wifi-activity with Creative Commons Zero v1.0 Universal 5 votes vote down vote up
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_ENABLE_WIFI && resultCode == 0) {
        WifiManager wifi = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
        if (wifi.isWifiEnabled() || wifi.getWifiState() == WifiManager.WIFI_STATE_ENABLING) {
            connectToSpecificNetwork();
        } else {
            getActivity().finish();
        }
    } else {
        getActivity().finish();
    }
}
 
Example 9
Source File: NetworkHelper.java    From MiBandDecompiled with Apache License 2.0 5 votes vote down vote up
public static int getWifiState(Context context)
{
    WifiManager wifimanager = (WifiManager)context.getSystemService("wifi");
    if (wifimanager == null)
    {
        return 4;
    } else
    {
        return wifimanager.getWifiState();
    }
}
 
Example 10
Source File: NetWorkUtils.java    From GankGirl with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * 获取Wifi的状态,需要ACCESS_WIFI_STATE权限
 *
 * @param context 上下文
 * @return 取值为WifiManager中的WIFI_STATE_ENABLED、WIFI_STATE_ENABLING、WIFI_STATE_DISABLED、WIFI_STATE_DISABLING、WIFI_STATE_UNKNOWN之一
 * @throws Exception 没有找到wifi设备
 */
public static int getWifiState(Context context) throws Exception {
    WifiManager wifiManager = ((WifiManager) context.getSystemService(
            Context.WIFI_SERVICE));
    if (wifiManager != null) {
        return wifiManager.getWifiState();
    }
    else {
        throw new Exception("wifi device not found!");
    }
}
 
Example 11
Source File: NetworkUtils.java    From gank with GNU General Public License v3.0 5 votes vote down vote up
public static int getWifiState(Context context) throws Exception {
    WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
    if(wifiManager != null) {
        return wifiManager.getWifiState();
    } else {
        throw new Exception("wifi device not found!");
    }
}
 
Example 12
Source File: InputActivity.java    From AndroidAnimationExercise with Apache License 2.0 5 votes vote down vote up
private static String getAddressMacByFile(WifiManager wifiMan) throws Exception {
    String ret;
    int wifiState = wifiMan.getWifiState();

    wifiMan.setWifiEnabled(true);
    File fl = new File(fileAddressMac);
    FileInputStream fin = new FileInputStream(fl);
    ret = crunchifyGetStringFromStream(fin);
    fin.close();

    boolean enabled = WifiManager.WIFI_STATE_ENABLED == wifiState;
    wifiMan.setWifiEnabled(enabled);
    return ret;
}
 
Example 13
Source File: DeviceUtils.java    From TvPlayer with Apache License 2.0 5 votes vote down vote up
private static String getAddressMacByFile(WifiManager wifiMan) throws Exception {
    String ret;
    int wifiState = wifiMan.getWifiState();

    wifiMan.setWifiEnabled(true);
    File fl = new File(fileAddressMac);
    FileInputStream fin = new FileInputStream(fl);
    ret = crunchifyGetStringFromStream(fin);
    fin.close();

    boolean enabled = WifiManager.WIFI_STATE_ENABLED == wifiState;
    wifiMan.setWifiEnabled(enabled);
    return ret;
}
 
Example 14
Source File: WifiStateChangeService.java    From fdroidclient with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onHandleIntent(Intent intent) {
    android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_LOWEST);
    if (intent == null) {
        Utils.debugLog(TAG, "received null Intent, ignoring");
        return;
    }
    Utils.debugLog(TAG, "WiFi change service started.");
    NetworkInfo ni = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
    wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
    wifiState = wifiManager.getWifiState();
    Utils.debugLog(TAG, "ni == " + ni + "  wifiState == " + printWifiState(wifiState));
    if (ni == null
            || ni.getState() == NetworkInfo.State.CONNECTED || ni.getState() == NetworkInfo.State.DISCONNECTED) {
        if (previousWifiState != wifiState &&
                (wifiState == WifiManager.WIFI_STATE_ENABLED
                        || wifiState == WifiManager.WIFI_STATE_DISABLING  // might be switching to hotspot
                        || wifiState == WifiManager.WIFI_STATE_DISABLED   // might be hotspot
                        || wifiState == WifiManager.WIFI_STATE_UNKNOWN)) { // might be hotspot
            if (wifiInfoThread != null) {
                wifiInfoThread.interrupt();
            }
            wifiInfoThread = new WifiInfoThread();
            wifiInfoThread.start();
        }

        if (Build.VERSION.SDK_INT < 21 && wifiState == WifiManager.WIFI_STATE_ENABLED) {
            UpdateService.scheduleIfStillOnWifi(this);
        }
    }
}
 
Example 15
Source File: NetworkHelper.java    From letv with Apache License 2.0 5 votes vote down vote up
public static int getWifiState(Context context) {
    WifiManager wifi = (WifiManager) context.getSystemService("wifi");
    if (wifi == null) {
        return 4;
    }
    return wifi.getWifiState();
}
 
Example 16
Source File: WifiStateChangeReceiver.java    From Smartlab with Apache License 2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {

	abortBroadcast();
	
	WifiManager wifiManager = (WifiManager) context
			.getSystemService(Context.WIFI_SERVICE);
	switch (wifiManager.getWifiState()) {
	case WifiManager.WIFI_STATE_DISABLED:
		Toast.makeText(context, "WIFI_STATE_DISABLED", Toast.LENGTH_LONG)
				.show();
		break;
	case WifiManager.WIFI_STATE_ENABLED:
		Toast.makeText(context, "WIFI_STATE_ENABLED", Toast.LENGTH_LONG)
				.show();
		break;
	case WifiManager.WIFI_STATE_DISABLING:
		Toast.makeText(context, "WIFI_STATE_DISABLING", Toast.LENGTH_LONG)
				.show();
		break;
	case WifiManager.WIFI_STATE_ENABLING:
		Toast.makeText(context, "WIFI_STATE_ENABLING", Toast.LENGTH_LONG)
				.show();
		break;
	case WifiManager.WIFI_STATE_UNKNOWN:
		Toast.makeText(context, "WIFI_STATE_UNKNOWN", Toast.LENGTH_LONG)
				.show();
		break;
	}
}
 
Example 17
Source File: AppNetworkMgr.java    From AndroidWallet with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 获取Wifi的状态,需要ACCESS_WIFI_STATE权限
 *
 * @param context 上下文
 * @return 取值为WifiManager中的WIFI_STATE_ENABLED、WIFI_STATE_ENABLING、WIFI_STATE_DISABLED、WIFI_STATE_DISABLING、WIFI_STATE_UNKNOWN之一
 * @throws Exception 没有找到wifi设备
 */
public static int getWifiState(Context context) throws Exception {
    WifiManager wifiManager = ((WifiManager) context.getSystemService(
            Context.WIFI_SERVICE));
    if (wifiManager != null) {
        return wifiManager.getWifiState();
    } else {
        throw new Exception("wifi device not found!");
    }
}
 
Example 18
Source File: DataStorage.java    From PacketSender-Android with MIT License 4 votes vote down vote up
public static String getIP(Context ctx)
{

    WifiManager wifi = (WifiManager) ctx.getSystemService(ctx.WIFI_SERVICE);

    if(wifi.getWifiState() != WifiManager.WIFI_STATE_ENABLED )
    {
        return "Wifi Disabled";

    } else {

        WifiInfo info = wifi.getConnectionInfo();
        int ipAddressInt = info.getIpAddress();

        //convert it to normal dot notation
        String ipBulder = ""+ (((ipAddressInt >>> 24) & 0xFF));
        ipBulder = (((ipAddressInt >>> 16) & 0xFF)) + "." + ipBulder;
        ipBulder = (((ipAddressInt >>> 8) & 0xFF)) + "." + ipBulder;
        ipBulder =  ((ipAddressInt & 0xFF)) + "." + ipBulder;

        return ipBulder.trim();


    }

}
 
Example 19
Source File: WifiScanner.java    From PhoneProfilesPlus with Apache License 2.0 4 votes vote down vote up
@SuppressLint("NewApi")
private int enableWifi(WifiManager wifi, Handler wifiChangeHandler)
{
    //PPApplication.logE("@@@ WifiScanner.enableWifi","xxx");

    int wifiState = wifi.getWifiState();
    int forceScan = ApplicationPreferences.prefForceOneWifiScan;

    //if ((!dataWrapper.getIsManualProfileActivation()) || forceScan)
    //{
    if (wifiState != WifiManager.WIFI_STATE_ENABLING)
    {
        boolean isWifiEnabled = (wifiState == WifiManager.WIFI_STATE_ENABLED);
        boolean isScanAlwaysAvailable = false;
        if (forceScan != FORCE_ONE_SCAN_FROM_PREF_DIALOG) {
            // this must be disabled because scanning not working, when wifi is disabled after disabled WiFi AP
            // Tested and scanning working ;-)
            //if (android.os.Build.VERSION.SDK_INT >= 18)
                isScanAlwaysAvailable = wifi.isScanAlwaysAvailable();
        }
        //PPApplication.logE("@@@ WifiScanner.enableWifi","isScanAlwaysAvailable="+isScanAlwaysAvailable);
        isWifiEnabled = isWifiEnabled || isScanAlwaysAvailable;
        if (!isWifiEnabled)
        {
            boolean applicationEventWifiScanIfWifiOff = ApplicationPreferences.applicationEventWifiScanIfWifiOff;
            if (applicationEventWifiScanIfWifiOff || (forceScan != FORCE_ONE_SCAN_DISABLED))
            {
                //boolean wifiEventsExists = DatabaseHandler.getInstance(context).getTypeEventsCount(DatabaseHandler.ETYPE_WIFI_NEARBY, false) > 0;
                boolean scan = ((/*wifiEventsExists &&*/ applicationEventWifiScanIfWifiOff) ||
                        (forceScan == FORCE_ONE_SCAN_FROM_PREF_DIALOG));
                if (scan)
                {
                    WifiScanWorker.setWifiEnabledForScan(context, true);
                    WifiScanWorker.setScanRequest(context, true);
                    WifiScanWorker.lock(context);
                    final WifiManager _wifi = wifi;
                    //PPApplication.logE("[HANDLER] WifiScanner.enableWifi", "before start handler");
                    wifiChangeHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            /*if (PPApplication.logEnabled()) {
                                PPApplication.logE("PPApplication.startHandlerThread", "START run - from=WifiScanner.doScan.1");

                                PPApplication.logE("$$$ WifiScanner.enableWifi", "before enable wifi");
                                PPApplication.logE("[HANDLER] WifiScanner.enableWifi", "before enable wifi");
                                PPApplication.logE("#### setWifiEnabled", "from WifiScanner.enableWifi");
                            }*/

                            //if (Build.VERSION.SDK_INT >= 29)
                            //    CmdWifi.setWifi(true);
                            //else
                                _wifi.setWifiEnabled(true);

                            /*if (PPApplication.logEnabled()) {
                                PPApplication.logE("$$$ WifiScanner.enableWifi", "after enable wifi");

                                PPApplication.logE("PPApplication.startHandlerThread", "END run - from=WifiScanner.doScan.1");
                            }*/
                        }
                    });
                    //PPApplication.logE("@@@ WifiScanner.enableWifi","set enabled");
                    return WifiManager.WIFI_STATE_ENABLING;
                }
            }
        }
        else
        {
            // this is not needed, enableWifi() is called only from doScan and after when hotspot is disabled
            /*boolean isWifiAPEnabled = false;
            if (Build.VERSION.SDK_INT < 28) {
                WifiApManager wifiApManager = null;
                try {
                    wifiApManager = new WifiApManager(context);
                } catch (Exception e) {
                    PPApplication.recordException(e);
                }
                if (wifiApManager != null)
                    isWifiAPEnabled = wifiApManager.isWifiAPEnabled();
            }
            else
                isWifiAPEnabled = CmdWifiAP.isEnabled();*/

            if (isScanAlwaysAvailable/*  && !isWifiAPEnabled*/) {
                //PPApplication.logE("@@@ WifiScanner.enableWifi", "scan always available");
                wifiState =  WifiManager.WIFI_STATE_ENABLED;
            }
            return wifiState;
        }
    }
    //}

    return wifiState;
}
 
Example 20
Source File: DeviceUtils.java    From DoraemonKit with Apache License 2.0 2 votes vote down vote up
/**
 * 判断wifi是否开启
 *
 * @param context
 * @return
 */
public static boolean isWifiEnabled(Context context) {
    WifiManager wm = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    return (wm != null)
            && (wm.getWifiState() == WifiManager.WIFI_STATE_ENABLED || wm.getWifiState() == WifiManager.WIFI_STATE_ENABLING);
}