[uiautomator篇][11]wifi
package com.softwinner.network.wifi; import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.RemoteException;
import android.support.test.InstrumentationRegistry;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;
import android.support.test.uiautomator.Until;
import android.util.Log; import java.io.IOException; import static android.support.test.InstrumentationRegistry.getArguments; import static android.support.test.InstrumentationRegistry.getContext;
import static org.junit.Assert.assertTrue; /**
* @author liuzhipeng
* Created by Administrator on 2017/6/27.
*/ public class wifiBaseClass {
private String packageName = "com.example.black.wifiswitch";
private UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
private String ssid ;
private String password ;
private String mLogTag ;
private WifiManager mWifiManager;
// = (WifiManager) InstrumentationRegistry.getContext().getSystemService(Context.WIFI_SERVICE); public wifiBaseClass(Context context, UiDevice device, String SSID, String passwd, String logTag, String packName){
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
mDevice = device;
ssid = SSID;
password = passwd;
mLogTag = logTag;
packageName = packName;
} /**
* after connect wifi, check the network is available
* @author liuzhipeng
* @throws UiObjectNotFoundException
* @throws InterruptedException
*/
public void connectWifiAndCheckNetwork() throws UiObjectNotFoundException, InterruptedException { final String ssidStr = "com.example.black.wifiswitch:id/ssid";
final String passwdIdStr = "com.example.black.wifiswitch:id/password";
final String connectIdStr = "com.example.black.wifiswitch:id/Connect" ;
Log.i(mLogTag,"trigger on wifi");
triggerOnWifi();
Log.i(mLogTag,"open wifiswitch apk");
openApplication(packageName);
Thread.sleep();
try {GetWiFiParameters();} catch (RemoteException e) {e.printStackTrace();}
Log.i(mLogTag,"connect wifi: " + ssid);
wakeupScreen();
UiObject ssidObj = mDevice.findObject(new UiSelector().resourceId(ssidStr));
ssidObj.setText(ssid);
wakeupScreen();
UiObject passwordObj = mDevice.findObject(new UiSelector().resourceId(passwdIdStr));
passwordObj.setText(password);
wakeupScreen();
UiObject connectObj = mDevice.findObject(new UiSelector().resourceId(connectIdStr));
connectObj.click();
Thread.sleep();
assertTrue("wifi state not enabled", checkWifiState() == );
Log.i(mLogTag, "check network available?");
assertTrue("wifi network unavailable", isNetworkAvailable());
Log.i(mLogTag, "network available");
} /**
* open third application:
* @author liuzhipeng
* @param packageNameStr
*/
public void openApplication(String packageNameStr){ try {mDevice.wakeUp();} catch (RemoteException e) {e.printStackTrace();}
UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
/* Start from the home screen*/
mDevice.pressHome(); // final String launcherPackage = mDevice.getLauncherPackageName();
// assertThat(launcherPackage,notNullValue());
// try {mDevice.wakeUp();} catch (RemoteException e) {e.printStackTrace();}
// mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
// 5000); // launch the app
Context context = InstrumentationRegistry.getContext();
final Intent intent = context.getPackageManager()
.getLaunchIntentForPackage(packageNameStr);
// Clear out any previous instances
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent); try {mDevice.wakeUp();} catch (RemoteException e) {e.printStackTrace();}
// Wait for the app to appear
mDevice.wait(Until.hasObject(By.pkg(packageNameStr).depth()),
);
} /** get wifi parameters: ssid and password
* @author liuzhipeng
* @throws RemoteException
*/
private void GetWiFiParameters() throws RemoteException { Bundle bundle = getArguments();
if (bundle.getString("ssid") != null) {
ssid = bundle.getString("ssid");
if (bundle.getString("password") != null) {
password = bundle.getString("password");
} else {
password = null;
}
}
} /**
* trigger on wifi
* @author liuzhipeng
*/
public void triggerOnWifi(){ // WifiManager mWifiManager = (WifiManager) InstrumentationRegistry.getContext().getSystemService(Context.WIFI_SERVICE);
if (!mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(true);
try {Thread.sleep();} catch (InterruptedException e) {e.printStackTrace();}
}
checkWifiState();
} /**
* trigger off wifi
* @author liuzhipeng
*/
public void triggerOffWifi(){ // WifiManager mWifiManager = (WifiManager) InstrumentationRegistry.getContext().getSystemService(Context.WIFI_SERVICE);
if (mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(false);
try {Thread.sleep();} catch (InterruptedException e) {e.printStackTrace();}
}
checkWifiState();
} /**
* check wifi state
* @author liuzhipeng
* @return wifiState
*/
public int checkWifiState(){ // WifiManager mWifiManager = (WifiManager) InstrumentationRegistry.getContext().getSystemService(Context.WIFI_SERVICE);
int tempInt = mWifiManager.getWifiState();
switch (tempInt){
case :
Log.i(mLogTag, "wifi state disabling");
break;
case :
Log.i(mLogTag, "wifi state disabled");
break;
case :
Log.i(mLogTag, "wifi state enabling");
break;
case :
Log.i(mLogTag, "wifi state enabled");
break;
case :
Log.i(mLogTag, "wifi state unknown");
break;
default:
break;
}
return tempInt; } /**
* @author liuzhipeng
* check network is available
* @return true if networkAviabile else false
*/
public static boolean isNetworkAvailable(){ ConnectivityManager connectivityManager = (ConnectivityManager) InstrumentationRegistry.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
return (info != null && info.isConnected() && (info.getType() == ConnectivityManager.TYPE_WIFI));
} /**
* wakeup screen
* @author liuzhipeng
*/
public void wakeupScreen(){
Context context = InstrumentationRegistry.getContext();
PowerManager pm=(PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK,"bright");
wl.acquire();
wl.release();
} public void quitApplication(String packageNameStr)
{
try {
mDevice.executeShellCommand("am force-stop "+ packageNameStr);
} catch (IOException e) {
e.printStackTrace();
} }
public void goToSleep(){
Context context = InstrumentationRegistry.getContext();
PowerManager pm =(PowerManager) context.getSystemService(Context.POWER_SERVICE);
// pm.goTosleep()
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ON_AFTER_RELEASE| PowerManager.PARTIAL_WAKE_LOCK,"wakeLockUtil");
wl.acquire();
wl.release(); }
}
[uiautomator篇][11]wifi的更多相关文章
- [uiautomator篇][python] wifi接口学习网址
https://wifi.readthedocs.io/en/latest/wifi_command.html#usage
- .NET Core CSharp初级篇 1-1
.NET Core CSharp初级篇 1-1 本节内容是对于C#基础类型的存储方式以及C#基础类型的理论介绍 基础数据类型介绍 例如以下这句话:"张三是一名程序员,今年15岁重50.3kg ...
- 保护嵌入式802.11 Wi-Fi设备时需要考虑的10件事
保护嵌入式802.11 Wi-Fi设备时需要考虑的10件事 10 things to consider when securing an embedded 802.11 Wi-Fi device 随着 ...
- .NET C#教程初级篇 1-1 基本数据类型及其存储方式
.NET C# 教程初级篇 1-1 基本数据类型及其存储方式 全文目录 (博客园).NET Core Guide (Github).NET Core Guide 本节内容是对于C#基础类型的存储方式以 ...
- 【智能家居篇】wifi网络结构(上)
转载请注明出处:http://blog.csdn.net/Righthek 谢谢! WIFI是什么.相信大家都知道,这里就不作说明了. 我们须要做的是深入了解其工作原理,包含软硬件.网络结构等.先说明 ...
- ESP8266开发之旅 网络篇⑤ Scan WiFi——ESP8266WiFiScan库的使用
1. 前言 现在,通常,为了让手机连上一个WiFi热点,基本上都是打开手机设置里面的WiFi设置功能,然后会看到里面有个WiFi热点列表,然后选择你要的连接上. 基本上你只要打开手机连接WiF ...
- 26-ESP8266 SDK开发基础入门篇--编写WIFI模块 SmartConfig/Airkiss 一键配网
https://www.cnblogs.com/yangfengwu/p/11427504.html SmartConfig/Airkiss 配网需要APP/微信公众号,这节大家先使用我做好的APP/ ...
- 【智能家居篇】wifi网络接入原理(上)——扫描Scanning
转载请注明出处:http://blog.csdn.net/Righthek 谢谢! 对于低头党来说,在使用WIFI功能时,常常性的操作是打开手机上的WIFI设备,搜索到心目中的热点,输入passwor ...
- iOS开发多线程篇 11 —自定义NSOperation
iOS开发多线程篇—自定义NSOperation 一.实现一个简单的tableView显示效果 实现效果展示: 代码示例(使用以前在主控制器中进行业务处理的方式) 1.新建一个项目,让控制器继承自UI ...
随机推荐
- JAVA设计模式之观察者模式 - Observer
有趣的事情发生时,可千万别错过了!有一个模式可以帮你的对象知悉现况,不会错过该对象感兴趣的事.对象甚至在运行时可决定是否要继续被通知.有了观察者,你将会消息灵通. 介绍 观察者模式的定义: 在对象之间 ...
- MySql中查询语句实现分页功能
import java.util.*;import java.sql.*; public class FruitDao { private Connection conn; private ...
- leetcode982 Triples with Bitwise AND Equal To Zero
思路: 使用unordered_map暴力枚举. 实现: #include <bits/stdc++.h> using namespace std; class Solution { pu ...
- CF1061B Views Matter
思路: 贪心. 实现: #include <bits/stdc++.h> using namespace std; ]; int main() { int n, m; while (cin ...
- uvm_scoreboard——得分
scoreboard 是验证平台很重要的一部分,因为,验证就是给激励,然后,检查结果.而scoreboard 就是肩负这检查结果的重任.测试用例能不能过,全由scoreboard说了算. A scor ...
- Windows Server 2008 R2中上传和下载文件
在 Windows Server 2008 R2 中,使用服务器管理器来启用或禁用 Windows 功能,那在这个上面如何上传和下载文件呢? 1.在“服务器管理器”->“角色”->“web ...
- MySQL从服务配置文件
[mysql]port=3306socket=/var/lib/mysql/mysql.sockdefault-character-set = utf8mb4 [mysqld]server-id=2l ...
- C# 分支语句 练习题
1.“请输入年份:”(1-9999) “请输入月份:”(1-12) “请输入日期:”(要判断大小月,判断闰年) 判断输入的时间日期是否正确 bool dateISOK = false;//放置日期是否 ...
- codevs 5462 HYY迎亲I
时间限制: 1 s 空间限制: 256000 KB 题目等级 : 青铜 Bronze 题目描述 Description HYY要娶山那头的JCH,可毕竟是山路,十分崎岖,他又十分的单(bai)纯(ch ...
- UVALive 3211 Now or Later (2-SAT)
题目的要求一个最小值最大,二分即可,但是怎么判断呢? 飞机早或者晚两种状态,可以用一个布尔变量表示,假设当前猜测为m,那么根据题意, 如果x和y所对应的时间冲突那么就是¬(xΛy)化成或的形式(¬x) ...