java获取手机 usb_java 后台 实现USB设备连接获取设备号ID,实现adb shell命令

该博客介绍了如何使用Java在后台获取连接到电脑的Android设备信息,包括设备序列号、产品名、型号和名称。通过执行adb命令获取设备列表,并根据设备状态判断是否已连接。此外,还展示了如何根据设备序列号执行特定的adb shell命令。
摘要由CSDN通过智能技术生成

();

map = new ArrayList();

//设置adb.exe存放路径,如果设置了环境变量,直接输入adb即可

String adb_path = "adb";

//执行adb device操作,查看pc当前连接手机或模拟器设备列表

//注意:一定要先配置好sdk环境变量,否则无法直接执行adb命令

process = excuteShell(adb_path + " devices -l");

if(process != null){

reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

while ((line = reader.readLine()) != null) {

if (line.length() > 1) {

list.add(line);

}

}

if(!list.contains("* daemon started successfully *")){

if (list != null && list.size() > 1) {

if(!list.contains("device")){

for (int i = 1; i < list.size(); i++) {

for (int j = 0; j < list.get(i).split(" ").length; j++) {

//获取手机设备连接状态,目前状态有:device(正常)、offline、unauthorized

device_tpye = list.get(i).split(" ")[j];

//判断当前设备状态是否正常

if(device_tpye.equals("device")){

//获取设备序列号

String device_sn = list.get(i).split(" ")[0];

//获取设备产品名

String device_product = list.get(i).split(" ")[8];

//获取设备型号

String device_model = list.get(i).split(" ")[9];

//获取设备名称

String device_name = list.get(i).split(" ")[10];

System.out.println("当前设备序列号:"+ device_sn);

System.out.println("当前设备产品名:"+ device_product);

System.out.println("当前设备型号:"+ device_model);

System.out.println("当前设备名称:"+ device_name);

ProdSalesRankingBo prodSalesRankingBo = new ProdSalesRankingBo();

prodSalesRankingBo.setDevice_sn(device_sn);

prodSalesRankingBo.setDevice_product(device_product);

prodSalesRankingBo.setDevice_model(device_model);

prodSalesRankingBo.setDevice_name(device_name);

map.add(prodSalesRankingBo);

if(tag == 1){

// 1 公众号

excuteShellTag(device_sn,"ExampleInstrumentedTest");

} else if(tag == 2){

// 2搜索信息

excuteShellTag(device_sn,"ExampleInstrumentedTest");

} else if(tag == 3){

// 3搜索群发送素材

excuteShellTag(device_sn,"SearchGroupTest");

}

}

}

}

}else{

System.out.println("当前设备列表中,没有device类型连接设备,请检查!");

}

}else{

System.out.println("当前设备列表没有连接的设备,请检查!");

}

}else{

getAllDevices(tag);

}

}else{

System.out.println("当前执行adb命令异常,请检查adb环境!");

}

} catch (IOException e) {

System.err.println("IOException" + e.getMessage());

return null;

}

return map;

}

/**

* 执行adb命令

*

* @param s 要执行的命令  参数

*

*/

public static Process excuteShell(String s) {

Process proc = null;

Runtime runtime = Runtime.getRuntime();

try {

proc = runtime.exec(s);

} catch (Exception e) {

System.out.print("执行命令:" + s + "出错啦!");

return null;

}

return proc;

}

/**

* 通过设备ID执行adb命令

* adb -s ac377077 shell am instrument -w -r -e debug false -e class 'com.juchiwang.app.uiautomatordemo2.ExampleInstrumentedTest' com.juchiwang.app.uiautomatordemo2.test/androidx.test.runner.AndroidJUnitRunner

*/

public static Process excuteShellTag(String s,String text) {

Process proc = null;

Runtime runtime = Runtime.getRuntime();

try {

String shellString = "adb -s "+ s + " shell am instrument -w -r -e debug false -e class 'com.juchiwang.app.uiautomatordemo2."+ text +"' com.juchiwang.app.uiautomatordemo2.test/androidx.test.runner.AndroidJUnitRunner";

proc = runtime.exec(shellString);

System.out.print("执行命令:" + shellString.toString());

} catch (Exception e) {

System.out.print("执行命令:" + s + "出错啦!");

return null;

}

return proc;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值