Android8.0热点开启、关闭、配置热点信息


直接上代码~

获取热点开启状态:

public static boolean getApEnable(Context context) {
WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wifiManager == null) {
return false;
}
try {
Method method = wifiManager.getClass().getDeclaredMethod("isWifiApEnabled");
method.setAccessible(true);
return (Boolean) method.invoke(wifiManager);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
return false;
}

热点开启与关闭:

public static void setApEnable(Context context, boolean enable) {
try {
WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wifiManager == null) {
return;
}
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O) {
// 如果是Android 8.0系统
if (enable) {
// 打开热点
wifiManager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {

@Override
public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
super.onStarted(reservation);
Log.d(TAG, "onStarted: ");
}

@Override
public void onStopped() {
super.onStopped();
Log.d(TAG, "onStopped: ");
}

@Override
public void onFailed(int reason) {
super.onFailed(reason);
Log.d(TAG, "onFailed: ");
}
}, new Handler());
} else {
// 关闭热点
Method method = wifiManager.getClass().getDeclaredMethod("stopSoftAp");
method.invoke(wifiManager);
}
} else {
Method method = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
method.invoke(wifiManager, null, enable);
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}

}

配置带信息的热点:

/**
* 打开热点
*
* @param context 上下文
* @param SSID 名称
* @param password 密码
* @param securityType 加密类型
* @param band WIFI_FREQUENCY_BAND_5GHZ WIFI_FREQUENCY_BAND_2GHZ
* @return 开启状态
*/
private static final int WIFI_AP_2_4G_CHANNEL = 9;//2.4G热点信道
private static final int WIFI_AP_5G_CHANNEL = 149;//5G热点信道
public static boolean configAp(Context context, String SSID, String password, int securityType, int band) {
if (TextUtils.isEmpty(SSID)) {
return false;
}

WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (wifiManager != null && wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(false);
}

WifiConfiguration wifiCfg = ServerWifiMgr.createWifiCfg(SSID, password, securityType);
if (isApOn(context)) {
closeWifiAp(context);
}

Method method = null;
Field field = null;
try {
method = wifiManager.getClass().getMethod("isDualBandSupported");
//判断是否支持5G
boolean isDualBandSupported = (boolean) method.invoke(wifiManager);
LogUtils.i(TAG, "=====isDualBandSupported = " + isDualBandSupported + "====");
if (isDualBandSupported && band == 1) {
field = wifiCfg.getClass().getField("apBand");
field.setInt(wifiCfg, 1);
field = wifiCfg.getClass().getField("apChannel");
field.setInt(wifiCfg, WIFI_AP_5G_CHANNEL);
} else {
field = wifiCfg.getClass().getField("apBand");
field.setInt(wifiCfg, 0);
field = wifiCfg.getClass().getField("apChannel");
field.setInt(wifiCfg, WIFI_AP_2_4G_CHANNEL);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Method configMethod = wifiManager.getClass().getMethod("setWifiApConfiguration", WifiConfiguration.class);
boolean isConfigured = (Boolean) configMethod.invoke(wifiManager, wifiCfg);
method = wifiManager.getClass().getMethod("startSoftAp", WifiConfiguration.class);
//返回热点打开状态
return (Boolean) method.invoke(wifiManager, wifiCfg);
} else {
method = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
method.invoke(wifiManager, wifiCfg, true);
return true;
}
} catch (NoSuchMethodException | NoSuchFieldException | InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
LogUtils.i(TAG, "=====Exception==" + e.getMessage());
}
return false;
}

其实不太明白为什么Android不开放,可能?大概?算了,懒得猜~ 反射虽麻烦,但也不是不行~

声明:TIL|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA[ZH]协议进行授权

转载:转载请注明原文链接 - Android8.0热点开启、关闭、配置热点信息


Life is very interesting. In the end, some of your greatest pains become your greatest strengths.