Skip to content Skip to sidebar Skip to footer

Android 6.0.1 Couldn't Enable Wifi Hotspot Programmatically

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)

Solution 1:

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

Solution 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,
            newString[]{Manifest.permission.WRITE_SETTINGS},
            121);
}

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

Solution 3:

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) {
    finalIntentintent=newIntent(Intent.ACTION_MAIN, null);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    finalComponentNamecn=newComponentName(
                   "com.android.settings",
                   "com.android.settings.TetherSettings");
    intent.setComponent(cn);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity( intent);
}else{
   createhotSpot();
}

Solution 4:

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())) {
    Intentintent=newIntent(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)
}

}

Post a Comment for "Android 6.0.1 Couldn't Enable Wifi Hotspot Programmatically"