the handset connects the PC through the USB cable, realizes the PC to send the message voluntarily to the handset;
one, get all the mobile phones connected with the computer
public static list<device> finddevices () {list<device>devices = new arraylist<device> ();
String str = NULL;
int port = 15000;
Process process = NULL;
Device Device = null;
string[] Devicestr = new string[2];
List<string>lines=new arraylist<string> ();
try {process = Runtime.getruntime (). EXEC ("adb devices");
InputStream in = Process.getinputstream ();
BufferedReader read=new BufferedReader (New InputStreamReader (in));
while ((Str=read.readline ())!=null) {lines.add (str);
System.out.println (str);
for (int i=1;i<lines.size () -1;i++) {str = lines.get (i);
Devicestr = Str.split ("");
if ("Device". Equals (Devicestr[1])) {device = new device ();
Device.setdeviceid (Devicestr[0]);
Device.setstate (devicestr[1]);
At present, port is specified first; Device.setport (port);
port++;
Devices.add (device);
A catch (IOException e) {e.printstacktrace ());
return devices;}
Second, port mapping; Start the app at the end of the phone
public static void Portforwardbydevice (Devices device) {String a = null;
String B = null;
String c = null;
String d = null;
Log logger = Logfactory.getlog (Devicefinder.class);
A = "Adb-s" + device.getdeviceid () + "Shell am broadcast-a notifyservicestop";
b = "Adb-s" + device.getdeviceid () + "forward tcp:" +device.getport () + "tcp:12222";
c = "Adb-s" + device.getdeviceid () + "Shell am broadcast-a notifyservicestart"; D= "Adb-s" + device.getdeviceid () + "Shell am start-n com.newland.realmobiledetection/com.newland.realmobiledetection."
System.activity.WelcomeActivity "; Logger.error ("... device...a ...)
+A); Logger.error ("... device...b ...)
+B); Logger.error ("... device...c ...)
+C); Logger.error ("... device...d ...)
+D);
try {runtime.getruntime (). exec (d);
Thread.Sleep (1000);
Runtime.getruntime (). exec (a);
Thread.Sleep (1000);
Runtime.getruntime (). exec (b);
Thread.Sleep (1000); Runtime.getruntime (). exec (c);
Thread.Sleep (1000); Logger.error ("Port mapping complete.
");
catch (IOException e) {logger.error ("with mobile communication Exception" +e.getmessage ());
}catch (interruptedexception e) {logger.error ("Thread Interrupt Exception" +e.getmessage ());
}
}
third, the connection:
try {
inetaddress serveraddr = null;
SERVERADDR = Inetaddress.getbyname ("127.0.0.1");
Socket = new Socket (serveraddr, pcport);
Socket.setkeepalive (true);
Socket.setsotimeout (1000);
Bufferedoutputstream out = new Bufferedoutputstream (Socket.getoutputstream ());
Bufferedinputstream in = new Bufferedinputstream (Socket.getinputstream ());
Out.write (Requestcontent.getbytes ());
Out.flush ();
Responsecontent=readfromsocket (in);
System.out.println (":::::;::::::::" + responsecontent);
: Logger.info (":::::;::::::::" +responsecontent);
obj = Gson.fromjson (responsecontent, Method.getgenericreturntype ());
Mobile phone end app);
ServerSocket =new Serverscoket (12222);
Socket socket = serversocket.accept ();
out = new Bufferedoutputstream (Socket.getoutputstream ());
in = new Bufferedinputstream (Socket.getinputstream ());