Java Code Examples for android.net.wifi.WifiManager#WIFI_STATE_ENABLING

The following examples show how to use android.net.wifi.WifiManager#WIFI_STATE_ENABLING . 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: 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 2
Source File: WifiManagerSnippet.java    From mobly-bundled-snippets with Apache License 2.0 6 votes vote down vote up
@Rpc(description = "Turns off Wi-Fi with a 30s timeout.")
public void wifiDisable() throws InterruptedException, WifiManagerSnippetException {
    if (mWifiManager.getWifiState() == WifiManager.WIFI_STATE_DISABLED) {
        return;
    }
    // If Wi-Fi is trying to turn on, wait for that to complete before continuing.
    if (mWifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING) {
        if (!Utils.waitUntil(
                () -> mWifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLED,
                TIMEOUT_TOGGLE_STATE)) {
            Log.e(String.format("Wi-Fi failed to stabilize after %ss.", TIMEOUT_TOGGLE_STATE));
        }
    }
    if (!mWifiManager.setWifiEnabled(false)) {
        throw new WifiManagerSnippetException("Failed to initiate disabling Wi-Fi.");
    }
    if (!Utils.waitUntil(
            () -> mWifiManager.getWifiState() == WifiManager.WIFI_STATE_DISABLED,
            TIMEOUT_TOGGLE_STATE)) {
        throw new WifiManagerSnippetException(
                String.format(
                        "Failed to disable Wi-Fi after %ss, timeout!", TIMEOUT_TOGGLE_STATE));
    }
}
 
Example 3
Source File: WifiStateChangeService.java    From fdroidclient with GNU General Public License v3.0 6 votes vote down vote up
private String printWifiState(int wifiState) {
    switch (wifiState) {
        case WifiManager.WIFI_STATE_DISABLED:
            return "WIFI_STATE_DISABLED";
        case WifiManager.WIFI_STATE_DISABLING:
            return "WIFI_STATE_DISABLING";
        case WifiManager.WIFI_STATE_ENABLING:
            return "WIFI_STATE_ENABLING";
        case WifiManager.WIFI_STATE_ENABLED:
            return "WIFI_STATE_ENABLED";
        case WifiManager.WIFI_STATE_UNKNOWN:
            return "WIFI_STATE_UNKNOWN";
        case Integer.MIN_VALUE:
            return "previous value unset";
        default:
            return "~not mapped~";
    }
}
 
Example 4
Source File: DLNAService.java    From AndroidVideoPlayer with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onReceive(Context c, Intent intent) {
	Bundle bundle = intent.getExtras();
	int statusInt = bundle.getInt("wifi_state");
	switch (statusInt) {
	case WifiManager.WIFI_STATE_UNKNOWN:
		break;
	case WifiManager.WIFI_STATE_ENABLING:
		break;
	case WifiManager.WIFI_STATE_ENABLED:
		LogUtil.e(TAG, "wifi enable");
		startThread();
		break;
	case WifiManager.WIFI_STATE_DISABLING:
		break;
	case WifiManager.WIFI_STATE_DISABLED:
		LogUtil.e(TAG, "wifi disabled");
		break;
	default:
		break;
	}
}
 
Example 5
Source File: WifiUtils.java    From FlyWoo with Apache License 2.0 5 votes vote down vote up
/**
 * Function: 连接Wifi热点 <br>
 *
 * @param SSID
 * @param Password
 * @param Type     <br>
 *                 没密码: {@linkplain WifiCipherType#WIFICIPHER_NOPASS}<br>
 *                 WEP加密: {@linkplain WifiCipherType#WIFICIPHER_WEP}<br>
 *                 WPA加密: {@linkplain WifiCipherType#WIFICIPHER_WPA}<br>
 * @return true:连接成功;false:连接失败
 */
public static boolean connectWifi(String SSID, String Password, WifiCipherType Type) {
    if (!isWifiEnabled()) {
        return false;
    }
    // 开启wifi需要一段时间,要等到wifi状态变成WIFI_STATE_ENABLED
    while (mWifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING) {
        try {
            // 避免程序不停循环
            Thread.currentThread();
            Thread.sleep(500);
        } catch (InterruptedException ie) {
        }
    }

    WifiConfiguration wifiConfig = createWifiInfo(SSID, Password, Type);
    if (wifiConfig == null) {
        return false;
    }

    WifiConfiguration tempConfig = isExsits(SSID);

    if (tempConfig != null) {
        mWifiManager.removeNetwork(tempConfig.networkId);
    }

    int netID = mWifiManager.addNetwork(wifiConfig);

    // 断开连接
    mWifiManager.disconnect();

    // 设置为true,使其他的连接断开
    boolean bRet = mWifiManager.enableNetwork(netID, true);
    mWifiManager.reconnect();
    return bRet;
}
 
Example 6
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 true:打开;false:关闭
 */
public static boolean isWifiOpen(Context context) throws Exception {
    int wifiState = getWifiState(context);
    return wifiState == WifiManager.WIFI_STATE_ENABLED ||
            wifiState == WifiManager.WIFI_STATE_ENABLING
            ? true
            : false;
}
 
Example 7
Source File: WifiApManager.java    From PhoneProfilesPlus with Apache License 2.0 5 votes vote down vote up
void startTethering(boolean doNotChangeWifi) {
    //PPApplication.logE("WifiApManager.startTethering", "mWifiManager="+mWifiManager);
    if (!doNotChangeWifi) {
        if (mWifiManager != null) {
            int wifiState = mWifiManager.getWifiState();
            boolean isWifiEnabled = ((wifiState == WifiManager.WIFI_STATE_ENABLED) || (wifiState == WifiManager.WIFI_STATE_ENABLING));
            //PPApplication.logE("WifiApManager.startTethering", "isWifiEnabled="+isWifiEnabled);
            if (isWifiEnabled) {
                //PPApplication.logE("#### setWifiEnabled", "from WifiAPManager.startTethering");
                //if (Build.VERSION.SDK_INT >= 29)
                //    CmdWifi.setWifi(false);
                //else
                    mWifiManager.setWifiEnabled(false);
            }
        }
    }
    //PPApplication.logE("WifiApManager.startTethering", "mConnectivityManager="+mConnectivityManager);
    if (mConnectivityManager != null) {
        try {
            //noinspection JavaReflectionMemberAccess
            Field internalConnectivityManagerField = ConnectivityManager.class.getDeclaredField("mService");
            internalConnectivityManagerField.setAccessible(true);

            callStartTethering(internalConnectivityManagerField.get(mConnectivityManager));
        } catch (Exception e) {
            //Log.e("WifiApManager.startTethering", Log.getStackTraceString(e));
            PPApplication.recordException(e);
            //PPApplication.logE("WifiApManager.startTethering", Log.getStackTraceString(e));
        }
    }
}
 
Example 8
Source File: WifiAutoConnectManager.java    From gokit-android with MIT License 5 votes vote down vote up
@Override
public void run() {
	// 打开wifi
	openWifi();
	// 开启wifi功能需要一段时间(我在手机上测试一般需要1-3秒左右),所以要等到wifi
	// 状态变成WIFI_STATE_ENABLED的时候才能执行下面的语句
	while (wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING) {
		try {
			// 为了避免程序一直while循环,让它睡个100毫秒检测……
			Thread.sleep(100);

		} catch (InterruptedException ie) {
			Log.e(TAG, ie.toString());
		}
	}

	WifiConfiguration tempConfig = isExsits(ssid);

	if (tempConfig != null) {
		// wifiManager.removeNetwork(tempConfig.networkId);

		boolean b = wifiManager.enableNetwork(tempConfig.networkId,
				true);
	} else {
		WifiConfiguration wifiConfig = createWifiInfo(ssid, password,
				type);
		//
		if (wifiConfig == null) {
			Log.d(TAG, "wifiConfig is null!");
			return;
		}

		int netID = wifiManager.addNetwork(wifiConfig);
		boolean enabled = wifiManager.enableNetwork(netID, true);
		Log.d(TAG, "enableNetwork status enable=" + enabled);
		boolean connected = wifiManager.reconnect();
		Log.d(TAG, "enableNetwork connected=" + connected);
	}

}
 
Example 9
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 true:打开;false:关闭
 */
public static boolean isWifiOpen(Context context) throws Exception {
    int wifiState = getWifiState(context);
    return wifiState == WifiManager.WIFI_STATE_ENABLED ||
            wifiState == WifiManager.WIFI_STATE_ENABLING
            ? true
            : false;
}
 
Example 10
Source File: WifiAutoConnectManager.java    From GOpenSource_AppKit_Android_AS with MIT License 5 votes vote down vote up
@Override
public void run() {
	// 打开wifi
	openWifi();
	// 开启wifi功能需要一段时间(我在手机上测试一般需要1-3秒左右),所以要等到wifi
	// 状态变成WIFI_STATE_ENABLED的时候才能执行下面的语句
	while (wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING) {
		try {
			// 为了避免程序一直while循环,让它睡个100毫秒检测……
			Thread.sleep(100);

		} catch (InterruptedException ie) {
			Log.e(TAG, ie.toString());
		}
	}

	WifiConfiguration tempConfig = isExsits(ssid);

	if (tempConfig != null) {
		// wifiManager.removeNetwork(tempConfig.networkId);

		boolean b = wifiManager.enableNetwork(tempConfig.networkId,
				true);
	} else {
		WifiConfiguration wifiConfig = createWifiInfo(ssid, password,
				type);
		//
		if (wifiConfig == null) {
			Log.d(TAG, "wifiConfig is null!");
			return;
		}

		int netID = wifiManager.addNetwork(wifiConfig);
		boolean enabled = wifiManager.enableNetwork(netID, true);
		Log.d(TAG, "enableNetwork status enable=" + enabled);
		boolean connected = wifiManager.reconnect();
		Log.d(TAG, "enableNetwork connected=" + connected);
	}

}
 
Example 11
Source File: WifiAutoConnectManager.java    From Gizwits-SmartBuld_Android with MIT License 5 votes vote down vote up
@Override
public void run() {
	// 打开wifi
	openWifi();
	// 开启wifi功能需要一段时间(我在手机上测试一般需要1-3秒左右),所以要等到wifi
	// 状态变成WIFI_STATE_ENABLED的时候才能执行下面的语句
	while (wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING) {
		try {
			// 为了避免程序一直while循环,让它睡个100毫秒检测……
			Thread.sleep(100);

		} catch (InterruptedException ie) {
		}
	}

	WifiConfiguration wifiConfig = createWifiInfo(ssid, password, type);
	//
	if (wifiConfig == null) {
		Log.d(TAG, "wifiConfig is null!");
		return;
	}

	WifiConfiguration tempConfig = isExsits(ssid);

	if (tempConfig != null) {
		wifiManager.removeNetwork(tempConfig.networkId);
	}

	int netID = wifiManager.addNetwork(wifiConfig);
	boolean enabled = wifiManager.enableNetwork(netID, true);
	Log.d(TAG, "enableNetwork status enable=" + enabled);
	boolean connected = wifiManager.reconnect();
	Log.d(TAG, "enableNetwork connected=" + connected);
}
 
Example 12
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 13
Source File: WifiAutoConnectManager.java    From YiZhi with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    // 打开wifi
    openWifi();
    // 开启wifi功能需要一段时间(我在手机上测试一般需要1-3秒左右),所以要等到wifi
    // 状态变成WIFI_STATE_ENABLED的时候才能执行下面的语句
    while (wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING) {
        try {
            // 为了避免程序一直while循环,让它睡个100毫秒检测……
            Thread.sleep(100);

        } catch (InterruptedException ie) {
            LogUtils.e(ie.toString());
        }
    }

    WifiConfiguration tempConfig = isExsits(ssid);

    if (tempConfig != null) {
        boolean b = wifiManager.enableNetwork(tempConfig.networkId,
                true);
    } else {
        WifiConfiguration wifiConfig = createWifiInfo(ssid, password,
                type);
        if (wifiConfig == null) {
            LogUtils.d("wifiConfig is null!");
            return;
        }

        int netID = wifiManager.addNetwork(wifiConfig);
        boolean enabled = wifiManager.enableNetwork(netID, true);
        LogUtils.d("enableNetwork status enable=" + enabled);
        boolean connected = wifiManager.reconnect();
        LogUtils.d("enableNetwork connected=" + connected);
    }

}
 
Example 14
Source File: WifiApManager.java    From PhoneProfilesPlus with Apache License 2.0 5 votes vote down vote up
private void setWifiApState(WifiConfiguration config, boolean enabled, boolean doNotChangeWifi) {
    try {
        /*if (PPApplication.logEnabled()) {
            PPApplication.logE("$$$ WifiAP", "WifiApManager.setWifiApState-config=" + config);
            PPApplication.logE("$$$ WifiAP", "WifiApManager.setWifiApState-enabled=" + enabled);
            PPApplication.logE("$$$ WifiAP", "WifiApManager.setWifiApState-mWifiManager=" + mWifiManager);
            PPApplication.logE("$$$ WifiAP", "WifiApManager.setWifiApState-wifiControlMethod=" + wifiControlMethod);
        }*/
        if (enabled) {
            if (!doNotChangeWifi) {
                if (mWifiManager != null) {
                    int wifiState = mWifiManager.getWifiState();
                    boolean isWifiEnabled = ((wifiState == WifiManager.WIFI_STATE_ENABLED) || (wifiState == WifiManager.WIFI_STATE_ENABLING));
                    if (isWifiEnabled) {
                        //PPApplication.logE("#### setWifiEnabled", "from WifAPManager.setWifiApState");
                        //if (Build.VERSION.SDK_INT >= 29)
                        //    CmdWifi.setWifi(false);
                        //else
                        mWifiManager.setWifiEnabled(false);
                    }
                }
            }
        }
        wifiControlMethod.setAccessible(true);
        wifiControlMethod.invoke(mWifiManager, config, enabled);
    } catch (Exception e) {
        //Log.e(TAG, "", e);
        //Log.e("$$$ WifiAP", "WifiApManager.setWifiApState-exception="+e);
        PPApplication.recordException(e);
    }
}
 
Example 15
Source File: QuickWifiHotUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    if (isStop) return;
    // 如果属于需要销毁, 则全部不处理
    if (operate != null && operate.isFinishing()) {
        isStop = true;
        isCheck = false;
        isThreadCheckHot = false;
        return;
    }

    // 是否延时检查
    boolean isPostDelayed = false;
    // 获取 Wifi 连接状态
    switch (wifiUtils.getWifiState()) {
        case WifiManager.WIFI_STATE_ENABLED: // 已打开
        case WifiManager.WIFI_STATE_ENABLING: // 正在打开
            // case WifiManager.WIFI_STATE_UNKNOWN: // 未知
            isPostDelayed = true;
            DevLogger.dTag(TAG, "Wifi 已打开、正在打开");
            wifiUtils.closeWifi(); // 关闭 Wifi
            break;
        case WifiManager.WIFI_STATE_DISABLED: // 已关闭
            isPostDelayed = false;
            DevLogger.dTag(TAG, "Wifi 已关闭");
            break;
        case WifiManager.WIFI_STATE_DISABLING: // 正在关闭
            isPostDelayed = true;
            DevLogger.dTag(TAG, "Wifi 正在关闭");
            break;
    }
    // 判断是否延时 0.4 秒进行开启热点
    if (isPostDelayed) {
        // 删除上一个任务, 并且重新绑定任务
        hotHandler.removeCallbacks(closeWifiThread);
        hotHandler.postDelayed(closeWifiThread, 400);
    } else { // 开启热点
        hotHandler.sendEmptyMessage(CLOSE_WIFI_SUCCESS);
    }
}
 
Example 16
Source File: WifiHelper.java    From AndroidBasicProject with MIT License 5 votes vote down vote up
/**
 * 启用/禁用 wifi
 * @param enabled
 */
@RequiresPermission(allOf = {
        Manifest.permission.CHANGE_WIFI_STATE,
        Manifest.permission.ACCESS_WIFI_STATE})
public void setEnabled(boolean enabled) {
    if(mWifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING ||
            mWifiManager.getWifiState() == WifiManager.WIFI_STATE_DISABLING){
        return;
    }
    mWifiManager.setWifiEnabled(enabled);
}
 
Example 17
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 18
Source File: WifiSetupActivity.java    From STGUploader with MIT License 4 votes vote down vote up
private boolean isWifiEnabled() {
    int state = wifiManager.getWifiState();
    return state == WifiManager.WIFI_STATE_ENABLING || state == WifiManager.WIFI_STATE_ENABLED;
}
 
Example 19
Source File: WifiHelper.java    From AndroidBasicProject with MIT License 4 votes vote down vote up
/**
 * wifi是否打开
 * @return
 */
@RequiresPermission(Manifest.permission.ACCESS_WIFI_STATE)
public boolean isEnabled() {
    return mWifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING ||
           mWifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLED;
}
 
Example 20
Source File: DeveloperActivity.java    From OpenMemories-Tweak with MIT License 4 votes vote down vote up
public boolean isWifiEnabled() {
    int state = wifiManager.getWifiState();
    return state == WifiManager.WIFI_STATE_ENABLING || state == WifiManager.WIFI_STATE_ENABLED;
}