I know how to turn on/off wifi hot spot using reflection in android using below method.
private static boolean changeWifiHotspotState(Context context,boolean enable) {
try {
WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
Method method = manager.getClass().getDeclaredMethod("setWifiApEnabled", WifiConfiguration.class,
Boolean.TYPE);
method.setAccessible(true);
WifiConfiguration configuration = enable ? getWifiApConfiguration(manager) : null;
boolean isSuccess = (Boolean) method.invoke(manager, configuration, enable);
return isSuccess;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
But the above method is not working Android 8.0(Oreo).
When I execute above method in Android 8.0, I am getting below statement in logcat.
com.gck.dummy W/WifiManager: com.gck.dummy attempted call to setWifiApEnabled: enabled = true
Is there any other way to on/off hotspot on android 8.0
Finally I got the solution. Android 8.0, they provided public api to turn on/off hotspot. WifiManager
Below is the code to turn on hotspot
onStarted(WifiManager.LocalOnlyHotspotReservation reservation)
method will be called if hotspot is turned on.. UsingWifiManager.LocalOnlyHotspotReservation
reference you callclose()
method to turn off hotspot.Note: To turn on hotspot, the
Location(GPS)
should be enabled in the device. Otherwise, it will throwSecurityException