[英]How to enable mobile hotspot programmatically in marshmallow and above android version?
I've tried to create wifi tethering Hotspot in Android Marshmallow using following code. 我尝试使用以下代码在Android Marshmallow中创建wifi tethering Hotspot。
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 而且我已经允许在我的清单文件中访问wifi
<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 谢谢
Finally, i got the solution from the Google's developers website 最后,我从Google的开发者网站获得了解决方案
https://developer.android.com/reference/android/Manifest.permission.html#WRITE_SETTINGS 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 非常感谢你帮助Mr.lorenzo-s
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. 您的应用需要
android.permission.WRITE_SETTINGS
权限,而在Marshmallow中,只需在清单中声明该权限即可授予此权限。 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 OP发布日志后,此回复已经过编辑
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.