Android 实现WiFi列表显示与连接,2021最新Android开发面试解答
2021/12/3 23:21:05
本文主要是介绍Android 实现WiFi列表显示与连接,2021最新Android开发面试解答,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
new Thread(new refreshWifiThread()).start();
break;
default:
break;
}
}
}
public void getAllNetWorkList() {
wifiArray = new ArrayList();
if (sb != null) {
sb = new StringBuffer();
}
wiFiAdmin.startScan();
list = wiFiAdmin.getWifiList();
if (list != null) {
for (int i = 0; i < list.size(); i++) {
mScanResult = list.get(i);
WifiInfo wifiInfo = new WifiInfo(mScanResult.BSSID,
mScanResult.SSID, mScanResult.capabilities,
mScanResult.level);
wifiArray.add(wifiInfo);
}
wifiInfoAdapter = new WiFiInfoAdapter(getApplicationContext(),
wifiArray);
listWifi.setAdapter(wifiInfoAdapter);
//
wiFiAdmin.getConfiguration();
listWifi.setOnItemClickListener(new AdapterView.OnItemClickListener() {
String wifiItemSSID = null;
public void onItemClick(android.widget.AdapterView<?> parent,
android.view.View view, int position, long id) {
Log.d(Constant.TAG, “BSSID:” + list.get(position).BSSID);
// 连接WiFi
wifiItemSSID = list.get(position).SSID;
int wifiItemId = wiFiAdmin.IsConfiguration("""
- list.get(position).SSID + “”");
if (wifiItemId != -1) {
if (wiFiAdmin.ConnectWifi(wifiItemId)) {
// 连接已保存密码的WiFi
Toast.makeText(getApplicationContext(), “正在连接”,
Toast.LENGTH_SHORT).show();
updateButton.setVisibility(View.INVISIBLE);
updat
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
eProgress.setVisibility(View.VISIBLE);
new Thread(new refreshWifiThread()).start();
}
} else {
// 没有配置好信息,配置
WifiPswDialog pswDialog = new WifiPswDialog(
WifiListActivity.this,
new OnCustomDialogListener() {
@Override
public void back(String str) {
wifiPassword = str;
if (wifiPassword != null) {
int netId = wiFiAdmin
.AddWifiConfig(list,
wifiItemSSID,
wifiPassword);
if (netId != -1) {
wiFiAdmin.getConfiguration();// 添加了配置信息,要重新得到配置信息
if (wiFiAdmin
.ConnectWifi(netId)) {
// 连接成功,刷新UI
updateProgress
.setVisibility(View.VISIBLE);
updateButton
.setVisibility(View.INVISIBLE);
new Thread(
new refreshWifiThread())
.start();
}
} else {
// 网络连接错误
}
} else {
}
}
});
pswDialog.show();
}
}
});
}
}
}
然后是WiFiAdmin:
package com.tchip.carlauncher.util;
import java.util.List;
import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;
import android.util.Log;
public class WifiAdmin {
/**
- 定义一个WifiManager对象,提供Wifi管理的各种主要API,主要包含wifi的扫描、建立连接、配置信息等
*/
private WifiManager mWifiManager;
// WIFIConfiguration描述WIFI的链接信息,包括SSID、SSID隐藏、password等的设置
private List wifiConfigList;
// 定义一个WifiInfo对象
private WifiInfo mWifiInfo;
// 扫描出的网络连接列表
private List mWifiList;
// 网络连接列表
private List mWifiConfigurations;
WifiLock mWifiLock;
public static final int TYPE_NO_PASSWD = 0x11;
public static final int TYPE_WEP = 0x12;
public static final int TYPE_WPA = 0x13;
public WifiAdmin(Context context) {
// 获得WifiManager对象
mWifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
// 取得WifiInfo对象
mWifiInfo = mWifiManager.getConnectionInfo();
}
/**
- 打开wifi
*/
public void openWifi() {
if (!mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(true);
}
}
/**
- 关闭wifi
*/
public void closeWifi() {
if (!mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(false);
}
}
/**
-
检查当前wifi状态
-
@return
*/
public int checkState() {
return mWifiManager.getWifiState();
}
/**
- 锁定wifiLock
*/
public void acquireWifiLock() {
mWifiLock.acquire();
}
/**
- 解锁wifiLock
*/
public void releaseWifiLock() {
// 判断是否锁定
if (mWifiLock.isHeld()) {
mWifiLock.acquire();
}
}
/**
- 创建一个wifiLock
*/
public void createWifiLock() {
mWifiLock = mWifiManager.createWifiLock(“test”);
}
/**
-
得到配置好的网络
-
@return
*/
public List getWiFiConfiguration() {
return mWifiConfigurations;
}
/**
-
指定配置好的网络进行连接
-
@param index
*/
public void connetionConfiguration(int index) {
if (index > mWifiConfigurations.size()) {
return;
}
// 连接配置好指定ID的网络
mWifiManager.enableNetwork(mWifiConfigurations.get(index).networkId,
true);
}
public void startScan() {
mWifiManager.startScan();
// 得到扫描结果
mWifiList = mWifiManager.getScanResults();
// 得到配置好的网络连接
mWifiConfigurations = mWifiManager.getConfiguredNetworks();
}
/**
-
得到网络列表
-
@return
*/
public List getWifiList() {
return mWifiList;
}
/**
-
查看扫描结果
-
@return
*/
public StringBuffer lookUpScan() {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < mWifiList.size(); i++) {
sb.append(“Index_” + new Integer(i + 1).toString() + “:”);
// 将ScanResult信息转换成一个字符串包
// 其中把包括:BSSID、SSID、capabilities、frequency、level
sb.append((mWifiList.get(i)).toString()).append("\n");
}
return sb;
}
public String getMacAddress() {
return (mWifiInfo == null) ? “NULL” : mWifiInfo.getMacAddress();
}
public String getBSSID() {
return (mWifiInfo == null) ? “NULL” : mWifiInfo.getBSSID();
}
public int getIpAddress() {
return (mWifiInfo == null) ? 0 : mWifiInfo.getIpAddress();
}
/**
-
得到连接的ID
-
@return
*/
public int getNetWordId() {
return (mWifiInfo == null) ? 0 : mWifiInfo.getNetworkId();
}
/**
-
得到wifiInfo的所有信息
-
@return
*/
public String getWifiInfo() {
return (mWifiInfo == null) ? “NULL” : mWifiInfo.toString();
}
/**
-
添加一个网络并连接
-
@param configuration
*/
public void addNetwork(WifiConfiguration configuration) {
int wcgId = mWifiManager.addNetwork(configuration);
mWifiManager.enableNetwork(wcgId, true);
}
public WifiConfiguration createWifiInfo(String SSID, String Password,
int Type) {
WifiConfiguration config = new WifiConfiguration();
config.allowedAuthAlgorithms.clear();
config.allowedGroupCiphers.clear();
config.allowedKeyManagement.clear();
config.allowedPairwiseCiphers.clear();
config.allowedProtocols.clear();
config.SSID = “”" + SSID + “”";
WifiConfiguration tempConfig = this.isExsits(SSID);
if (tempConfig != null) {
mWifiManager.removeNetwork(tempConfig.networkId);
}
if (Type == 1) // WIFICIPHER_NOPASS
{
config.wepKeys[0] = “”;
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
}
if (Type == 2) // WIFICIPHER_WEP
{
config.hiddenSSID = true;
config.wepKeys[0] = “”" + Password + “”";
config.allowedAuthAlgorithms
.set(WifiConfiguration.AuthAlgorithm.SHARED);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
config.allowedGroupCiphers
.set(WifiConfiguration.GroupCipher.WEP104);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
}
if (Type == 3) // WIFICIPHER_WPA
{
config.preSharedKey = “”" + Password + “”";
config.hiddenSSID = true;
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.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.CCMP);
config.status = WifiConfiguration.Status.ENABLED;
}
return config;
}
private WifiConfiguration isExsits(String ssid) {
WifiConfiguration config = new WifiConfiguration();
if (mWifiList.size() > 0) {
for (int i = 0; i < mWifiList.size(); i++) {
if (mWifiList.get(i).SSID.equals(ssid)) {
config = mWifiConfigurations.get(i);
}
}
}
return config;
}
/**
-
断开指定ID的网络
-
@param netId
*/
public void disConnectionWifi(int netId) {
mWifiManager.disableNetwork(netId);
mWifiManager.disconnect();
}
// ============
/**
-
连接指定Id的WIFI
-
@param wifiId
-
@return
*/
public boolean ConnectWifi(int wifiId) {
for (int i = 0; i < wifiConfigList.size(); i++) {
WifiConfiguration wifi = wifiConfigList.get(i);
if (wifi.networkId == wifiId) {
while (!(mWifiManager.enableNetwork(wifiId, true))) {// 激活该Id,建立连接
Log.i(“ConnectWifi”,
String.valueOf(wifiConfigList.get(wifiId).status));// status:0–已经连接,1–不可连接,2–可以连接
}
return true;
}
}
return false;
}
/**
- 得到Wifi配置好的信息
*/
public void getConfiguration() {
wifiConfigList = mWifiManager.getConfiguredNetworks();// 得到配置好的网络信息
for (int i = 0; i < wifiConfigList.size(); i++) {
Log.i(“getConfiguration”, wifiConfigList.get(i).SSID);
Log.i(“getConfiguration”,
String.valueOf(wifiConfigList.get(i).networkId));
}
}
/**
-
判定指定WIFI是否已经配置好,依据WIFI的地址BSSID,返回NetId
-
@param SSID
-
@return
*/
public int IsConfiguration(String SSID) {
Log.i(“IsConfiguration”, String.valueOf(wifiConfigList.size()));
for (int i = 0; i < wifiConfigList.size(); i++) {
Log.i(wifiConfigList.get(i).SSID,
String.valueOf(wifiConfigList.get(i).networkId));
if (wifiConfigList.get(i).SSID.equals(SSID)) {// 地址相同
return wifiConfigList.get(i).networkId;
}
}
return -1;
}
/**
-
添加指定WIFI的配置信息,原列表不存在此SSID
-
@param wifiList
-
@param ssid
-
@param pwd
-
@return
*/
public int AddWifiConfig(List wifiList, String ssid, String pwd) {
int wifiId = -1;
for (int i = 0; i < wifiList.size(); i++) {
ScanResult wifi = wifiList.get(i);
if (wifi.SSID.equals(ssid)) {
Log.i(“AddWifiConfig”, “equals”);
WifiConfiguration wifiCong = new WifiConfiguration();
wifiCong.SSID = “”" + wifi.SSID + “”";// “转义字符,代表”
wifiCong.preSharedKey = “”" + pwd + “”";// WPA-PSK密码
wifiCong.hiddenSSID = false;
wifiCong.status = WifiConfiguration.Status.ENABLED;
wifiId = mWifiManager.addNetwork(wifiCong);// 将配置好的特定WIFI密码信息添加,添加完成后默认是不激活状态,成功返回ID,否则为-1
if (wifiId != -1) {
return wifiId;
}
}
}
return wifiId;
}
}
密码对话框比较简单:
package com.tchip.carlauncher.ui.dialog;
import com.tchip.carlauncher.R;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
这篇关于Android 实现WiFi列表显示与连接,2021最新Android开发面试解答的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-01-18android.permission.read_media_video
- 2024-01-18android_getaddrinfo failed eai_nodata
- 2024-01-18androidmo
- 2024-01-15Android下三种离屏渲染技术
- 2024-01-09Android 蓝牙使用
- 2024-01-06Android对接华为AI - 文本识别
- 2023-11-15代码安全之代码混淆及加固(Android)
- 2023-11-10简述Android语音播报TTS
- 2023-11-06Android WiFi工具类
- 2023-07-22Android开发未来的出路