我还是在学习阶段,只提供参考,还得根据具体情况而定
方法的实现有:
public class Activity1 {
// 定义wifimanager对象
private WifiManager mWifiManager;
// 定义WifiInfo对象
private WifiInfo mWifiInfo;
// 扫描出的网络连接列表
private List<ScanResult> mWifiList;
// 网络连接列表
private List<WifiConfiguration> mWifiConfiguration;
WifiLock mWifiLock;
public Activity1(Context context) {
// 取得wifimanager对象
mWifiManager = (WifiManager) context
.getSystemService(context.WIFI_SERVICE);
// 取得wifiinfo对象
mWifiInfo = mWifiManager.getConnectionInfo();
}
//打开wifi
public void OpenWifi() {
if (!mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(true);
}
}
public void CloseWifi() {
if (!mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(false);
}
}
// 锁定wifilock
public void AcquireWifiLock() {
mWifiLock.acquire();
}
// 解锁wifilock
public void ReleaseWifiLock() {
if (mWifiLock.isHeld()) {
mWifiLock.acquire();
}
}
// 创建一个wifilock
public void CreatWifiLock() {
mWifiLock = mWifiManager.createWifiLock("Test");
}
// 得到配置好的网络
public List<WifiConfiguration> getConfiguration() {
return mWifiConfiguration;
}
public void ConnectConFiguration(int index) {
if (index > mWifiConfiguration.size()) {
return;
}
mWifiManager.enableNetwork(mWifiConfiguration.get(index).networkId,
true);
}
public void StartScan() {
mWifiManager.startScan();
mWifiList = mWifiManager.getScanResults();
mWifiConfiguration = mWifiManager.getConfiguredNetworks();
}
public List<ScanResult> getWifiList() {
return mWifiList;
}
public StringBuilder LookUpScan() {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < mWifiList.size(); i++) {
stringBuilder.append("Index" + new Integer(i + 1).toString() + ":");
stringBuilder.append(mWifiList.get(i).toString());
stringBuilder.append("\n");
}
return stringBuilder;
}
// 得到MAC地址
public String GetMacAddress() {
return (mWifiInfo == null) ? "NULL" : mWifiInfo.getMacAddress();
}
// 得到接入点BSSID
public String GetBSSID() {
return (mWifiInfo == null) ? "NULL" : mWifiInfo.getBSSID();
}
// 得到IP地址
public int GetIPAddress() {
return (mWifiInfo == null) ? 0 : mWifiInfo.getIpAddress();
}
// 得到连接的ID
public int GetNetworkId() {
return (mWifiInfo == null) ? 0 : mWifiInfo.getNetworkId();
}
// 得到WifiInfo的所有信息包
public String GetWifiId() {
return (mWifiInfo == null) ? "NULL" : mWifiInfo.toString();
}
// 添加一个网络并连接
public void AddNetwork(WifiConfiguration wcg) {
int wcgID = mWifiManager.addNetwork(wcg);
mWifiManager.enableNetwork(wcgID, true);
}
// 断开指定ID的网络
public void DisconnectWifi(int netId) {
mWifiManager.disableNetwork(netId);
mWifiManager.disconnect();
}
}
使用的权限基本上有:
<!-- ----------得到WIFI访问网络所需要的权限------------------ -->
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<!-- 允许程序改变网络连接状态 -->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<!-- 允许程序改变Wi-Fi连接状态 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- 允许程序访问有关GSM网络信息 -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- 允许程序访问Wi-Fi网络状态信息 -->
<uses-permission android:name="android.permission.ACCESS_CHECKIN_PROPERTIES" />
<!-- 允许读写访问”properties”表在checkin数据库中,改值可以修改上传 -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- 允许使用PowerManager的 WakeLocks保持进程在休眠时从屏幕消失 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- 允许程序打开网络套接字 -->
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<!-- 允许修改话机状态,如电源,人机接口等 -->