Android 6.0.1 couldn't enable wifi hotspot programmatically

前端 未结 5 1858
孤城傲影
孤城傲影 2021-02-09 02:20

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

java.lang.reflect.InvocationTargetException at java.lang.reflect.Method

5条回答
  •  情话喂你
    2021-02-09 03:02

    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();
    }
    

提交回复
热议问题