android的wifi包其实并不复杂,就只有那么几个类,最常用的类就是WifiManager、WifiConfiguration、ScanResult、WifiInfo
这里我总结一下连接wifi的代码,供以后查阅:
主要的逻辑代码:
package dxd.netcheck.test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import android.app.Activity;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.os.Bundle;
import android.util.Log;
public class NetCheckActivity extends Activity {
WifiManager mWifiManager ;
Thread mThread = new Thread(){
@Override
public void run() {
scanWifiStation();
Log.e("NetCheckActivity","connectWifi return -->" + connectWifi("dlink-dianxin" ,"dxd4321", KeyMgmt.WPA_PSK));
super.run();
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
mThread.start();// 异步搜索,防止UI阻塞。
}
/**
* 连接wifi热点
* return -1 failure
* @return
*/
public boolean connectWifi(String ssid ,String key ,int keyMgmt){
WifiConfiguration mWifiConfiguration = new WifiConfiguration();
mWifiConfiguration.SSID = "\"" + ssid + "\""; // SSID 和 密码别忘加双引号
mWifiConfiguration.status = WifiConfiguration.Status.ENABLED;
mWifiConfiguration.preSharedKey = "\"" + key + "\"";
// 可要可不要。
/*mWifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
mWifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
mWifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
mWifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
mWifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);*/
// 这个必须要。
switch(keyMgmt){
case KeyMgmt.NONE:
mWifiConfiguration.allowedKeyManagement.set(KeyMgmt.NONE);
break ;
case KeyMgmt.WPA_PSK:
mWifiConfiguration.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
break ;
case KeyMgmt.WPA_EAP:
mWifiConfiguration.allowedKeyManagement.set(KeyMgmt.WPA_EAP);
break ;
case KeyMgmt.IEEE8021X:
mWifiConfiguration.allowedKeyManagement.set(KeyMgmt.IEEE8021X);
break ;
default :
break ;
}
int id = mWifiManager.addNetwork(mWifiConfiguration);// 添加一个网络
return mWifiManager.enableNetwork(id, true); // 别忘设置enableNetwork(),不然不会自动去获取IP。对应热点,显示的信息是“停用”
}
/**
* 打印wifi热点信息
* @param wifiScanResult
*/
public void print(List<ScanResult> wifiScanResult){
Iterator<ScanResult> iter = wifiScanResult.iterator();
Log.e("NetCheckActivity", "wifiScanResult.size = " + wifiScanResult.size()) ;
int i = 0 ;
while(iter.hasNext()){
ScanResult result = (ScanResult) iter.next();
Log.e("NetCheckActivity", "result["+(i++)+"] = " + result.toString());
}
}
/**
* 扫描wifi热点
*/
public void scanWifiStation(){
List<ScanResult> wifiScanResult = new ArrayList<ScanResult>();
Log.e("NetCheckActivity","mWifiManager.getWifiState()" + mWifiManager.getWifiState());
if(!mWifiManager.isWifiEnabled()){
// 如果当前wifi的状态不是打开状态,那么就打开它
mWifiManager.setWifiEnabled(true);
}
if(mWifiManager.startScan()){// 如果成功打开Wifi,那么就可用扫描周围的热点。
wifiScanResult = mWifiManager.getScanResults();
if(wifiScanResult == null){
boolean flag = true ;
int time = 0 ;
while(flag){// 此循环保证wifi打开,和搜索附近的wifi热点,直到收到为止。这两个功能
Log.e("NetCheckActivity", "wifiScanResult is null");
try {
Thread.sleep(2000);
time++ ;
} catch (InterruptedException e) {
e.printStackTrace();
}
wifiScanResult = mWifiManager.getScanResults();
if(wifiScanResult.size() > 0){
flag = false ;
// 这里可以发送消息给UI,让页面显示收到的wifi热点的信息。
print(wifiScanResult);
}
if(time == 5){ // 10秒还没有搜索到wifi热点的话,也不搜索了。
flag = false ;
// 这里可以发送消息给UI,让页面显示收不到wifi热点的提示。
}
}
}else{
//如果之前就能收到wifi热点的信息,那么这里就可以直接发消息给UI吧
print(wifiScanResult);
}
}
}
}当然,在wifi热点搜索这个模块,要看不同的需求,像android系统自带的wifi搜索就是一直都在搜。另外,WifiConfiguration中的加密算法、协议等等,其实还是比较重要的,但是只要不是非常重要的通信哪些,就没必要添加那么多的算法。
本文介绍了一个简单的Android应用程序示例,用于扫描并连接WiFi热点。通过使用核心类如WifiManager和WifiConfiguration,文章提供了完整的代码实现,并解释了如何配置WiFi连接参数。
1万+

被折叠的 条评论
为什么被折叠?



