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的更多相关文章

  1. [uiautomator篇][python] wifi接口学习网址

    https://wifi.readthedocs.io/en/latest/wifi_command.html#usage

  2. .NET Core CSharp初级篇 1-1

    .NET Core CSharp初级篇 1-1 本节内容是对于C#基础类型的存储方式以及C#基础类型的理论介绍 基础数据类型介绍 例如以下这句话:"张三是一名程序员,今年15岁重50.3kg ...

  3. 保护嵌入式802.11 Wi-Fi设备时需要考虑的10件事

    保护嵌入式802.11 Wi-Fi设备时需要考虑的10件事 10 things to consider when securing an embedded 802.11 Wi-Fi device 随着 ...

  4. .NET C#教程初级篇 1-1 基本数据类型及其存储方式

    .NET C# 教程初级篇 1-1 基本数据类型及其存储方式 全文目录 (博客园).NET Core Guide (Github).NET Core Guide 本节内容是对于C#基础类型的存储方式以 ...

  5. 【智能家居篇】wifi网络结构(上)

    转载请注明出处:http://blog.csdn.net/Righthek 谢谢! WIFI是什么.相信大家都知道,这里就不作说明了. 我们须要做的是深入了解其工作原理,包含软硬件.网络结构等.先说明 ...

  6. ESP8266开发之旅 网络篇⑤ Scan WiFi——ESP8266WiFiScan库的使用

    1. 前言     现在,通常,为了让手机连上一个WiFi热点,基本上都是打开手机设置里面的WiFi设置功能,然后会看到里面有个WiFi热点列表,然后选择你要的连接上. 基本上你只要打开手机连接WiF ...

  7. 26-ESP8266 SDK开发基础入门篇--编写WIFI模块 SmartConfig/Airkiss 一键配网

    https://www.cnblogs.com/yangfengwu/p/11427504.html SmartConfig/Airkiss 配网需要APP/微信公众号,这节大家先使用我做好的APP/ ...

  8. 【智能家居篇】wifi网络接入原理(上)——扫描Scanning

    转载请注明出处:http://blog.csdn.net/Righthek 谢谢! 对于低头党来说,在使用WIFI功能时,常常性的操作是打开手机上的WIFI设备,搜索到心目中的热点,输入passwor ...

  9. iOS开发多线程篇 11 —自定义NSOperation

    iOS开发多线程篇—自定义NSOperation 一.实现一个简单的tableView显示效果 实现效果展示: 代码示例(使用以前在主控制器中进行业务处理的方式) 1.新建一个项目,让控制器继承自UI ...

随机推荐

  1. SpringMVC中,前台jsp封装参数,绑定参数,传递参数到后台controller的过程详解

    前台到后台的流程:前台jsp->后台:controller控制器层->service业务层->DAO数据访问层->数据库model模型层. 从上面流程可知,前台jsp的数据,想 ...

  2. 【持续更新】JS 时间与日期

    JS 的日期时间在项目中是必定会用到的,所以必须掌握. UTC 与 GMT 背景 十七世纪,格林威治皇家天文台为了海上霸权的扩张计画而进行天体观测.1675年旧皇家观测所(Old Royal Obse ...

  3. CF739B

    深搜的过程中保存路径,二分路径中满足要求的区段.不必将每个节点的ans加1,只需将合法区段末尾加1同时将开头减1来表示并保存在一个“前缀”数组中即可.最后再dfs一次累加得到答案. #include ...

  4. cf1028C. Rectangles(前缀和)

    题意 给出$n$个矩形,找出一个点,使得至少在$n$个矩阵内 Sol 呵呵哒,昨天cf半夜场,一道全场切的题,我没做出来..不想找什么理由,不会做就是不会做.. 一个很显然的性质,如果存在一个点 / ...

  5. CSS3 基本要素概览

    这篇文章将对 CSS 的几个新属性 (text-shadow,box-shadow,and border-radius) 做基本介绍.这些 CSS3 属性通常用来加强页面布局.  RGBA  前面的 ...

  6. IP-XACT IP IEEE交换格式

    1 What is  IP-XACT?      IP-XACT is an XML format that defines and describes electronic components a ...

  7. UVA - 1395 Slim Span (最小生成树Kruskal)

    Kruskal+并查集. 点很少,按边权值排序,枚举枚举L和R,并查集检查连通性.一旦连通,那么更新答案. 判断连通可以O(1),之前O(n)判的,第一次写的过了,后来T.. #include< ...

  8. VC-基础:VC中得到当前系统的时间和日期

    得到当前时间的方法一般都是得到从1900年0点0分到现在的秒数,然后转为年月日时分秒的形式得到当前的时间(时分秒).主要方法如下: 1)使用CRT函数 C++代码   ]; time_t nowtim ...

  9. PLAYGROUND 延时运行

    PLAYGROUND 延时运行 由 王巍 (@ONEVCAT) 发布于 2015/09/16 从 WWDC 14 的 Keynote 上 Chris 的演示就能看出 Playground 异常强大,但 ...

  10. React中 checkbox 与 label 标签的搭配

    用<label>标签替代checkbox的点击样子,点击<label>实际上就是点击checkbox checkbox的checked值会跟着一起变 <input typ ...