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

The following examples show how to use android.net.wifi.WifiManager#WIFI_STATE_ENABLED . 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: WifiStateReceiver.java    From WifiUtils with Apache License 2.0 7 votes vote down vote up
@Override
public void onReceive(Context context, @NonNull Intent intent) {
    int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);

    switch (wifiState) {
        case WifiManager.WIFI_STATE_ENABLED:
            wifiStateCallback.onWifiEnabled();
            break;
        case WifiManager.WIFI_STATE_ENABLING:
            break;
        case WifiManager.WIFI_STATE_DISABLING:
            break;
        case WifiManager.WIFI_STATE_DISABLED:
            break;
    }
}
 
Example 2
Source File: SwapWorkflowActivity.java    From fdroidclient with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    setUpFromWifi();

    int wifiStatus = -1;
    TextView textWifiVisible = container.findViewById(R.id.wifi_visible);
    if (textWifiVisible != null) {
        intent.getIntExtra(WifiStateChangeService.EXTRA_STATUS, -1);
    }
    switch (wifiStatus) {
        case WifiManager.WIFI_STATE_ENABLING:
            textWifiVisible.setText(R.string.swap_setting_up_wifi);
            break;
        case WifiManager.WIFI_STATE_ENABLED:
            textWifiVisible.setText(R.string.swap_not_visible_wifi);
            break;
        case WifiManager.WIFI_STATE_DISABLING:
        case WifiManager.WIFI_STATE_DISABLED:
            textWifiVisible.setText(R.string.swap_stopping_wifi);
            break;
        case WifiManager.WIFI_STATE_UNKNOWN:
            break;
    }
}
 
Example 3
Source File: WifiEnabledSlot.java    From Easer with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {
        int extraWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);

        switch (extraWifiState)
        {
            case WifiManager.WIFI_STATE_DISABLED:
                changeSatisfiedState(!eventData.enabled);
                break;
            case WifiManager.WIFI_STATE_ENABLED:
                changeSatisfiedState(eventData.enabled);
                break;
        }
    }
}
 
Example 4
Source File: ConfirmationFragment.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
@Override
public void onReceive(final Context context, final Intent intent) {
    String action = intent.getAction();
    mLogger.info("ConfirmationFragment: action = " + action
            + ", isWaitingWiFiEnabled = " + mIsWaitingWifiEnabled);
    if (!mIsWaitingWifiEnabled) {
        return;
    }
    if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
        int state = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
        if (state == WifiManager.WIFI_STATE_ENABLED) {
            mIsWaitingWifiEnabled = false;
            connectTheta();
        }
    }
}
 
Example 5
Source File: WifiEnabledTracker.java    From Easer with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {
        int extraWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);

        switch (extraWifiState)
        {
            case WifiManager.WIFI_STATE_DISABLED:
                newSatisfiedState(!data.enabled);
                break;
            case WifiManager.WIFI_STATE_ENABLED:
                newSatisfiedState(data.enabled);
                break;
            default:
                newSatisfiedState(null);
        }
    }
}
 
Example 6
Source File: ConnectivityActivity.java    From RoMote with Apache License 2.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    if (intent == null
            || (!intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)
            && !intent.getAction().equals("android.net.conn.CONNECTIVITY_CHANGE"))) {
        return;
    }

    if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
        int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED);

        boolean isConnected = wifiState == WifiManager.WIFI_STATE_ENABLED;

        if (!isConnected &&
                !mNetworkMonitor.isMobileAccessPointOn() &&
                mDialog == null) { //!mNetworkMonitor.isConnectedToiWiFi() && mDialog == null) {
            showDialog();
            onWifiDisconnected();
        }
    } else if (mNetworkMonitor.isConnectedToiWiFi() && mDialog != null) {
        dismissDialog();
        onWifiConnected();
    }
}
 
Example 7
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 8
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 9
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 10
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 11
Source File: WifiActivity.java    From PMCADemo with MIT License 5 votes vote down vote up
protected void wifiStateChanged(int state) {
    switch (state) {
        case WifiManager.WIFI_STATE_ENABLING:
            log("Enabling wifi");
            break;
        case WifiManager.WIFI_STATE_ENABLED:
            log("Wifi enabled");
            break;
    }
}
 
Example 12
Source File: RepeatTask.java    From PhoneMonitor with GNU General Public License v3.0 5 votes vote down vote up
private void uploadRecordings() {
    if (getRecordingsFromDataFolder().length > 0) {//if any recordings present
        int initialWIFIState = WifiManager.WIFI_STATE_DISABLED;
        WifiManager wifiManager = (WifiManager) _context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);

        if (new AppSettings(_context).getForceWifiOnForRecordUpload()) {//if wifi is to be enabled first
            if (wifiManager != null) {
                initialWIFIState = wifiManager.getWifiState();//get current wifi state
                wifiManager.setWifiEnabled(true);//enable wifi
                /*waits till internet connection is available or timeout reached whichever occurs first*/
                try {
                    HelperMethods.waitWithTimeout(new Callable() {
                        @Override
                        public Object call() throws Exception {
                            return HelperMethods.isInternetAvailable(_context);
                        }
                    }, true, 10000);
                } catch (Exception ex) {
                    Log.w(AppSettings.getTAG(), "Exception at RepeatTask.run()\n" + ex.getMessage());
                }

            }
        }

        FileUploader fileUploader = new FileUploader(_context);
        uploadAndDeleteFiles(fileUploader);

        if (new AppSettings(_context).getForceWifiOnForRecordUpload() && wifiManager != null && initialWIFIState != WifiManager.WIFI_STATE_ENABLED)
            wifiManager.setWifiEnabled(false);
    }
}
 
Example 13
Source File: CmdWifiAP.java    From PhoneProfilesPlus with Apache License 2.0 5 votes vote down vote up
static boolean setWifiAP(boolean enable, boolean doNotChangeWifi) {
    //PPApplication.logE("CmdWifiAP.setWifiAP", "START enable="+enable);
    //PPApplication.logE("CmdWifiAP.setWifiAP", "START doNotChangeWifi="+doNotChangeWifi);
    final String packageName = PPApplication.PACKAGE_NAME;
    try {
        IConnectivityManager connectivityAdapter = IConnectivityManager.Stub.asInterface(ServiceManager.getService("connectivity"));  // service list | grep IConnectivityManager
        //PPApplication.logE("CmdWifiAP.setWifiAP", "connectivityAdapter="+connectivityAdapter);
        if (enable) {
            if (!doNotChangeWifi) {
                IWifiManager wifiAdapter = IWifiManager.Stub.asInterface(ServiceManager.getService("wifi"));  // service list | grep IWifiManager
                //PPApplication.logE("CmdWifiAP.setWifiAP", "wifiAdapter="+wifiAdapter);
                int wifiState = wifiAdapter.getWifiEnabledState();
                boolean isWifiEnabled = ((wifiState == WifiManager.WIFI_STATE_ENABLED) || (wifiState == WifiManager.WIFI_STATE_ENABLING));
                //PPApplication.logE("CmdWifiAP.setWifiAP", "isWifiEnabled="+isWifiEnabled);
                if (isWifiEnabled)
                    wifiAdapter.setWifiEnabled(packageName, false);
            }

            ResultReceiver dummyResultReceiver = new ResultReceiver(null);
            connectivityAdapter.startTethering(0, dummyResultReceiver, false, packageName);
        } else {
            connectivityAdapter.stopTethering(0, packageName);
        }

        //PPApplication.logE("CmdWifiAP.setWifiAP", "END=");
        return true;
    } catch (java.lang.SecurityException ee) {
        //Log.e("CmdWifiAP.setWifiAP", Log.getStackTraceString(ee));
        //PPApplication.logToCrashlytics("E/CmdWifiAP.setWifiAP: " + Log.getStackTraceString(ee));
        //PPApplication.recordException(e);
        //PPApplication.logE("CmdWifiAP.setWifiAP", Log.getStackTraceString(e));
        return false;
    } catch (Throwable e) {
        //Log.e("CmdWifiAP.setWifiAP", Log.getStackTraceString(e));
        PPApplication.recordException(e);
        //PPApplication.logE("CmdWifiAP.setWifiAP", Log.getStackTraceString(e));
        return false;
    }
}
 
Example 14
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 15
Source File: NetworkConnectivityManager.java    From android-project-wo2b with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param context
 * @param intent
 * @param action
 */
public void wifiStateChangedAction(Context context, Intent intent, String action)
{
	if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action))
	{
		// 在此监听wifi有无
		int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
		switch (wifiState)
		{
			case WifiManager.WIFI_STATE_DISABLED:
			{
				Toast.makeText(context, "WIFI_STATE_DISABLED", 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_ENABLED:
			{
				Toast.makeText(context, "WIFI_STATE_ENABLED", 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 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: 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 18
Source File: BaseActivity.java    From OpenMemories-AppStore with MIT License 5 votes vote down vote up
public WifiState getWifiState() {
    if (Environment.isEmulator()) {
        return WifiState.CONNECTED;
    } else {
        switch (wifiManager.getWifiState()) {
            case WifiManager.WIFI_STATE_ENABLED:
                if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected()) {
                    return WifiState.CONNECTED;
                } else {
                    switch (WifiInfo.getDetailedStateOf(wifiManager.getConnectionInfo().getSupplicantState())) {
                        case SCANNING:
                            return WifiState.SCANNING;
                        case AUTHENTICATING:
                        case CONNECTING:
                        case OBTAINING_IPADDR:
                            return WifiState.CONNECTING;
                        default:
                            return WifiState.ENABLED;
                    }
                }
            case WifiManager.WIFI_STATE_ENABLING:
                return WifiState.ENABLING;
            default:
                return WifiState.DISABLED;
        }
    }
}
 
Example 19
Source File: MyWifiAnd3G.java    From zone-sdk with MIT License 4 votes vote down vote up
/**
 *  连接指定的配置好的网络进行 
 * 
 * @param wcfg     WifiConfiguration(能在此类中get到)
 * @param mostCount     最多连多少次
 * @param sleepMs     每次睡多少毫秒
 */
public boolean connectConfiguration(WifiConfiguration wcfg,int mostCount,int sleepMs) {
	// 索引大于配置好的网络索引返回
	if (wcfg ==null) {
		System.err.println("WifiConfiguration wcfg为null!!!");
		return  false;
	}
	mWifiInfo=mWifiManager.getConnectionInfo();
	if (mWifiInfo != null && mWifiInfo.getNetworkId() == wcfg.networkId&&mWifiInfo.getLinkSpeed()>0) {
		// 不连接  有wifi并且当前wifi  是这个即将要连接的wifi
		System.out.println("当前连接的wifi 已经连接上 不必重复连接~~~");
		return  true;

	} else {
		// 连接配置好的指定ID的网络
		System.out.println("要连接的netid:\t" + wcfg.networkId);
		boolean connStatue= mWifiManager.enableNetwork(wcfg.networkId, true);
		boolean  resultStatue=false;
		if (connStatue) {
			// 当连接 wifi 成功的时候
			int i = 1;
			while (i <= mostCount) {
				try {
					if (getWifiInfo().getNetworkId() == wcfg.networkId
							&& WifiManager.WIFI_STATE_ENABLED == mWifiManager.getWifiState()) {
						resultStatue = true;
						System.out.println("wifi开启成功success!!!");
						break;
					}
					Thread.sleep(sleepMs);
					if (i == 1) {
						System.out.println("每次等待时间为:\t" + sleepMs + "\tms");
					}
					System.out.println("等待wifi完全开启  第" + i + "次");
					i++;
				} catch (InterruptedException e) {
					e.printStackTrace();
					System.err.println("Thread sleep发生异常");
				}
			}
			if (!resultStatue) {
				System.err.println("连接fail");
			}
		}
	
		return resultStatue;
	}
	
}
 
Example 20
Source File: NetWorkUtils.java    From Simpler with Apache License 2.0 2 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;
}