抬头仰望星空,是否能发现自己的渺小。

伪斜杠青年

人们总是混淆了欲望和理想

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不开放,可能?大概?算了,懒得猜~ 反射虽麻烦,但也不是不行~

2019更新

过去了挺久,还是更新下,上面的东西可用 但是启动有问题,当前context销毁后,热点会关闭,要想像原生一样开启,还是需要调framework接口:

ConnectivityManager  connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
connectivityManager.startTethering(ConnectivityManager.TETHERING_WIFI,
true, new ConnectivityManager.OnStartTetheringCallback() {

@Override
public void onTetheringFailed() {
super.onTetheringFailed();
Log.d(PlatformTAG.TAG_NETWORK, "onTetheringFailed");
}

@Override
public void onTetheringStarted() {
super.onTetheringStarted();
Log.d(PlatformTAG.TAG_NETWORK, "onTetheringStarted");
}

});

原生系统设置里扣下来的,普通应用没办法用,需要导系统framework.jar,建议放中间件。


本站由以下主机服务商提供服务支持:

2条评论

  • Dong

    android 8.0执行到boolean isConfigured = (Boolean) configMethod.invoke(wifiManager, wifiCfg);时候就会报错
    java.lang.reflect.InvocationTargetException

    这是为什么呢?

    • Mosaic-C

      sdk变更了吧,估计现在已经凉凉了,三方应用不建议这样做了。

发表评论