我尝试使用以下代码在Android Marshmallow中创建WiFi热点:
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;
}
同时,我已经在我的清单文件(manifest file)中授予了访问Wi-Fi的所有权限。
<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" />
但在Marshmallow及以上版本中,它无法激活。
每当我启用热点时,我都会收到此错误。
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
请帮忙解决这个问题。
谢谢。