Workaround for Android versions >=Oreo

Worked with WifiManager's startLocalOnlyHotspotCallback
It is specifically for android versions>=O
This commit is contained in:
Adeel Zafar 2019-05-29 20:52:09 +05:00
parent c8e975f8ed
commit d04839f6ef
3 changed files with 52 additions and 8 deletions

View File

@ -32,6 +32,10 @@
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<!-- Device with versions >= Oreo need location permission to start/stop the hotspot -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:name=".KiwixApplication"
android:allowBackup="true"

View File

@ -930,14 +930,18 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
openExternalUrl(intentSupportKiwix);
case R.id.menu_wifi_hotspot:
if (wifiHotspotManager.isWifiApEnabled()) {
wifiHotspotManager.setWifiEnabled(null, false);
Toast.makeText(this, "Wifi Hotspot Disabled", Toast.LENGTH_LONG)
.show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
wifiHotspotManager.turnOnHotspot();
} else {
wifiHotspotManager.setWifiEnabled(null, true);
Toast.makeText(this, "Wifi Hotspot Enabled", Toast.LENGTH_LONG)
.show();
if (wifiHotspotManager.isWifiApEnabled()) {
wifiHotspotManager.setWifiEnabled(null, false);
Toast.makeText(this, "Wifi Hotspot Disabled", Toast.LENGTH_LONG)
.show();
} else {
wifiHotspotManager.setWifiEnabled(null, true);
Toast.makeText(this, "Wifi Hotspot Enabled", Toast.LENGTH_LONG)
.show();
}
}
default:

View File

@ -6,8 +6,10 @@ import android.net.Uri;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Handler;
import android.provider.Settings;
import android.util.Log;
import androidx.annotation.RequiresApi;
import java.lang.reflect.Method;
/**
@ -17,8 +19,9 @@ import java.lang.reflect.Method;
*/
public class WifiHotspotManager {
private final WifiManager wifiManager;
private WifiManager wifiManager;
private Context context;
private WifiManager.LocalOnlyHotspotReservation hotspotReservation;
public WifiHotspotManager(Context context) {
this.context = context;
@ -52,6 +55,39 @@ public class WifiHotspotManager {
}
}
//Workaround to turn on hotspot for Oreo versions
@RequiresApi(api = Build.VERSION_CODES.O)
public void turnOnHotspot() {
wifiManager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {
@Override
public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
super.onStarted(reservation);
//hotspotReservation = reservation;
}
@Override
public void onStopped() {
super.onStopped();
Log.v("DANG", "Local Hotspot Stopped");
}
@Override
public void onFailed(int reason) {
super.onFailed(reason);
Log.v("DANG", "Local Hotspot failed to start");
}
}, new Handler());
}
//Workaround to turn off hotspot for Oreo versions
@RequiresApi(api = Build.VERSION_CODES.O)
public void turnOffHotspot() {
if (hotspotReservation != null) {
hotspotReservation.close();
}
}
// This method returns the current state of the Wifi access point
public WIFI_AP_STATE_ENUMS getWifiApState() {
try {