概述
工作需要用到扫描附近wifi并连接到指定wifi功能,在这里做一个记录。在Android 7.0 和 8.1 都测试过都可用。代码来源网络。扫描并连接指定wifi包括以下4个步骤:
- 授权
- 扫描附近的wifi
- 注册广播监听wifi状态变化
- 连接指定的wifi
授权
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
//要定位权限才能搜索wifi
PermissionUtil.requestEach(this, new PermissionUtil.OnPermissionListener() {
@Override
public void onSucceed() {
//授权成功后打开wifi
scanWifiInfo();
}
@Override
public void onFailed(boolean showAgain) {
}
}, PermissionUtil.LOCATION);
扫描附近的wifi
/**
* 扫描附近wifi
*/
private void scanWifiInfo() {
String wserviceName = Context.WIFI_SERVICE;
WifiManager mWifiManager = (WifiManager) getSystemService(wserviceName);
mWifiManager.setWifiEnabled(true);
mWifiManager.startScan();
mWifiList.clear();
mWifiList = mWifiManager.getScanResults();
if(mWifiList != null && mWifiList.size() > 0) {
adapter = new ListAdapter(this, mWifiList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//连接wifi
ScanResult scanResult = mWifiList.get(position);
connectWifi(scanResult.SSID, "ILOVEYOU", "WPA");
}
});
}
}
注册广播监听wifi状态变化
@Override
public void onResume() {
super.onResume();
//注册广播
wifiReceiver = new WifiBroadcastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);//监听wifi是开关变化的状态
filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);//监听wifiwifi连接状态广播
filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);//监听wifi列表变化(开启一个热点或者关闭一个热点)
registerReceiver(wifiReceiver, filter);
}
@Override
public void onPause() {
super.onPause();
//取消监听
unregisterReceiver(wifiReceiver);
}
//监听wifi状态广播接收器
public class WifiBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {
int state = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
switch (state) {
/**
* WIFI_STATE_DISABLED WLAN已经关闭
* WIFI_STATE_DISABLING WLAN正在关闭
* WIFI_STATE_ENABLED WLAN已经打开
* WIFI_STATE_ENABLING WLAN正在打开
* WIFI_STATE_UNKNOWN 未知
*/
case WifiManager.WIFI_STATE_DISABLED: {
Log.i(TAG, "已经关闭");
break;
}
case WifiManager.WIFI_STATE_DISABLING: {
Log.i(TAG, "正在关闭");
break;
}
case WifiManager.WIFI_STATE_ENABLED: {
Log.i(TAG, "已经打开");
// sortScaResult();
break;
}
case WifiManager.WIFI_STATE_ENABLING: {
Log.i(TAG, "正在打开");
break;
}
case WifiManager.WIFI_STATE_UNKNOWN: {
Log.i(TAG, "未知状态");
break;
}
}
} else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
Log.i(TAG, "--NetworkInfo--" + info.toString());
if (NetworkInfo.State.DISCONNECTED == info.getState()) {//wifi没连接上
Log.i(TAG, "wifi没连接上");
} else if (NetworkInfo.State.CONNECTED == info.getState()) {//wifi连接上了
Log.i(TAG, "wifi连接上了");
} else if (NetworkInfo.State.CONNECTING == info.getState()) {//正在连接
Log.i(TAG, "wifi正在连接");
}
} else if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(intent.getAction())) {
Log.i(TAG, "网络列表变化了");
}
}
}
连接指定的wifi
/**
* 连接wifi
* @param targetSsid wifi的SSID
* @param targetPsd 密码
* @param enc 加密类型
*/
@SuppressLint("WifiManagerLeak")
public void connectWifi(String targetSsid, String targetPsd, String enc) {
// 1、注意热点和密码均包含引号,此处需要需要转义引号
String ssid = "\"" + targetSsid + "\"";
String psd = "\"" + targetPsd + "\"";
//2、配置wifi信息
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = ssid;
switch (enc) {
case "WEP":
// 加密类型为WEP
conf.wepKeys[0] = psd;
conf.wepTxKeyIndex = 0;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
break;
case "WPA":
// 加密类型为WPA
conf.preSharedKey = psd;
break;
case "OPEN":
//开放网络
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
}
//3、链接wifi
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(conf);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for (WifiConfiguration i : list) {
if (i.SSID != null && i.SSID.equals(ssid)) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
break;
}
}
}
最后附上代码的 GitHub地址
网友评论