[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 ...
随机推荐
- WdatePicker时间插件 有百度云下载 jsp界面选择时间的简单方法
链接:https://pan.baidu.com/s/1XCod602gCMv-qMQ4fMOLbg 提取码:ok8i 复制这段内容后打开百度网盘手机App,操作更方便哦 把东西复制到项目.导入j ...
- 6、旋转数组的最小位置------------>剑指offer系列
题目 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转, ...
- CSS元素隐藏的display和visibility
一.CSS元素隐藏 在CSS中,让元素隐藏(指屏幕范围内肉眼不可见)的方法很多,有的占据空间,有的不占据空间:有的可以响应点击,有的不能响应点击. { display: none; /* 不占据空间, ...
- 海康威视采集卡结合opencv使用(两种方法)-转
(注:第一种方法是我的原创 ^_^. 第二种方法是从网上学习的.) 第一种方法:利用 板卡的API: GetJpegImage 得到 Jpeg 格式的图像数据,然后用opencv里的一个函数进行解码 ...
- Spark-水库抽样-根据抽样率确定每个分区的样本大小
/* * 输入:采样率,待采样的RDD * 输出:每个分区的样本大小(记录数) * 由采样率确定,每个分区的样本大小 */ def findNumPerPartition[T: ClassTag, U ...
- uvm_factory——我们的工厂(二)
上节我们说到uvm_object_registry #(T),uvm_object_reistry 又继承自uvm_object_wrapper,所以首先,让我们先看看它爹是啥样子的: //----- ...
- ImportError: No module named flask.ext.wtf 解决方法
install pip install flask.ext.wtf
- shell 简单脚本 2
#!/bin/bash source /etc/profile APPLICATIONS_HOME="/cpic/cpicapp/cpic_analy/jars" APPLICAT ...
- bootstrap下拉菜单(Dropdowns)
本章将重点讲解bootstrap下拉菜单(Dropdowns),下拉菜单是可切换的,是以列表格式显示链接的上下文菜单. <!DOCTYPE html><html><hea ...
- Bootstrap 基本按钮
本章将通过实例讲解如何使用Bootstrap按钮,任何带有class.btn的元素都会继承圆角灰色默认按钮样式,但Bootstrap提供了一些选项来定义按钮的样式. 实例 <!DOCTYPE h ...