最近项目需要一个开启WiFi热点的功能,由于Android 7.1实现机制跟之前版本有所不同,在网上搜索了很久终于解决了这个问题,特意把过程记录下来,希望大家不要踩坑。
开启WiFi热点函数 主要参考博客 : https://blog.csdn.net/zhang01/article/details/79222057
可以用一个按钮来触发开启热点。
public boolean setWifiApEnabled(boolean enabled) {
if (enabled) { // disable WiFi in any case
wifiManager.setWifiEnabled(false);
}
wifiManager = (WifiManager)getApplicationContext().getSystemService(WIFI_SERVICE);
mContext = this;
handler = new Handler(getMainLooper());
try {
mResultReceiver = new ResultReceiver(handler);
WifiConfiguration apConfig = new WifiConfiguration();
apConfig.SSID = "asdf";
apConfig.preSharedKey = "12121212";
apConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
if (Build.VERSION.SDK_INT >= Bu