Problem:
I required my device to have its Wifi enabled, but should not connect to a network.
Tried:
wifiManager.disconnect();
wifiManager.enableNetwork(-1, true);
The particular line of interest is the enabledNetwork(-1, true);
Declaration:
public boolean enableNetwork(int netId, boolean disableOthers) {
My code disconnects from the current network, but after a brief period of time, auto-configures itself and connects to the most prefered network, thus ignoring the disableOthers
feature.
I did some more searching, I found:
wifiManager.getWifiState();
Awesome! and we find as a constant in the WifiManager
class:
public static final int WIFI_MODE_SCAN_ONLY = 2;
This is exactly what I want, but, how do I implement this?
TL;DR: I require my Wifi to be in a scan only mode, meaning it should not connect to any network but stay scanning.
Not sure if you were looking for something while the device is idle. If yes, you probably want to give an attempt to wifilocks . Its purpose is to keep the wifi modul active even though the device is idle (similarly to wakelocks for the CPU).
So you create one, than call acquire() . At this point, the wifi modul wont be turned off until you call release(). If you disconnected from the network before, the documentation suggests it wont connect automatically (while the lock is hold and device is idle).
Note: actual behavior, that is not exactly defined by the documentation might be device dependent.
Alternatively, you might keep disconnecting from any network like this: android-how-to-disconnect-from-a-wifi-network
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.