我尝试使用以下代码在Android Marshmallow中创建wifi tethering Hotspot.
public class WifiAccessManager { private static final String SSID = "1234567890abcdef"; 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(WifiConfiguration.KeyMgmt.NONE); return conf; } }
}
但它显示以下权限问题..
java.lang.SecurityException: googleplus.tarun.info.hotspotcreation was not granted either of these permissions: android.permission.CHANGE_NETWORK_STATE, android.permission.WRITE_SETTINGS.
但我已经在清单上添加了那些..
我该如何解决这个问题?