I am trying to scan wifi networks every 2 minutes. I am using service for that. When the phone is "active" or "in use" after startscan() called I get SCAN_RESULTS_AVAILABLE_ACTION in 6 sec. So i can scan for wifis periodically. But after the phone has not been touched by anyone for a certain time (10 min) startscan() stops "working" and only after 10 min getting result. Anybody experienced this?
According to this I have found the solution - this is because the Wifi sleep policy.
You can set your wifi device never goes to sleep with this:
Settings.System.putInt(getContentResolver(),
Settings.System.WIFI_SLEEP_POLICY,
Settings.System.WIFI_SLEEP_POLICY_NEVER);
Be sure, that you added this permission in AndroidManifest.xml file:
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
the scanning period will be 5-15s - and this is automatic - you dont need to call startscan(). more info about Android scanning process can be found here.
Edit:
maybe this is even better solution if you only want to scan for hotspots:
WIFI_MODE_SCAN_ONLY - it can be activated by:
WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiLock wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_SCAN_ONLY , "MyWifiLock");
if(!wifiLock.isHeld()){
wifiLock.acquire();
}
dont forget to release it, more info about WifiLock here
also define this permission:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
Edit2 this works as well:
you can enable, and disable wifi periodically:
WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if(wm.isWifiEnabled() == false) {
wm.setWifiEnabled(true);
}
then scan after your broadcastreceiver gets the action containing WIFI_STATE_ENABLED extra
Related
I have Two buttons to disable WIFI and GPS functionality. On Clicking on button user will not allowed to change the state of gps or wifi. it has to completely block the wifi and gps. Any one having solutions?
WifiManager wifiManager = (WifiManager)this.context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(status);
also, add permission. You can find the details from here
You also need the following permissions in your manifest file:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
WifiManager wifiManager = (WifiManager) this.context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(status);
to Block wifi Completely we need a Broadcast receiver who will detect the event for wifi status change. And if the status is on we need to make it off.. by this i resoled it issue.
So I've created the majority of my application but I am having an issue with power saving applications interfering with it. I use the AlarmManager to run a piece of code that send information to a server every x minutes (minimum 1h), the main issue I am having is that power managers are disabling with WiFi because the device is sleeping.
What's the most effective way to ensure WiFi is available at wakeup? Is it to simply enable WiFi and reconnect it?
Maybe an other way to your solution is to listen for the connection_changed intents. That way you know there is a connection to the internet and you can upload.
You can also enable WiFi but you will need permissions for that: (I guess these)
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(enabled);
Haven't tried it but this should do the trick.
I'm trying to block the wifi connections. I want my application to turn on the wifi, but does not connect to any network that is already stored on the smartphone. But even after I use the SCAN_ONLY mode, he continues to connect to networks that already "know".
.....
wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
.....
wifiManager.setWifiEnabled(true);
WifiLock scanOnly = wifiManager.createWifiLock(WifiManager.WIFI_MODE_SCAN_ONLY, "scanOnly");
scanOnly.acquire();
Already in despair i tried to disconnect after to make sure that the state is WIFI_STATE_ENABLED wifi. That the app can not connect for a few seconds, but after some time it connects to a network in the same ...
wifiManager.setWifiEnabled(true);
....
WHEN (WIFI STATE == WIFI_STATE_ENABLED)
{wifiManager.disconnect();
scanOnly.acquire();}
Can someone help me?
Tks
WifiLock not for this. Only for "Allows an application to keep the Wi-Fi radio awake". This does not forbid anyone to connect to wifi. Only says "I want to be able to do at least this in sleep mode".
You have to do what you said Peanut above: get the list of all the stored networks and then disable wifi in BroadcastReceiver.
#Override
public void onReceive(Context c, Intent intent) {
saveWifiList(mainWifi.getScanResults());
unregisterReceiver(this);
mainWifi.setWifiEnabled(false);
showWifiList();
}
One way would be to get the list of all the stored networks and then disable them. You might want to store their state somewhere so you can restore the initial state after you're done.
The following will not turn wifi on, if the user has it turned off, right?:
WifiManager manager = (WifiManager)context.getSystemService(
Context.WIFI_SERVICE);
manager.startScan(); <-- !
I want to make sure the wifi isn't being turned on (if off) when the above is called,
Thanks
Yes, you are correct. The above code will not turn on wifi.
You can however use setWifiEnabled(true) to enable wi-fi access. You will need to add android.permission.CHANGE_WIFI_STATE to your manifest file.
For a long time my device would always auto start WIFI now it does not. Could someone show how to turn wifi on automatically when device boots? Also if my app sleeps sometimes on recovery wifi is gone? Show how to test if wifi is active and if not active how to turn it on programatically. Thanks
Use wifi manager class and use the following code to test wifi connectivity
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if(wifi.isWifiEnabled())
{
// write your code
}else{
wifi.setWifiEnabled(true);
}