WindySky / WIFI / android WIFI连接开发

分享

   

android WIFI连接开发

2016-07-15  WindySky

首先附上一个工具类,有各种WIFI操作方法:

 

001.public class WifiAdmin {
002.// 定义WifiManager对象 
003.private WifiManager mWifiManager;
004.// 定义WifiInfo对象 
005.private WifiInfo mWifiInfo;
006.// 扫描出的网络连接列表 
007.private List<ScanResult> mWifiList;
008.// 网络连接列表 
009.private List<WifiConfiguration> mWifiConfiguration;
010.// 定义一个WifiLock 
011.WifiLock mWifiLock;
012. 
013.// 构造器 
014.public WifiAdmin(Context context) {
015.// 取得WifiManager对象 
016.mWifiManager = (WifiManager) context
017..getSystemService(Context.WIFI_SERVICE);
018.// 取得WifiInfo对象 
019.mWifiInfo = mWifiManager.getConnectionInfo();
020.}
021. 
022.// 打开WIFI 
023.public void openWifi() {
024.if (!mWifiManager.isWifiEnabled()) {
025.mWifiManager.setWifiEnabled(true);
026.}
027.}
028. 
029.// 关闭WIFI 
030.public void closeWifi() {
031.if (mWifiManager.isWifiEnabled()) {
032.mWifiManager.setWifiEnabled(false);
033.}
034.}
035. 
036.// 检查当前WIFI状态 
037.public int checkState() {
038.return mWifiManager.getWifiState();
039.}
040. 
041.// 锁定WifiLock 
042.public void acquireWifiLock() {
043.mWifiLock.acquire();
044.}
045. 
046.// 解锁WifiLock 
047.public void releaseWifiLock() {
048.// 判断时候锁定 
049.if (mWifiLock.isHeld()) {
050.mWifiLock.acquire();
051.}
052.}
053. 
054.// 创建一个WifiLock 
055.public void creatWifiLock() {
056.mWifiLock = mWifiManager.createWifiLock("Test");
057.}
058. 
059.// 得到配置好的网络 
060.public List<WifiConfiguration> getConfiguration() {
061.return mWifiConfiguration;
062.}
063. 
064.// 指定配置好的网络进行连接 
065.public void connectConfiguration(int index) {
066.// 索引大于配置好的网络索引返回 
067.if (index > mWifiConfiguration.size()) {
068.return;
069.}
070.// 连接配置好的指定ID的网络 
071.mWifiManager.enableNetwork(mWifiConfiguration.get(index).networkId,
072.true);
073.}
074. 
075.public void startScan() {
076.mWifiManager.startScan();
077.// 得到扫描结果 
078.mWifiList = mWifiManager.getScanResults();
079.// 得到配置好的网络连接 
080.mWifiConfiguration = mWifiManager.getConfiguredNetworks();
081.}
082. 
083.// 得到网络列表 
084.public List<ScanResult> getWifiList() {
085.return mWifiList;
086.}
087. 
088.// 查看扫描结果 
089.public StringBuilder lookUpScan() {
090.StringBuilder stringBuilder = new StringBuilder();
091.for (int i = 0; i < mWifiList.size(); i++) {
092.stringBuilder
093..append("Index_" + new Integer(i + 1).toString() + ":");
094.// 将ScanResult信息转换成一个字符串包 
095.// 其中把包括:BSSID、SSID、capabilities、frequency、level 
096.stringBuilder.append((mWifiList.get(i)).toString());
097.stringBuilder.append("/n");
098.}
099.return stringBuilder;
100.}
101. 
102.// 得到MAC地址 
103.public String getMacAddress() {
104.return (mWifiInfo == null) ? "NULL" : mWifiInfo.getMacAddress();
105.}
106. 
107.// 得到接入点的BSSID 
108.public String getBSSID() {
109.return (mWifiInfo == null) ? "NULL" : mWifiInfo.getBSSID();
110.}
111. 
112.// 得到IP地址 
113.public int getIPAddress() {
114.return (mWifiInfo == null) ? 0 : mWifiInfo.getIpAddress();
115.}
116. 
117.// 得到连接的ID 
118.public int getNetworkId() {
119.return (mWifiInfo == null) ? 0 : mWifiInfo.getNetworkId();
120.}
121. 
122.// 得到WifiInfo的所有信息包 
123.public String getWifiInfo() {
124.return (mWifiInfo == null) ? "NULL" : mWifiInfo.toString();
125.}
126. 
127.// 添加一个网络并连接 
128.public void addNetwork(WifiConfiguration wcg) {
129.int wcgID = mWifiManager.addNetwork(wcg);
130.boolean b =  mWifiManager.enableNetwork(wcgID, true);
131.System.out.println("a--" + wcgID);
132.System.out.println("b--" + b);
133.}
134. 
135.// 断开指定ID的网络 
136.public void disconnectWifi(int netId) {
137.mWifiManager.disableNetwork(netId);
138.mWifiManager.disconnect();
139.}
140.}

然后是一个实际应用方法,只验证过没有密码的情况:

 

01.WifiConfiguration CreateWifiInfo(String SSID, String Pass<a href="" target="_blank" class="keylink">word</a>, int Type)
02.{
03.WifiConfiguration config = new WifiConfiguration();  
04.config.allowedAuthAlgorithms.clear();
05.config.allowedGroupCiphers.clear();
06.config.allowedKeyManagement.clear();
07.config.allowedPairwiseCiphers.clear();
08.config.allowedProtocols.clear();
09.config.SSID = "\"" + SSID + "\"";  
10.if(Type == Data.WIFICIPHER_NOPASS)
11.{
12.config.wepKeys[0] = "";
13.config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
14.config.wepTxKeyIndex = 0;
15.}
16.if(Type == Data.WIFICIPHER_WEP)
17.{
18.config.hiddenSSID = true;
19.config.wepKeys[0]= "\""+Pass<a href="" target="_blank" class="keylink">word</a>+"\"";
20.config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
21.config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
22.config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
23.config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
24.config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
25.config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
26.config.wepTxKeyIndex = 0;
27.}
28.if(Type == Data.WIFICIPHER_WPA)
29.{
30.config.preSharedKey = "\""+Password+"\"";
31.config.hiddenSSID = true;  
32.config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);  
33.config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);                        
34.config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);                        
35.config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);                   
36.config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);                     
37.config.status = WifiConfiguration.Status.ENABLED;  
38.}
39. 
40.return config;
41.}
42. 
43. 
44.}

分为三种情况:1没有密码2用wep加密3用wpa加密

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。

    来自: WindySky > 《WIFI》

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多
    喜欢该文的人也喜欢 更多

    ×
    ×

    ¥.00

    微信或支付宝扫码支付:

    开通即同意《个图VIP服务协议》

    全部>>