3

I've tried to create wifi tethering Hotspot in Android Marshmallow using following code.

public class WifiAccessManager {

private static final String SSID = "mHotspot";
public static boolean setWifiApState(Context context, boolean enabled) {
    //config = Preconditions.checkNotNull(config);
    try {
        WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (enabled) {
            mWifiManager.setWifiEnabled(false);
        }
        WifiConfiguration conf = getWifiApConfiguration();
        mWifiManager.addNetwork(conf);

        return (Boolean) mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class).invoke(mWifiManager, conf, enabled);
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

public static WifiConfiguration getWifiApConfiguration() {
    WifiConfiguration conf = new WifiConfiguration();
    conf.SSID =  SSID;
    conf.allowedKeyManagement.set(Integer.parseInt("12345678"));
    return conf;
}

}

And also I have given all permission to access wifi in my manifest file

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<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" />

But it's not activating in marshmallow and above version.

Whenever I am enabling the hotspot I got this error

08-18 13:56:32.304 27844-27844/com.example.deneebo.ffconnect W/System.err: java.lang.reflect.InvocationTargetException
08-18 13:56:32.304 27844-27844/com.example.deneebo.ffconnect W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
08-18 13:56:32.304 27844-27844/com.example.deneebo.ffconnect W/System.err:     at com.example.deneebo.ffconnect.WifiAccessManager.setWifiApState(WifiAccessManager.java:28)
08-18 13:56:32.304 27844-27844/com.example.deneebo.ffconnect W/System.err:     at com.example.deneebo.ffconnect.DeviceFoundActivity.onCreate(DeviceFoundActivity.java:72)
08-18 13:56:32.304 27844-27844/com.example.deneebo.ffconnect W/System.err:     at android.app.Activity.performCreate(Activity.java:6689)
08-18 13:56:32.304 27844-27844/com.example.deneebo.ffconnect W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
08-18 13:56:32.305 27844-27844/com.example.deneebo.ffconnect W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2709)
08-18 13:56:32.305 27844-27844/com.example.deneebo.ffconnect W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2825)
08-18 13:56:32.305 27844-27844/com.example.deneebo.ffconnect W/System.err:     at android.app.ActivityThread.-wrap12(ActivityThread.java)
08-18 13:56:32.305 27844-27844/com.example.deneebo.ffconnect W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1557)
08-18 13:56:32.305 27844-27844/com.example.deneebo.ffconnect W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:110)
08-18 13:56:32.305 27844-27844/com.example.deneebo.ffconnect W/System.err:     at android.os.Looper.loop(Looper.java:203)
08-18 13:56:32.305 27844-27844/com.example.deneebo.ffconnect W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6339)
08-18 13:56:32.305 27844-27844/com.example.deneebo.ffconnect W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
08-18 13:56:32.305 27844-27844/com.example.deneebo.ffconnect W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1084)
08-18 13:56:32.305 27844-27844/com.example.deneebo.ffconnect W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:945)
08-18 13:56:32.305 27844-27844/com.example.deneebo.ffconnect W/System.err: Caused by: java.lang.SecurityException: com.example.deneebo.ffconnect was not granted  this permission: android.permission.WRITE_SETTINGS.
08-18 13:56:32.305 27844-27844/com.example.deneebo.ffconnect W/System.err:     at android.os.Parcel.readException(Parcel.java:1684)
08-18 13:56:32.305 27844-27844/com.example.deneebo.ffconnect W/System.err:     at android.os.Parcel.readException(Parcel.java:1637)
08-18 13:56:32.305 27844-27844/com.example.deneebo.ffconnect W/System.err:     at android.net.wifi.IWifiManager$Stub$Proxy.setWifiApEnabled(IWifiManager.java:1888)
08-18 13:56:32.305 27844-27844/com.example.deneebo.ffconnect W/System.err:     at android.net.wifi.WifiManager.setWifiApEnabled(WifiManager.java:1748)
08-18 13:56:32.306 27844-27844/com.example.deneebo.ffconnect W/System.err:  ... 15 more

Kindly help to solve this issue.

Thanks

B001ᛦ
  • 2,036
  • 6
  • 23
  • 31

2 Answers2

2

Finally, i got the solution from the Google's developers website

https://developer.android.com/reference/android/Manifest.permission.html#WRITE_SETTINGS

And I have added following permissions in manifest file

 <uses-permission android:name="android.permission.WRITE_SETTINGS" />
 <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />

and add the following lines in my code

>  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
>             if (!Settings.System.canWrite(getApplicationContext())) {
>                 Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" +
> getPackageName()));
>                 startActivityForResult(intent, 200);
> 
>             }
>         }

Thank you so much for your help Mr.lorenzo-s

0

The issue, from the log, is this:

Caused by: java.lang.SecurityException: com.example.deneebo.ffconnect was not granted  this permission: android.permission.WRITE_SETTINGS

Your app requires the android.permission.WRITE_SETTINGS permission, and in Marshmallow this permission cannot be granted by simply declaring it in the manifest. This is a change they did to enforce security.

You have to ask your user to allow your app to use that permission explicitly. You can find more info in this answer.


This reply has been edited after OP posted his logs

lorenzo-s
  • 16,603
  • 15
  • 54
  • 86