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. [问题记录]Ubuntu下chmsee安装失败的解决

    日期:2016年2月26日 一直在找Ubuntu下查看chm的工具但是普遍不理想,发现在deepin中的chmsee相对比较好,但是直接执行网上的sudo apt-get install chmsee ...

  2. git处理时的问题

    1. 在node.js开发的时候常常会遇到从别人的远程仓库中clone时出现文件名过长的错误, 或则是在本地npm下载之后的文件进行上传到自己的远程仓库的时候会出现 File too long的情况, ...

  3. Spring MVC系列[1]—— HelloWorld

    1.导入jar包 ioc mvc 复制spring-mvc.xml到src目录下. 2.web.xml <?xml version="1.0" encoding=" ...

  4. MySQL备份还原介绍

    window系统下 1.导出整个数据库mysqldump -u 用户名 -p 数据库名 > 导出的文件名mysqldump -u dbuser -p dbname > dbname.sql ...

  5. HTML5文档结构 摘要

    <!DOCType html>--声明文档结构类型 <html lang=zh-cn> <head> <meta charset=utf-8> < ...

  6. 洛谷 P2253 好一个一中腰鼓!

    题目背景 话说我大一中的运动会就要来了,据本班同学剧透(其实早就知道了),我萌萌的初二年将要表演腰鼓[喷],这个无厘头的题目便由此而来. Ivan乱入:“忽一人大呼:‘好一个安塞腰鼓!’满座寂然,无敢 ...

  7. 霍金的新语音系统 ACAT 将开源

    英国理论物理学家斯蒂芬·霍金(Stephen Hawking)使用了二十年的语音通讯系统被英特尔开发的新一代通讯平台替代,显著改进了通讯效率.但霍金的声音并没有发生改变,他仍然使用相同的语音合成器.霍 ...

  8. Codeforces Round #290 (Div. 2) _B找矩形环的三种写法

    http://codeforces.com/contest/510/status/B 题目大意 给一个n*m  找有没有相同字母连起来的矩形串 第一种并查集 瞎搞一下 第一次的时候把val开成字符串了 ...

  9. 国家气象局提供的天气预报接口(完整Json接口)

    国家气象局提供的天气预报接口主要有三个,分别是:http://www.weather.com.cn/data/sk/101010100.htmlhttp://www.weather.com.cn/da ...

  10. css3中animation属性animation-timing-function知识点以及其属性值steps()

    在animation中最重要的其实就是时间函数(animation-timing-function)这个属性,他决定了你的动画将以什么样的速度执行,所以最关键的属性值也就是cubic-bezier(n ...