Connect Android device to localhost via USB cable

·

2 min read

Hey there! As a developer, we all must have come across the problem where we would like to connect our device to the server running on our local machine. In short, connecting an Android device to the server running as a localhost. It may be to debug client-side code OR to see the behavior of how our server code responds to the device client request.

There are many posts that explain the not-so-easy method to get this done and running. In this post, I am going to share the easiest way to do this. Somehow this very simple way is not explained OR mentioned anywhere in the forum, hence I thought of creating a post to share this information with the developers which can make their development life easy

If you are reading this post, it is assumed you already have these:

  1. An Android device

  2. USB cable to connect your device to the system

  3. Server running on your machine which is accessible using http://localhost:<port>

  4. Additionally, this needs androids tool “adb” in your system

Step 1

Once you connect your android device with a USB cable to your system, make sure that the device can be detected by the adb command. To check this run the following command

>adb devices

This command will show all the devices connected to the system. Make sure that this list contains your device under test

Step 2

Now you just need to run this adb command

>adb reverse tcp:8000 tcp:8000

replace 8000 above with the post number your localhost server is using

That’s it, you should be now able to access your localhost server from your android device. If in case you are running an application on the device, just change the server URL to “http:localhost“. Make sure to use the port information which is used by the server running on the localhost machine.

You can also verify if the changes are working by just opening a web browser on your device and accessing “http:locahost“. It should connect to your server without any issues.

Hope this post helps you get going with your development activity. Please share this with others as well to make everyone’s life easy