Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 543
Kamalone Avatar asked Jan 03 '12 07:01

Kamalone


People also ask

Why is my Wi-Fi showing disabled?

Go to settings, then on Wireless and Network check to ensure that the WiFi icon is turned on. Alternatively, draw down the notification bar menu, then enable WiFi icon if it's off. Many users have reported having fixed android wifi problem by simply disabling airplane mode.

Why can't I turn on my Wi-Fi on my Android?

Open your Android Settings and tap Connections. Make sure the Wi-Fi switch is toggled on. You can try toggling the setting off and on again to reset the connection. If your Android device is too far from the modem, you won't be able to connect to Wi-Fi.

Why can't I turn on my Wi-Fi on my phone?

Disable Airplane Mode Some Android devices will not turn on Wi-Fi if Airplane mode is active. Swipe down from the notification panel and tap the airplane icon to disable Airplane mode. Alternatively, you can navigate to Settings > Network & Internet > Advanced and toggle off Airplane mode.


2 Answers

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

like image 181
Horrorgoogle Avatar answered Oct 20 '22 09:10

Horrorgoogle


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.

like image 26
Uttkarsh Patel Avatar answered Oct 20 '22 08:10

Uttkarsh Patel