Use ADB forword to communicate with Android mobile phones

Source: Internet
Author: User

Use ADB forword to communicate with Android mobile phones

The server code is as follows:

import java.io.IOException;import java.io.ObjectOutputStream;import java.net.Socket;import java.net.UnknownHostException;import java.util.Scanner;public class Server {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";/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubYingyonghuiHubServer.execAdb();}public static void execAdb() {// run the adb bridgetry {Process p = Runtime.getRuntime().exec(ADB_PATH + " forward tcp:" + PC_LOCAL_PORT + " tcp:"+ String.valueOf(PHONE_PORT));Scanner sc = new Scanner(p.getErrorStream());// If there is some output, it failed to start adbif (sc.hasNext()) {while (sc.hasNext())System.out.println(sc.next());System.err.println("Cannot start the Android debug bridge");return;}initializeConnection();} catch (Exception e) {System.err.println(e.toString());}}static Socket socket;public static void initializeConnection() {// Create socket connectiontry {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();}}}

 

The client code is as follows:

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 Client extends Activity {public static final String TAG = "client";public static int PHONE_PORT = 22222;Context mContext = null;TextView textView = null;ServerSocket server = null;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.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 {@Overrideprotected Object doInBackground(Object... params) {Socket client = null;// initialize server socketwhile (true) {try {// attempt to accept a connectionclient = 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 blocke.printStackTrace();}}}@Overrideprotected void onProgressUpdate(Object... values) {super.onProgressUpdate(values);Toast.makeText(mContext, values[0].toString(), Toast.LENGTH_LONG).show();}}}

Related Article

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.