Preferred method for programmatically disabling wifi on android - android

Hi I'm new to android and was wondering what the preferred method of disabling wifi in my app. Both methods work fine but which is preferred/faster.
1
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(false);
Uses the following permission.
CHANGE_WIFI_STATE
2
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled()) {
wifi.setWifiEnabled(false);
}
Uses the following permission.
CHANGE_WIFI_STATE
ACCESS_WIFI_STATE
Thanks in advance.

You need the following permissions in your 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 you can use the following in your activity class:
WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
You can, but its not really necessary, to use the following to check if it's enabled or not
boolean wifiEnabled = wifiManager.isWifiEnabled()

Related

how to turn on wifi programatically in flutter and android?

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()

Android blacklist wifi bssid

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 an Android app get access to the MAC address of devices trying to connect to its hotspot?

Suppose say an android phones hotspot is switched on,can an application running on the phone get access to the MAC addresses of the different devices that are trying to connect to it?
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wInfo = wifiManager.getConnectionInfo();
String macAddress = wInfo.getMacAddress();
Also, add below permission in your manifest file
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
reference on stack overflow answer

Android: Get SSID and password of a phone which is a portable wifi hotspot

I need to find a way to get the SSID of the network that my Android device is shared (My device is being a portable wifi hotspot).
Not the SSID of the network I connected to.
thanks
I finally found that there are some hidden method about getting/setting wifiAP mode in android WifiManager class. In WifiManager class, getWifiApConfiguration could be invoke to get the configuration in which the SSID of wifiAp could be found.
I know OP has already answered his/her own question, but it wasn't too clear for me, so I'll just post my complete solution here for future people that got here:
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
Method[] methods = wifimanager.getClass().getDeclaredMethods();
for (Method m: methods) {
if (m.getName().equals("getWifiApConfiguration")) {
WifiConfiguration config = (WifiConfiguration)m.invoke(wifimanager);
String ssid = config.SSID;
String bssid = config.BSSID;
//or other info about the current hotspot, accessible through config
}
}
Note: you would need the following permissions:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

WiFi state is not enabling

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

Categories

Resources