Android 6.0.1 couldn't enable wifi hotspot programmatically
Asked Answered
L

5

9

When I tried to enable wifi tethering from the following code it throws the exception

java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at com.....

.... not granted this permission: android.permission.WRITE_SETTINGS

But this works fine in android 6.0 and below versions. And also tried with giving android.permission.WRITE_SETTINGS too.

Is there any limitation in accessing wifiAP in android 6.1?

Follow I attached the code sample that I used to enable hotspot.

            WifiConfiguration netConfig = new WifiConfiguration();
            netConfig.SSID = ssId;
            netConfig.preSharedKey = passkey;
            netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
            netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
            
            try {
                boolean apstatus = (Boolean) method.invoke(wifiManager, netConfig, true);
                
                for (Method isWifiApEnabledmethod : wmMethods) {
                    if (isWifiApEnabledmethod.getName().equals("isWifiApEnabled")) {
                        while (!(Boolean) isWifiApEnabledmethod.invoke(wifiManager)) {}

                        for (Method method1 : wmMethods) {
                            if (method1.getName().equals("getWifiApState")) {
                                int apstate;
                                apstate = (Integer) method1.invoke(wifiManager);
                                Log.i(TAG, "Apstate ::: " + apstate);
                            }
                        }
                    }
                }
                
                if (apstatus) {
                    Log.d(TAG, "Access Point created");
                } else {
                    Log.d(TAG, "Access Point creation failed");
                }

            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
Leery answered 18/12, 2015 at 12:19 Comment(0)
K
3

I think Android M don't support to create hotspot programmatically . You can take your Marshmallow user to settings page to create hotspot by himself. below code will help yo to go setting page.

  startActivity(
    new Intent(Settings.ACTION_SETTINGS));
Kenlee answered 19/2, 2016 at 11:55 Comment(1)
Why do you "Think"..Do you have anything that supports this? Cause I m trying to enable the hotspot in M. I have created Hotspot but I am not able to enable it. Getting Exception in softap start java.lang.IllegalStateException: command '2707 softap set wlan0 Ectype
L
2

This is not the correct way.But this fixed the issue.

Changed the target sdk version to 21. Then hotspot will start programmatically even in android 6.0.1. Think there should be a proper way to do this for android 6 and later versions. I think requesting runtime permissions needs to execute those kind of processess. This talks about the android permission requesting in runtime

Leery answered 12/1, 2016 at 5:50 Comment(1)
I had to do something similar for bluetooth scanning, set the target sdk back a bit and it emulates the old functionality, and doesn't encumber you with the new way of doing things.Boyle
F
2

Set target SDK version 21 and ask for write_settings permission in your activity. Also add android.permission.WRITE_SETTINGS permission in manifest.

if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.WRITE_SETTINGS)){

}else {
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.WRITE_SETTINGS},
            121);
}

For more details please visit http://developer.android.com/training/permissions/requesting.html

Fellow answered 29/4, 2016 at 19:46 Comment(0)
S
1

Guys I tried everything and I wasn't able to start the hotspot in Android 6.0. You can just check if Api is >= 23, and if so just take the user to settings page to create hotspot by himself.

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    final Intent intent = new Intent(Intent.ACTION_MAIN, null);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    final ComponentName cn = new ComponentName(
                   "com.android.settings",
                   "com.android.settings.TetherSettings");
    intent.setComponent(cn);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity( intent);
}else{
   createhotSpot();
}
Superannuate answered 27/12, 2016 at 9:11 Comment(2)
I've written an app that turns on the wifi hotspot based on location services (so for example it goes on when I'm home) the whole point of programming is to automate things so the user doesn't have to do anything. Any change google has un-rescinded the functionality?Boyle
@Boyle I deployed that application, and I wasn't able to find a way how to start wifi without user interaction. And yeah programming after all is a meant for automatization without user interaction but google is dictating the game here ;)Superannuate
B
0

Permission is not your problem. You need something like this 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); //You need a callback for activity result so that check if user enabled this feature, go for starting hotspot (google for it)
} else {
    // Do your stuff about starting hotspot (in network thread)
}

}

Banjo answered 5/1, 2018 at 10:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.