上一篇文章写到了如何连接ADB,接下来要做的就是如何利用Socket通信。
现在我们用java代码来实现连接。
需要在项目中导入ddmlib.jar。位于SDK中..\sdk\tools\lib\ddmlib.jar
ADB连接代码如下:
public static int PC_LOCAL_PORT = 22222;
public static int PHONE_PORT = 22222;
private static ADB mADB;
private static IDevice[] mDevices;
private static IDevice mDevice;
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();//socket数据传输
}
public class ADB {
private AndroidDebugBridge mAndroidDebugBridge;
public boolean initialize() {
boolean success = true;
String adbLocation = System
.getProperty("com.android.screenshot.bindir");
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;
}
}
连接上之后,直接利用socket进行数据传输。
Client:
static Socket socket;
public static void initializeConnection() {
// Create socket connection
try {
socket = new Socket("localhost", PC_LOCAL_PORT);
OutputStream outputStream = socket.getOutputStream();
outputStream.write("llaalal".getBytes());
outputStream.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();
}
}
Server:
public static final String TAG = "server";
public static int PHONE_PORT = 22222;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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.e(TAG, "Get a connection from "
+ client.getRemoteSocketAddress().toString());
InputStream inputStream = client.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String cache= "";
String somewords ="";
while((cache = br.readLine())!=null){
somewords +=cache;
}
Log.e(TAG, "Get some words" + somewords);
this.publishProgress(somewords);
client.close();
} catch (IOException e) {
Log.e(TAG, "" + e);
}
}
}
@Override
protected void onProgressUpdate(Object... values) {
super.onProgressUpdate(values);
Toast.makeText(mContext, values[0].toString(), Toast.LENGTH_LONG)
.show();
}
}
运行结果:
如果不成功,需要确认:
1、有没有导入ddmlib.jar。(位于SDK中..\sdk\tools\lib\ddmlib.jar)
2、adb是否连上。(参考Android开发-通过USB实现ADB控制)