android Turning on wifi hotspot programmatically

2.7k views Asked by At

I want to turn on wifi hotspot progmmatically. I use the following code:

public class MainActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void enableHotspot(View v){
        WifiAccessManager.setWifiApState(this, true);

    }
}

public class WifiAccessManager {

    private static final String SSID = "1234567890abcdef";
    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(WifiConfiguration.KeyMgmt.NONE);
        return conf;
    }
}

Permissions in AndroidmManifest 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.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

I'm testing it on 2 different devices(android 4 and android 6). This is working normally on android 4 (no issues) but on android 6 it does not work and the following logs are being generated:

java.lang.reflect.InvocationTargetException
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err:     at ir.captainweb.wifitests.WifiAccessManager.setWifiApState(WifiAccessManager.java:20)
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err:     at ir.captainweb.wifitests.MainActivity.enableHotspot(MainActivity.java:17)
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err:     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err:     at android.view.View.performClick(View.java:5210)
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err:     at android.view.View$PerformClick.run(View.java:21328)
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err:     at android.os.Looper.loop(Looper.java:148)
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5551)
09-02 23:04:25.053 5220-5220/ir.captainweb.wifitests W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
09-02 23:04:25.053 5220-5220/ir.captainweb.wifitests W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
09-02 23:04:25.053 5220-5220/ir.captainweb.wifitests W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
09-02 23:04:25.053 5220-5220/ir.captainweb.wifitests W/System.err: Caused by: java.lang.SecurityException: ir.captainweb.wifitests was not granted  this permission: android.permission.WRITE_SETTINGS.
09-02 23:04:25.053 5220-5220/ir.captainweb.wifitests W/System.err:     at android.os.Parcel.readException(Parcel.java:1599)
09-02 23:04:25.053 5220-5220/ir.captainweb.wifitests W/System.err:     at android.os.Parcel.readException(Parcel.java:1552)
09-02 23:04:25.053 5220-5220/ir.captainweb.wifitests W/System.err:     at android.net.wifi.IWifiManager$Stub$Proxy.setWifiApEnabled(IWifiManager.java:1540)
09-02 23:04:25.053 5220-5220/ir.captainweb.wifitests W/System.err:     at android.net.wifi.WifiManager.setWifiApEnabled(WifiManager.java:1588)
09-02 23:04:25.053 5220-5220/ir.captainweb.wifitests W/System.err:  ... 14 more

I also added WRITE_SETTINGS permission but again I have the same problem

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

There are 1 answers

1
Jitesh Mohite On

We have to give permission manually in android 6.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  // please check here whether you already have these permission or not.
        requestPermissions(new String[]{Manifest.permission.WRITE_SETTINGS}, 2909);
    }

You will get the response of permission whether it granted or not.

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case 2909: {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.e("Permission", "Granted");
            } else {
                Log.e("Permission", "Denied");
            }
            return;
        }
    }
}