I am developing a wifi search application. Whenever the device is in aeroplane mode wifi is turned off automatically. I found that system apps can turn on the wifi even if the device is in aeroplane mode. I am testing it on Android 9. How do I make my app can able to turn on the wifi?
Asked
Active
Viewed 1,731 times
0
-
1What have you researched or attempted so far? – Andreas Jan 23 '19 at 07:04
-
Checked this WifiManager.setWifiEnabled(true) method and it was returning false when the device is in aeroplane. – Asharali V U Jan 23 '19 at 07:13
-
Do your app has the required permission to change the wifi state? – Andreas Jan 23 '19 at 07:14
-
Yes I have CHANGE_WIFI_STATE,ACCESS_WIFI_STATE, and WRITE_SETTINGS permissions. – Asharali V U Jan 23 '19 at 07:16
-
Check this out then https://stackoverflow.com/questions/8709188/wifi-state-is-not-enabling – Andreas Jan 23 '19 at 07:19
-
I have tried this, but didn't work on Android 9.This works good for below android 9 devices. I think there is a restriction in Android 9, WifiManager.setWifiEnabled(true) return false when in aeroplane mode or tethering hotspot is on. – Asharali V U Jan 23 '19 at 08:39
1 Answers
1
In android 9, you cannot turn on wifi programmatically when the device is in airplane mode or is tethering hotspot. In this case, the
boolean retVal = mWifiManager.setWifiEnabled(true);
always returns false as the device is in airplane mode. But I've found an unethical way to do it. Not sure even if is it good to do it this way. Works for me.
if(myWifiManager.isWifiEnabled()){
System.out.println("Toggle Wifi Enabled going to disable");
myWifiManager.setWifiEnabled(false);
}
else{
System.out.println("Wifi Disabled going to enable ");
if(Settings.System.getInt(context.getContentResolver(),Settings.System.AIRPLANE_MODE_ON, 0) != 0){
Runtime.getRuntime().exec("adb shell settings put global airplane_mode_radios cell,nfc,wimax,bluetooth");
}
myWifiManager.setWifiEnabled(true);
System.out.println("WI: "+myWifiManager.isWifiEnabled());
}
Explanation for the adb command is here. As mentioned in the link, you can turn things back to how they were by adding the following code when the airplane mode is switched off.
Runtime.getRuntime().exec("adb shell settings delete global airplane_mode_radios");

ExodosJr
- 26
- 3