I test these codes in Samsung J200F it's working best but when i test these codes in Lenovo 7.0 Android Nought they does not working. There is no any exceptions in Logcat. I am already added these Permissions at runtime and AndroidManifest
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
There is my codes for turning on/off my Wifi Hotspot.
public class HotspotControl {
private static Method getWifiApConfiguration;
private static Method getWifiApState;
private static Method isWifiApEnabled;
private static Method setWifiApEnabled;
private static Method setWifiApConfiguration;
private WifiManager wm;
private String deviceName;
private WifiManager.LocalOnlyHotspotReservation mReservation;
private static HotspotControl instance = null;
static {
for (Method method : WifiManager.class.getDeclaredMethods()) {
switch (method.getName()) {
case "getWifiApConfiguration":
getWifiApConfiguration = method;
break;
case "getWifiApState":
getWifiApState = method;
break;
case "isWifiApEnabled":
isWifiApEnabled = method;
break;
case "setWifiApEnabled":
setWifiApEnabled = method;
break;
case "setWifiApConfiguration":
setWifiApConfiguration = method;
break;
}
}
}
private HotspotControl(Context context) {
wm = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
}
public boolean turnOnPreOreoHotspot(String name) {
wm.setWifiEnabled(false);
//Create new Open Wifi Configuration
WifiConfiguration wifiConf = new WifiConfiguration();
wifiConf.SSID = "\"" + name + "\"";
wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wm.addNetwork(wifiConf);
wm.saveConfiguration();
return setHotspotEnabled(wifiConf, true);
}
private boolean setHotspotEnabled(WifiConfiguration config, boolean enabled) {
Object result = invokeSilently(setWifiApEnabled, wm, config, enabled);
if (result == null) {
return false;
}
return (Boolean) result;
}
private static Object invokeSilently(Method method, Object receiver, Object... args) {
try {
return method.invoke(receiver, args);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
Log.e(TAG, "exception in invoking methods: " + e.getMessage());
}
return null;
}
}
Can you help me in this situation. Thanks in advance.