利用ddmlib 实现 PC端与android手机端adb forword socket通信(转)
上篇文章讲了PC与android手机连接的办法 ,通过java调用系统命令执行adb命令操作,实际上是一个比较笨的办法。
网上查阅资料,发现google 提供了ddmlib库 (adt-bundle\sdk\tools 目录下), 提供了adb相关操作的所有api。
文档参考
参考范例如下
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import com.android.ddmlib.AdbCommandRejectedException;
import com.android.ddmlib.IDevice;
import com.android.ddmlib.TimeoutException;
public class YingyonghuiHubServer {
public static final String TAG = "server";
public static int PC_LOCAL_PORT = 22222;
public static int PHONE_PORT = 22222;
public static String ADB_PATH = "adb.exe";
private static ADB mADB;
private static IDevice[] mDevices;
private static IDevice mDevice;
/**
* @param args
*/
public static void main(String[] args) {
mADB = new ADB();
mADB.initialize();
mDevices = mADB.getDevices();
mDevice = mDevices[0];
try {
mDevice.createForward(PC_LOCAL_PORT, PHONE_PORT);
} catch (TimeoutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AdbCommandRejectedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
initializeConnection();
}
static Socket socket;
public static void initializeConnection() {
// Create socket connection
try {
socket = new Socket("localhost", PC_LOCAL_PORT);
ObjectOutputStream oos = new ObjectOutputStream(
socket.getOutputStream());
oos.writeObject("lalala");
oos.close();
socket.close();
} catch (UnknownHostException e) {
System.err.println("Socket connection problem (Unknown host)"
+ e.getStackTrace());
e.printStackTrace();
} catch (IOException e) {
System.err.println("Could not initialize I/O on socket");
e.printStackTrace();
}
}
}
/*
* Copyright (C) 2009-2013 adakoda
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.File;
import com.android.ddmlib.AndroidDebugBridge;
import com.android.ddmlib.IDevice;
public class ADB {
private AndroidDebugBridge mAndroidDebugBridge;
public boolean initialize() {
boolean success = true;
String adbLocation = System
.getProperty("com.android.screenshot.bindir");
// for debugging (follwing line is a example)
// adbLocation = "C:\\ ... \\android-sdk-windows\\platform-tools"; // Windows
// adbLocation = "/ ... /adt-bundle-mac-x86_64/sdk/platform-tools"; // MacOS X
if (success) {
if ((adbLocation != null) && (adbLocation.length() != 0)) {
adbLocation += File.separator + "adb";
} else {
adbLocation = "adb";
}
AndroidDebugBridge.init(false);
mAndroidDebugBridge = AndroidDebugBridge.createBridge(adbLocation,
true);
if (mAndroidDebugBridge == null) {
success = false;
}
}
if (success) {
int count = 0;
while (mAndroidDebugBridge.hasInitialDeviceList() == false) {
try {
Thread.sleep(100);
count++;
} catch (InterruptedException e) {
}
if (count > 100) {
success = false;
break;
}
}
}
if (!success) {
terminate();
}
return success;
}
public void terminate() {
AndroidDebugBridge.terminate();
}
public IDevice[] getDevices() {
IDevice[] devices = null;
if (mAndroidDebugBridge != null) {
devices = mAndroidDebugBridge.getDevices();
}
return devices;
}
}
手机端代码参考如下
package com.broadthinking.yingyonghuihubclinet;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
public static final String TAG = "client";
public static int PHONE_PORT = 22222;
Context mContext = null;
TextView textView = null;
ServerSocket server = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.mContext = this;
this.textView = (TextView) this.findViewById(R.id.textView1);
try {
server = new ServerSocket(PHONE_PORT);
} catch (IOException e) {
e.printStackTrace();
return;
}
new RepackTestTask().execute();
}
private class RepackTestTask extends AsyncTask<Object, Object, Object> {
@Override
protected Object doInBackground(Object... params) {
Socket client = null;
// initialize server socket
while (true) {
try {
// attempt to accept a connection
client = server.accept();
Log.d(TAG, "Get a connection from "
+ client.getRemoteSocketAddress().toString());
ObjectInputStream ois = new ObjectInputStream(
client.getInputStream());
String somewords = (String) ois.readObject();
Log.d(TAG, "Get some words" + somewords);
this.publishProgress(somewords);
client.close();
} catch (IOException e) {
Log.e(TAG, "" + e);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
protected void onProgressUpdate(Object... values) {
super.onProgressUpdate(values);
Toast.makeText(mContext, values[0].toString(), Toast.LENGTH_LONG)
.show();
}
}
}
转自:http://blog.csdn.net/wsdx/article/details/9420707
利用ddmlib 实现 PC端与android手机端adb forword socket通信(转)的更多相关文章
- TODO monkey笔记,PC端执行和手机端执行
微博不给力啊 吞我笔记,还好我有txt... 1.环境准备:安装Android sdk, 配置环境变量:platfrom_tools,tools,aapt;java:2.查询当前apk信息: aapt ...
- H.264视频在android手机端的解码与播放(转)
随着无线网络和智能手机的发展,智能手机与人们日常生活联系越来越紧密,娱乐.商务应用.金融应用.交通出行各种功能的软件大批涌现,使得人们的生活丰富多彩.快捷便利,也让它成为人们生活中不可取代的一部分.其 ...
- PHP判断客户端是PC web端还是移动手机端方法
PHP判断客户端是PC web端还是移动手机端方法需要实现:判断手机版的内容加上!c550x260.jpg后缀变成缩略图PHP用正则批量替换Img中src内容,用正则表达式获取图片路径实现缩略图功能 ...
- pc端和android端应用程序测试有什么区别?(ps面试题)
pc端和android端应用程序测试有什么区别?(ps面试题) [VIP7]大连-凭海临风(215687736) 2014/4/10 8:56:171.测试环境不同PC平台一般都是windows an ...
- PC端的软件端口和adb 5037端口冲突解决方案
引用https://www.aliyun.com/jiaocheng/32552.html 阿里云 > 教程中心 > android教程 > PC端的软件端口和adb 50 ...
- PC端使用opencv获取webcam,通过socket把Mat图像传输到android手机端
demo效果图: PC端 android端 大体流程 android端是服务器端,绑定IP和端口,监听来自PC端的连接, pc端通过socket与服务器andorid端传输图片. 主要代码 andro ...
- 利用jQuery实现PC端href生效,移动端href失效
今天要写一个功能,记录一下吧.if(navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)){ $('.item-a').attr('href' ...
- pc端前端和手機端區別
1.pc端寬度比較固定,手機端可以橫屏或者豎屏: 2.pc端不需要處理手機觸摸,而手機端需要: 3.pc端不需要處理鍵盤事件: 3.pc的瀏覽器內核很多,手機端基本上是webkit或者是基於webki ...
- Android 手机端自动化测试框架
前言: 大概有4个月没有更新了,因项目和工作原因,忙的手忙脚乱,趁十一假期好好休息一下,年龄大了身体还是扛不住啊,哈哈.这次更新Android端自动化测试框架,也想开源到github,这样有人使用才能 ...
随机推荐
- ls与dir的区别
1.ls具有上色的效果,dir没有. 2.ls是unix/linux系列的命令,dir是dos/windows系列的命令.
- Scoket
1.Socket 几个常用的名词 IPC—>Inter Process Communication,进程间通信 socket —> 套接字 TCP—>Transmission Con ...
- 《Cortex-M0权威指南》之体系结构---嵌套中断控制器(NVIC)
转载请注明来源:cuixiaolei的技术博客 为了管理中断请求的优先级并处理其他异常,Cortex-M0处理器内置了嵌套中断控制器(NVIC).NVIC的一些可编程控制器控制着中断管理功能,这些寄存 ...
- JdbcTemplate 、NamedParameterJdbcTemplate、SimpleJdbcTemplate的区别
一.JdbcTemplate 首先在配置文件中设置数据源 <bean id="dataSource" class="org.springframework.jdbc ...
- Linux 网络设备驱动程序设计(2)
二.回环网卡的程序设计 /*************************** *******回环网卡的驱动程序*** ***********吕晓宁*********** *********2015 ...
- DOM 1
首先getAttribute setAttribute只能被元素节点对象调用.(属性节点和文本节点调用不了) 我们可以通过一下三种方式得到元素: document.getElementById(); ...
- 【转载】TalkingData首席金融行业专家鲍忠铁:18亿数据解读移动互联网
http://www.36dsj.com/archives/33417 鲍忠铁:大家下午好! 今天我会讲三个议题,一是用18亿数据解读现在移动互联网的生态圈.二是看看数据有什么样的应用.三是大数据的隐 ...
- Android开发的十项注意
随着移动平台的发展及其应用的不断改善,质量成为决定成败的关键.用户要求他们安装的应用响应快.性能好,如果某个应用不能提供卓越的功能和稳定的用户体验,那注定会被很快卸载: 尽管现在Android智能手机 ...
- 关于使用AIDL出现空指针的解决办法
使用AIDL进行远程调用的时候出现的空指针异常,解决过程稍微有点小曲折.具体安下 1.先贴异常信息 ERROR/AndroidRuntime(9435): FATAL EXCEPTION: main ...
- JavaScript之动画3
给一个div添加颜色,使其产生渐变效果,我们设置index为变量,使用setInterval函数方法改变rgb颜色值. window.onload = function(){ var boxDom = ...