C # and Android Enable USB communication via ADB

Source: Internet
Author: User
Tags getmessage

Transferred from: http://blog.csdn.net/linweidong/article/details/6273507

Demand:

Android APK gets Mobile information and sends results to PC client

Note Place:

1.android default phone-side IP is "127.0.0.1"

2. To connect the PC and Android phone Sokcet, be sure to use ADB forward to make the lower port forwarding to the socket.

3. Using socket communication, you need to add Permission:android.permission.INTERNET in Mainfest.xml

Runtime.getruntime (). EXEC ("adb forward tcp:12580 tcp:10086");   Thread.Sleep (3000);  

Android as a service import java.io.BufferedInputStream; import java.io.BufferedOutputStream;

Importjava.io.IOException;Importjava.net.InetAddress;ImportJava.net.ServerSocket;ImportJava.net.Socket; Public classTcpconnectImplementsrunnable{Private Final intServer_port = 10086; PrivateServerSocket Mserversocket; PrivateSocket mclient; PrivateString Mdeviceid; PrivateString Mdevicetype;  Publictcpconnect (String Adeviceid, String adevicetype) { This. mdeviceid=Adeviceid;  This. Mdevicetype =Adevicetype; Try{String IP=inetaddress.getlocalhost (). gethostaddress (); System.out.println ("IP address is:" +IP); //System.out.println (Adeviceid + "model:" + adevicetype);Mserversocket =NewServerSocket (Server_port); System.out.println ("Tcpconnect" + "build Socket"); // listen (); } Catch(IOException e) {//TODO auto-generated Catch block//e.printstacktrace ();System.out.println ("Tcpconnect" +e.getmessage ()); } }  Public voidListen () { while(true){ Try{mclient=mserversocket.accept (); // log.e ("Tcpconnect", "in Active Listening");}Catch(IOException e) {//TODO auto-generated Catch block//e1.printstacktrace ();System.out.println ("Tcpconnect" +e.getmessage ()); } }} @Override Public voidrun () {//TODO auto-generated Method Stub// if (mclient.isconnected ()) {Bufferedoutputstream out =NULL; System.out.println ("Tcpconnect" + "Start listening");  while(true){ Try{// log.e ("Tcpconnect", "Start Monitoring");Mclient =mserversocket.accept (); // if (mclient.isconnected ()) {System.out.println ("Tcpconnect" + "detected connection"); out=NewBufferedoutputstream (Mclient.getoutputstream ()); String Recordstr= Mdeviceid + "|" +Mdevicetype; Out.write (Recordstr.getbytes ("Utf-8")); // int length = Recordstr.getbytes (). length; // byte[] B = recordstr.getbytes (); // out.writeint (length); // Out.write (b); Out.flush (); // log.e ("Tcpconnect", recordstr); // Out.flush (); // }}Catch(Exception e) {
System.out.println ("Tcpconnect" +e.getmessage ()); }finally{ if(Out! =NULL){ Try{out.close (); } Catch(IOException e) {//TODO auto-generated Catch blockSystem.out.println ("Tcpconnect" +e.getmessage ()); } } if(Mserversocket! =NULL){ Try{mserversocket.close (); } Catch(IOException e) {//TODO auto-generated Catch blockSystem.out.println ("Tcpconnect" +e.getmessage ()); } } // } } } } Public Static voidMain (string[] args) {NewThread (NewTcpconnect ("2366578546946", "T959") . Start (); } }

C # As a client, bind the port on the client:

Process p =NewProcess ();//instance a process class, starting an independentp.StartInfo.FileName ="Cmd.exe";//Set Program nameP.startinfo.useshellexecute =false;//turn off the use of the shellP.startinfo.redirectstandardinput =true;//REDIRECT standard inputP.startinfo.redirectstandardoutput =true;//REDIRECT standard outputP.startinfo.redirectstandarderror =true;//REDIRECT error outputP.startinfo.createnowindow =true;//settings do not display WindowsP.startinfo.errordialog =false; P.startinfo.windowstyle=Processwindowstyle.hidden;                P.start (); P.standardinput.writeline (@"adb forward tcp:12580 tcp:10086"); //Thread.Sleep (3000); Socketclient client =Newsocketclient (); MessageBox.Show ("the data received are:"+ Client.listen ());

Socket Client for C #:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Net;usingSystem.Net.Sockets;namespacepreinstaller.io{classsocketclient { Public stringListen () {Socket client=Newsockets (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp); IPAddress MyIP= Ipaddress.parse ("127.0.0.1"); IPEndPoint Ephost=NewIPEndPoint (MyIP,int. Parse ("12580")); Client.                      Connect (Ephost); byte[] T_data =New byte[1024x768]; stringdata =NULL; inti =0;  while((i = client.) Receive (t_data))! =0) {Data= Encoding.UTF8.GetString (T_data,0, i); } client.             Close (); returndata; }    }}

(go) C # and Android via ADB for USB communication

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.