Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Security exception by accessing the wifi in android

Tags:

android

wifi

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

like image 696
androider Avatar asked Nov 23 '11 10:11

androider


2 Answers

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.

like image 163
YoYoMa Avatar answered Sep 19 '22 13:09

YoYoMa


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!

like image 28
visrahane Avatar answered Sep 20 '22 13:09

visrahane