当我尝试在android设备api> = 23上运行android wifi热点programmaticaly时,它不起作用,直到我降低targetSdkVersion小于23.我想这是因为Marshmallow中引入的运行时权限。但我没有找到什么样的运行时权限我应该要求打开/关闭wifi热点。我用这个类控制wifi热点:
public class ApManager {
//check whether wifi hotspot on or off
public static boolean isApOn(Context context) {
WifiManager wifimanager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
try {
Method method = wifimanager.getClass().getDeclaredMethod("isWifiApEnabled");
method.setAccessible(true);
return (Boolean) method.invoke(wifimanager);
}
catch (Throwable ignored) {}
return false;
}
// toggle wifi hotspot on
public static boolean turnHotSpotOn(Context context) {
if(isApOn(context)) {
return true;
}
WifiManager wifimanager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wc = new WifiConfiguration();
wc.allowedAuthAlgorithms.clear();
wc.allowedGroupCiphers.clear();
wc.allowedKeyManagement.clear();
wc.allowedPairwiseCiphers.clear();
wc.allowedProtocols.clear();
wc.SSID = "mynetwork";
wc.preSharedKey = "mypassword";
wc.status = WifiConfiguration.Status.ENABLED;
wc.hiddenSSID = false;
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
int netId = wifimanager.addNetwork(wc);
if (netId != -1 ) {
wifimanager.enableNetwork(netId, true);
}
wifimanager.saveConfiguration();
try {
// if WiFi is on, turn it off
if (wifimanager.isWifiEnabled()){
wifimanager.setWifiEnabled(false);
}
Method method = wifimanager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
method.invoke(wifimanager, wc, true);
return true;
}
catch (Exception e) {
e.printStackTrace();
}
return false;
}
// toggle wifi hotspot off
public static boolean turnHotSpotOff(Context context) {
if(!isApOn(context)) {
return true;
}
WifiManager wifimanager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
try {
Method method = wifimanager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
method.invoke(wifimanager, null, false);
return true;
}
catch (Exception e) {
e.printStackTrace();
}
return false;
}}
答案 0 :(得分:-1)
我明白了。要在Marshmallow及以上版本上使用WifiHotSpot,您应该使用此代码来请求写入权限:
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);
}
}
Manifest.xml权限:
<uses-permission
android:required="true"
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission
android:required="true"
android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:required="true"
android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission
android:required="true"
android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission
android:required="true"
android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission
android:required="true"
android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission
android:required="true"
android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission
android:required="true"
android:name="android.permission.INTERNET"/>
<uses-permission
android:required="true"
android:name="android.permission.WRITE_SETTINGS" />