1,想实现连接到指定的WiFi网络吗?
其实这个不是很难,可这个一直困扰了我很长时间,今天终于找到了原因,因为,我之前的配置就是错误的,所以,导致无法连接到指定的无线网络。
2,实现方法:
请参考以下的代码(特别是有密码与没有密码的部分,他们的配置都不尽相同,所以,一定得细心):
WifiConfiguration config = new WifiConfiguration();
config.allowedAuthAlgorithms.clear();
config.allowedGroupCiphers.clear();
config.allowedKeyManagement.clear();
config.allowedPairwiseCiphers.clear();
config.allowedProtocols.clear();
config.SSID = "\"" + SSID + "\"";
if(Type == NOPASS)
{
config.wepKeys[0] = "";
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
}
if(Type == WEP)
{
config.preSharedKey = "\""+Password+"\"";
config.hiddenSSID = true;
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 == 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.status = WifiConfiguration.Status.ENABLED;
}
3,下面是实现的具体关键代码(仅用了没有密码的实现方式,其他的可以自己去实现):
List<ScanResult> sRet = mWifi.getScanResults();
for (int i=0; i<sRet.size(); i++)
{
ScanResult retS = sRet.get(i);
txt.append("resT: " + retS.SSID +" " + retS.BSSID + "\n");
Log.d(tag, "resT: " + retS.SSID +" " + retS.BSSID);
if (retS.SSID.equalsIgnoreCase("Shuttled"))
{
txt.append("Found: " + retS.SSID +" " + retS.BSSID + "\n");
WifiConfiguration wc = new WifiConfiguration();
wc.allowedAuthAlgorithms.clear();
wc.allowedGroupCiphers.clear();
wc.allowedKeyManagement.clear();
wc.allowedProtocols.clear();
wc.allowedPairwiseCiphers.clear();
wc.SSID = "\""+retS.SSID+"\"";
wc.wepKeys[0]="";
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wc.wepTxKeyIndex = 0;
int netID = mWifi.addNetwork(wc); // add network
// txt.append("addNetwork: "+ Integer.toString(netID) +"\n");
// mWifi.saveConfiguration();
if(mWifi.enableNetwork(netID, true)) // enable network
{
txt.append("enableNetwork: true\n");
}
4,在StackOverflow上,有位高人,让通过广播来实现(通知扫描完成事件),下面是实现代码:
private BroadcastReceiver scanResultsReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent intent) {
String action = intent.getAction();
if (action.equals("android.net.wifi.SCAN_RESULTS"))
{
do connect?????
}
}
/* 注册事件时,需要添加行为 */
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.net.wifi.SCAN_RESULTS");
registerReceiver(scanResultsReceiver, intentFilter);
5,完成,将近快10月了,才知道怎么去弄,也许是关注度不够吧。唉~~~,加油。。。