Android 开启热点 获取热点 名和密码 适配Android O及以上
Android 开启热点
1.高于Android8.0 开启热点
下面展示一些 内联代码片 。
@RequiresApi(api = Build.VERSION_CODES.O)
private void openApSystemO(final Callbak callbak) {
if (isApOpen()) {
if (!TextUtils.isEmpty(sid) && !TextUtils.isEmpty(pwd)) {
callbak.onConnected("", sid, pwd);
return;
}
closeAp();
}
if (isApOpen()) {
Log.e("TAG","热点关闭失败");
callbak.onConnected("close wifi ap first please", null, null);
return;
}
getWifiMgr().startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {
@Override
public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
mReservation = reservation;
sid = reservation.getWifiConfiguration().SSID;
pwd = reservation.getWifiConfiguration().preSharedKey;
callbak.onConnected("", sid, pwd);
}
@Override
public void onStopped() {
mReservation = null;
sid = "";
pwd = "";
}
@Override
public void onFailed(int reason) {
sid = "";
pwd = "";
callbak.onConnected("wifi ap is failed to open", null, null);
}
}, new Handler());
}
2.低于Android8.0 开启热点 下面展示一些 内联代码片 。
private void openApBelow8(Callbak callbak) {
if (getWifiMgr().isWifiEnabled()) {
getWifiMgr().setWifiEnabled(false);
}
if (isApOpen()) {
sid = getApSSID();
pwd = getApPassword();
if (!TextUtils.isEmpty(sid) && !TextUtils.isEmpty(pwd)) {
callbak.onConnected("", sid, pwd);
return;
} else {
sid = "";
pwd = "";
}
}
WifiConfiguration config = new WifiConfiguration();
config.SSID = "Scan-Wifi";
config.preSharedKey = "Scan-Wifi@233";
config.hiddenSSID = false;
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
config.status = WifiConfiguration.Status.ENABLED;
try {
Method method = getWifiMgr().getClass().getMethod("setWifiApEnabled",
WifiConfiguration.class, Boolean.TYPE);
Boolean result = (Boolean) method.invoke(getWifiMgr(), config, true);
if (UText.isTrue(result)) {
sid = "Scan-Wifi";
pwd = "Scan-Wifi@233";
callbak.onConnected("", sid, pwd);
} else {
sid = "";
pwd = "";
callbak.onConnected("wifi ap is failed to open", null, null);
}
} catch (Exception e) {
sid = "";
pwd = "";
callbak.onConnected("wifi ap is failed to open", null, null);
}
}
3.热点是否打开 {@code true} 打开
public boolean isApOpen() {
try {
Method method = getWifiMgr().getClass().getDeclaredMethod("isWifiApEnabled");
method.setAccessible(true);
return (boolean) method.invoke(getWifiMgr());
} catch (Throwable ignored) {
return false;
}
}
4.关闭热点
public void closeAp() {
try {
if (isSystemO()) {
if (mReservation != null) {
mReservation.close();
mReservation = null;
}
} else {
Method method = getWifiMgr().getClass().getMethod("setWifiApEnabled",
WifiConfiguration.class, boolean.class);
method.invoke(getWifiMgr(), null, false);
}
} catch (Exception ignored) {
}
}
亲测Android10.0 和Android6.0
代码下载地址:https://download.csdn.net/download/qq_34754268/22910187添加链接描述
|