There are many Android apps where you can run some tasks without wireless connection (4G, 3G, Wi-fi or Bluetooth), only using USB cable. Below, I’ll describe an easy way to do it.

As usual, you’ll need Android Development Kit, that can be found here.

At client development, we need to change ADB ports, used by socket connections. We can do this by running the following command:

<PLATFORM-TOOLS DIR>/adb forward tcp:59900 tcp:59900

Using Java:

Runtime.getRuntime().exec("<PLATFORM-TOOLS DIR>/adb forward tcp:59900 tcp:59900");

Remember that “<PLATFORM-TOOLS DIR>” is the path to “platform-tools” directory, located in Android SDK installation directory.

We only changed the “ears” of ADB to listen the port 59900, used in our connection.

Lets send a “Hello World!”:

Socket socket = new Socket("127.0.0.1", 59900);
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
bos.write("Hello World!".getBytes());
bos.flush();
bos.close();

Now the Client is ready, lets go to the application.

Add the permissions “android.permission.ACCESS_NETWORK_STATE” and “android.permission.INTERNET” to the AndroidManifest.xml.

Given the appropriate permissions, we do application to listen the requests coming from port 59900, using the class ServerSocket from package java.net:

ServerSocket server = new ServerSocket(59900);
Socket socket = server.accept();

With Socket object, we can get the data using getInputStream():

InputStreamReader reader = new InputStreamReader(socket.getInputStream());
BufferedReader br = new BufferedReader(reader);
msg = br.readLine();
br.close();

We are working with Strings, therefore I used the class BufferedReafer, to get entire line with method readLine();

It’s done! Now we can communicate application and computer using only USB cable.

It’s not allowed using main thread to opening socket, so, you’ll need to open the socket in a separated Thread or to extend the class AsyncTask.

If you still have doubts about this method, I’ll make available the example used in this tutorial here.