如何在marshmallow和Android版本以编程方式启用移动热点?

时间:2017-08-18 07:32:39

标签: android

我尝试使用以下代码在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;
}

}

而且我已经允许在我的清单文件中访问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" />

但它没有在棉花糖及以上版本中激活。

每当我启用热点时,我都会收到此错误

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

请帮助解决这个问题。

由于

2 个答案:

答案 0 :(得分:2)

最后,我从Google的开发者网站获得了解决方案

https://developer.android.com/reference/android/Manifest.permission.html#WRITE_SETTINGS

我在清单文件中添加了以下权限

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

并在我的代码中添加以下行

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

非常感谢你帮助Mr.lorenzo-s

答案 1 :(得分:0)

问题来自日志:

Caused by: java.lang.SecurityException: com.example.deneebo.ffconnect was not granted  this permission: android.permission.WRITE_SETTINGS

您的应用需要android.permission.WRITE_SETTINGS权限,而在Marshmallow中,只需在清单中声明该权限即可授予此权限。这是他们为加强安全所做的改变。

您必须要求您的用户允许您的应用明确使用该权限。您可以在 this answer 中找到更多信息。

此回复已在OP发布日志后进行了编辑