热点页面:com.android.settings.TetherSettings
热点状态变化监听 [^xr热点状态变化监听]
获取热点打开/关闭状态:
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
boolean isEnable = wifiManager.isWifiApEnabled()
需要权限:android.Manifest.permission.ACCESS_WIFI_STATE
关闭热点:
8.0使用
ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
connectivityManager.stopTethering(ConnectivityManager.TETHERING_WIFI);
需要权限:android.Manifest.permission.TETHER_PRIVILEGED
7.0使用
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiApEnabled(WifiConfiguration, false)
需要权限:android.Manifest.permission.TETHER_PRIVILEGED
打开热点:
8.0使用
ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
connectivityManager.startTethering(ConnectivityManager.TETHERING_WIFI, true, callback, handler);
其中第二个参数“true”意思://待完善
第四个参数“handler”表示,第三个参数callback中的方法应该在哪个线程中运行。
需要权限:
(1)android.Manifest.permission.TETHER_PRIVILEGED
(2)系统App
7.0使用
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiApEnabled(WifiConfiguration, true)
需要权限:android.Manifest.permission.TETHER_PRIVILEGED
配置热点:
Android 8.0
对话框:com.android.settings.wifi.WifiApDialog,弹出来如下所示:
获取热点配置信息:
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wifiConfiguration = wifiManager.getWifiApConfiguration();
String networkName = wifiConfiguration.SSID;
//isEncrypted = true, 表示热点是采用WPA2加密的
boolean isEncrypted = wifiConfiguration.allowedKeyManagement.get(KeyMgmt.WPA2_PSK);
设置热点配置信息:
//设置热点不加密
wifiConfiguration.allowedKeyManagement.set(KeyMgmt.NONE);
//设置热点是加密
wifiConfiguration.allowedKeyManagement.set(KeyMgmt.WPA2_PSK);
wifiConfiguration.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
//设置密码
config.preSharedKey = password;
调用WifiManager完成配置:
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiApConfiguration(wifiConfiguration);
注意:配置信息前,热点是开着的,完成配置信息后,需要将热点关闭
热点状态变化监听
private BroadcastReceiver mHotspotApChangedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (mHotspotApChangedListener == null) {
return;
}
String action = intent.getAction() == null ? "" : intent.getAction();
if (action.equals(WifiManager.WIFI_AP_STATE_CHANGED_ACTION)) {
int state = intent.getIntExtra(WifiManager.EXTRA_WIFI_AP_STATE, 0);
if (state == WifiManager.WIFI_AP_STATE_DISABLING) {
//热点关闭中
} else if (state == WifiManager.WIFI_AP_STATE_DISABLED) {
//热点已关闭
} else if (state == WifiManager.WIFI_AP_STATE_ENABLING) {
//热点打开中
} else if (state == WifiManager.WIFI_AP_STATE_ENABLED) {
//热点已打开
}
}
}
};
IntentFilter filter = new IntentFilter(WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
mContext.registerReceiver(mHotspotApChangedReceiver, filter);
注意:注册完后会默认调用一次WifiManager.WIFI_AP_STATE_DISABLED或者WifiManager.WIFI_AP_STATE_ENABLED