I am trying to create a widget for enabling and disabling the wifi.
if(myWifiManager.isWifiEnabled()){
System.out.println("Toggle Wifi Enabled going to disable");
myWifiManager.setWifiEnabled(false);
}
else{
System.out.println("Wifi Disabled going to enable ");
myWifiManager.setWifiEnabled(true);
System.out.println("WI: "+myWifiManager.isWifiEnabled());
}
This is the code i am using the disabling part is working fine but the enabling part is not working fine. Soon after enabling the wifi i am printing the wifi state i am getting it as false.
Here is how to turn on and turn off wifi in android.
First you need to declare the following in your manifest file
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
After doing it that on your Activity class
private WifiManager wifiManager;
#Override
public void onCreate(Bundle icicle) {
....................
wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
if(wifiManager.isWifiEnabled()){
wifiManager.setWifiEnabled(false);
}else{
wifiManager.setWifiEnabled(true);
}
}
Explanation
Get the Wifi service from our system
wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
Check the our wifi is currently turned on or turned off
if(wifiManager.isWifiEnabled()){
Turn on/off our wifi
wifiManager.setWifiEnabled();
Reference
WifiEnabler
http://google-androidlovers.blogspot.com/2012/01/scan-for-wireless-networks-in-android.html
http://www.java2s.com/Open-Source/Android/android-platform-apps/Settings/com/android/settings/wifi/WifiApEnabler.java.htm
Deprecated: Starting with Build.VERSION_CODES#Q, applications are not
allowed to enable/disable Wi-Fi. Compatibility Note: For applications
targeting android.os.Build.VERSION_CODES#Q or above, this API will
always fail and return false. If apps are targeting an older SDK
(android.os.Build.VERSION_CODES#P or below), they can continue to use
this API.
(Source)
So this will only work for devices with android Pie and below.
Download this example it is what you want
https://github.com/siddhpuraamitr/WIfi-Toggle-Widget
Related
I want to create app functionality to turn on WIFI programmatically.
In android wifi.setWifiEnabled method was deprecated.
First, you need to ask the permission in the manifest file
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
Then in the activity file, you can disable wifi
WifiManager wifiManager = (WifiManager) this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
wifiManager.setWifiEnabled(false);
boolean wifiEnabled = wifiManager.isWifiEnabled()
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.
I have a problem banning a specific bssid. I can see there is a class called WifiNative that was in api 19 but not able to access it:https://android.googlesource.com/platform/frameworks/base/+/kitkat-release/wifi/java/android/net/wifi/WifiNative.java
Is there any other way anyone knows of to do this? Maybe via JNI? Any android system libraries that can be used? c,c++?
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
newWifiInfo = wifiManager.getConnectionInfo();
from there you can get the BSSID by doing
newWifiInfo.getBSSID()
then "blacklist" by turning off the wifi via
wifiManager.setWifiEnabled(false)
Also don't forget your permissions
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
If you want more control over your wifi state, you can use the following library to "sniff" your connection and other things
https://github.com/pwittchen/ReactiveNetwork
can we switch on/off the wi-fi of the device in a test cases in robotium?
because i am testing an issue which needs wifi to be on in initial phase then turning off the wi-fi and continue with testing.
Yes you can do it, see the example:
public void testNoNetworkConnection() throws Exception {
setWifiEnabled(false);
// do stuff solo.something
setWifiEnabled(true);
}
private void setWifiEnabled(boolean state) {
WifiManager wifiManager = (WifiManager)solo.getCurrentActivity().getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(state);
}
Remember to add permission in your Manifest file:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
EDIT: With the new Robotium 5.3.1 you can use setWiFiData(Boolean turnedOn) to turn wifi on or off (See documentation)
Enjoy
You can use ExtSolo provided by Testdroid. Library can be found on https://github.com/bitbar/robotium-extensions and api for needed method: turn wifi. If you don't want to use extra library you can use code as below:
protected void turnWifi(boolean enabled) {
try {
WifiManager wifiManager = (WifiManager) getInstrumentation()
.getTargetContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(enabled);
} catch (Exception ignored) {
// don't interrupt test execution, if there
// is no permission for that action
}
}
You need permission in your application to change state of Wifi
Since, you will test the application where you know the behavior(when wifi is on and when wifi is off) of application , you can mock the data accordingly. Mocking the data will make the application to behave as it is expected in that particular situation.
There is not an easy way to do this, that is different to me saying that it is not possible though but might be enough to disuade you from doing this.
If your application has the correct permissions then you can from your robotium script in fact turn on or off the wifi just as any application can. But remember the permission needs to be in your applications manifest not your test apk manifest.
If your application doesn't have the correct permission (and you are unwilling to change it) there is another way but is quite a lot of work (working on getting the solution I use open sourced). Make an application that does have the permission as a service, bind to that service in your tests and ask that service to turn off the wifi.
I am new to android i stuck for using coding enable location and sectury in that use wireless network enabled so please tell mi how it's possible
thanks
priya naral
From what I gathered, you want to programmatically turn on Wi-Fi.
If so, first you need to add these to your AndroidManifest.xml:
<application>
.
.
.
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
</application>
Then you have the ability to use WifiManager, so your code would look something like the following, where this is your application Context:
WifiManager manager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
if(!manager.isWifiEnabled()){
manager.setWifiEnabled(true);
}