Why does this code not work in android 5.0 ?
为什么这个代码在android 5.0中不起作用?
What methods I should call to turn it On/Off in Android 5.0 ?
我应该用什么方法在Android 5.0中打开/关闭它?
WifiConfiguration wificonfiguration = new WifiConfiguration();
wificonfiguration.SSID = "Wifi Hotspot";
wificonfiguration.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
wificonfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wificonfiguration.preSharedKey = "123";
WifiManager mWifiManager;
mWifiManager = (WifiManager) this.context1.getSystemService(Context.WIFI_SERVICE);
try {
if (mWifiManager.isWifiEnabled()) { // disable WiFi in any case
mWifiManager.setWifiEnabled(false);
}
Method method = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
method.invoke(mWifiManager, wificonfiguration, true);
//Toast.makeText(context, "OK", 0).show();
} catch (Exception e) {
Log.e(this.getClass().toString(), "", e);
}
Add in Manifest:
加入清单:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
1 个解决方案
#1
0
You should catch specific exceptions to know what went wrong more clearly . Try this -
您应该捕获特定的例外情况,以便更清楚地了解哪些问题。试试这个 -
private boolean setWifiApEnabled()
{
boolean result = false;
// initialise you wifiManager first
wifiManager.setWifiEnabled(false);
Method enableWifi;
try {
enableWifi = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
} catch (NoSuchMethodException e) {
Logger.e(TAG,e.toString());
return result;
}
WifiConfiguration myConfig = new WifiConfiguration();
myConfig.SSID = "Your SSID";
myConfig.preSharedKey = "Your pass";
myConfig.status = WifiConfiguration.Status.ENABLED;
myConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
myConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
myConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
myConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
myConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
myConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
myConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
try {
result = (Boolean) enableWifi.invoke(wifiManager, myConfig,status);
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
Logger.e(TAG,e.toString());
return result;
}
return result;
}
#1
0
You should catch specific exceptions to know what went wrong more clearly . Try this -
您应该捕获特定的例外情况,以便更清楚地了解哪些问题。试试这个 -
private boolean setWifiApEnabled()
{
boolean result = false;
// initialise you wifiManager first
wifiManager.setWifiEnabled(false);
Method enableWifi;
try {
enableWifi = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
} catch (NoSuchMethodException e) {
Logger.e(TAG,e.toString());
return result;
}
WifiConfiguration myConfig = new WifiConfiguration();
myConfig.SSID = "Your SSID";
myConfig.preSharedKey = "Your pass";
myConfig.status = WifiConfiguration.Status.ENABLED;
myConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
myConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
myConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
myConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
myConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
myConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
myConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
try {
result = (Boolean) enableWifi.invoke(wifiManager, myConfig,status);
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
Logger.e(TAG,e.toString());
return result;
}
return result;
}