Please see the following code
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled() == false)
{
Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled", Toast.LENGTH_LONG).show();
wifi.setWifiEnabled(true);
}
Though I have added the permission in manifest file as
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.wifi"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<user-permission android:name="android.permission.ACCESS_WIFI_STATE" />
but still it is giving following error
11-23 15:18:24.399: E/AndroidRuntime(6800): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.wifi/com.test.wifi.WifiDemoActivity}: java.lang.SecurityException: WifiService: Neither user 10082 nor current process has android.permission.ACCESS_WIFI_STATE.
Please help why this is happening
To be able to access the wifi you need android:name="android.permission.ACCESS_WIFI_STATE" as you already know.
If you are enabling or disabling the wifi connection you are also going to need both permissions: ACCESS_WIFI_STATE and CHANGE_WIFI_STATE
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
That should do it for you.
add following lines
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
hope they help!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With