我正在尝试以编程方式在Oreo及更高版本的设备上打开热点,但是不幸的是,热点在几秒钟内被关闭了。可能是什么原因?
这是我要开启的方式。
public static boolean configApState(Context context, WiFiListener listener) {
if (isOreo()) {
turnOnHotspotOreo(context, listener);
return true;
}
return false;
}
@RequiresApi(api = Build.VERSION_CODES.O)
private static void turnOnHotspotOreo(Context context, final WiFiListener listener) {
WifiManager manager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
boolean isLocationEnable = Util.isLocationEnable(context);
if (!isLocationEnable) {
// location is not enabled
return;
}
manager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {
@Override
public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
super.onStarted(reservation);
mReservation = reservation;
if (listener != null) {
listener.onSuccess();
}
}
@Override
public void onStopped() {
super.onStopped();
if (hotspotListener != null) {
hotspotListener.onStop();
}
mReservation = null;
}
@Override
public void onFailed(int reason) {
super.onFailed(reason);
if (listener != null) {
listener.onFailure(reason);
}
mReservation = null;
}
}, new Handler());
}
清单文件中有权限。
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE " />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_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" />
<uses-permission
android:name="android.permission.WRITE_SETTINGS"
tools:node="replace" />