The previous article on the PC and Android phone connection method, through the Java Call System command to perform the ADB command operation, is actually a relatively stupid way.
Online access to information, found that Google has provided the Ddmlib library (Adt-bundle\sdk\tools directory), provides all the ADB-related operations of the API.
Document reference
Http://www.jarvana.com/jarvana/view/com/google/android/tools/ddmlib/r13/ddmlib-r13-javadoc.jar!/index.html
Reference examples are as follows
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 are not a use this file except in compliance with the License.
* Obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable or agreed to writing, software
* Distributed under the License is distributed on a "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;
}
}
Mobile phone Code reference is as follows
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 ();
}
}
}
Transferred from: http://blog.csdn.net/wsdx/article/details/9420707
Using Ddmlib to achieve PC-to-Android ForWord Socket Communication (RPM)