huzhenv 2011-05-04
首先需要强调一点的是wifi只能在真机上测试!
在Android里,所有的wifi操作都在android.net.wifi包里,而常用的类也就只用几个,对于一般的开发人员来说,常用的操作也是有限的,不需要刻意的去记,用的时候现查就ok了,下面是《Android应用开发揭秘》里面关于wifi的范例,个人感觉还挺齐全,跟大家分享一下!
viewplaincopytoclipboardprint?
packagecom.yarin.android.Examples_08_08;
importjava.util.List;
importandroid.content.Context;
importandroid.net.wifi.ScanResult;
importandroid.net.wifi.WifiConfiguration;
importandroid.net.wifi.WifiInfo;
importandroid.net.wifi.WifiManager;
importandroid.net.wifi.WifiManager.WifiLock;
publicclassWifiAdmin
{
//定义WifiManager对象
privateWifiManagermWifiManager;
//定义WifiInfo对象
privateWifiInfomWifiInfo;
//扫描出的网络连接列表
privateList<ScanResult>mWifiList;
//网络连接列表
privateList<WifiConfiguration>mWifiConfiguration;
//定义一个WifiLock
WifiLockmWifiLock;
//构造器
publicWifiAdmin(Contextcontext)
{
//取得WifiManager对象
mWifiManager=(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
//取得WifiInfo对象
mWifiInfo=mWifiManager.getConnectionInfo();
}
//打开WIFI
publicvoidOpenWifi()
{
if(!mWifiManager.isWifiEnabled())
{
mWifiManager.setWifiEnabled(true);
}
}
//关闭WIFI
publicvoidCloseWifi()
{
if(!mWifiManager.isWifiEnabled())
{
mWifiManager.setWifiEnabled(false);
}
}
//锁定WifiLock
publicvoidAcquireWifiLock()
{
mWifiLock.acquire();
}
//解锁WifiLock
publicvoidReleaseWifiLock()
{
//判断时候锁定
if(mWifiLock.isHeld())
{
mWifiLock.acquire();
}
}
//创建一个WifiLock
publicvoidCreatWifiLock()
{
mWifiLock=mWifiManager.createWifiLock("Test");
}
//得到配置好的网络
publicList<WifiConfiguration>GetConfiguration()
{
returnmWifiConfiguration;
}
//指定配置好的网络进行连接
publicvoidConnectConfiguration(intindex)
{
//索引大于配置好的网络索引返回
if(index>mWifiConfiguration.size())
{
return;
}
//连接配置好的指定ID的网络
mWifiManager.enableNetwork(mWifiConfiguration.get(index).networkId,true);
}
publicvoidStartScan()
{
mWifiManager.startScan();
//得到扫描结果
mWifiList=mWifiManager.getScanResults();
//得到配置好的网络连接
mWifiConfiguration=mWifiManager.getConfiguredNetworks();
}
//得到网络列表
publicList<ScanResult>GetWifiList()
{
returnmWifiList;
}
//查看扫描结果
publicStringBuilderLookUpScan()
{
StringBuilderstringBuilder=newStringBuilder();
for(inti=0;i<mWifiList.size();i++)
{
stringBuilder.append("Index_"+newInteger(i+1).toString()+":");
//将ScanResult信息转换成一个字符串包
//其中把包括:BSSID、SSID、capabilities、frequency、level
stringBuilder.append((mWifiList.get(i)).toString());
stringBuilder.append("\n");
}
returnstringBuilder;
}
//得到MAC地址
publicStringGetMacAddress()
{
return(mWifiInfo==null)?"NULL":mWifiInfo.getMacAddress();
}
//得到接入点的BSSID
publicStringGetBSSID()
{
return(mWifiInfo==null)?"NULL":mWifiInfo.getBSSID();
}
//得到IP地址
publicintGetIPAddress()
{
return(mWifiInfo==null)?0:mWifiInfo.getIpAddress();
}
//得到连接的ID
publicintGetNetworkId()
{
return(mWifiInfo==null)?0:mWifiInfo.getNetworkId();
}
//得到WifiInfo的所有信息包
publicStringGetWifiInfo()
{
return(mWifiInfo==null)?"NULL":mWifiInfo.toString();
}
//添加一个网络并连接
publicvoidAddNetwork(WifiConfigurationwcg)
{
intwcgID=mWifiManager.addNetwork(wcg);
mWifiManager.enableNetwork(wcgID,true);
}
//断开指定ID的网络
publicvoidDisconnectWifi(intnetId)
{
mWifiManager.disableNetwork(netId);
mWifiManager.disconnect();
}
}
ScanResultsr=resultList.get(position);
WifiConfigurationwc=newWifiConfiguration();
wc.BSSID=sr.BSSID;
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wc.status=WifiConfiguration.Status.ENABLED;
wc.networkId=wifiManager.addNetwork(wc);
wifiManager.enableNetwork(wc.networkId,true);