Mobile phone with PC USB connection ADB socket communication

Source: Internet
Author: User
Tags getmessage sleep
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 ());

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.