//获得WifiManager
private WifiManager getWifiManager(Instrumentation inst) {return (WifiManager) inst.getTargetContext().getSystemService(
Context.WIFI_SERVICE);
}
// 断开WiFi连接
public boolean disconnectWifi(Instrumentation inst) {
getWifiManager(inst).disconnect();
int i = 5;
while (isWifiConnected(inst) == true && i > 0) {
getWifiManager(inst).disconnect();
solo.sleep(2000);
isWifiConnected(inst);
i--;
}
if (i == 0 && isWifiConnected(inst)) {
Log.v(TAG, "已经尝试5次,依然没有关闭WiFi");
return isWifiConnected(inst);
} else {
Log.v(TAG, "成功关闭WiFi");
return isWifiConnected(inst);
}
}
//重新连接WiFi
public boolean reconnectWifi(Instrumentation inst) {
getWifiManager(inst).reconnect();
int i = 5;
while (isWifiConnected(inst) == false && i > 0) {
getWifiManager(inst).reconnect();
solo.sleep(2000);
isWifiConnected(inst);
i--;
}
if (i == 0 && isWifiConnected(inst)) {
Log.v(TAG, "已经尝试5次,依然没有连接WiFi");
return isWifiConnected(inst);
} else {
Log.v(TAG, "成功连接WiFi");
return isWifiConnected(inst);
}
}
//判断WiFi连接状态 :true表示连接;false表示未连接
public static boolean isWifiConnected(Instrumentation inst) {
ConnectivityManager cm = (ConnectivityManager) inst.getContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiNetworkInfo = cm
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
return wifiNetworkInfo.isConnected();
}