Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing localhost of PC from USB connected Android mobile device

Tags:

android

I have an android device (Samsung galaxy tab) connected to my PC via USB .

I want to use WebServices and run a web page which is located on my local xampp server of my PC on my android device .

I can achieve it if I have both , my PC and android device , connected on the same wireless network .

However is it possible to access the localhost page without any wireless connection , using only USB connection .

(PS : This same scenario works when I run the app on an emulator and use the ip 10.0.2.2 to access the localhost on my PC)

like image 293
saurabh Avatar asked Mar 27 '12 10:03

saurabh


People also ask

Can I access localhost from another device?

On your mobile device's browser (any will work), navigate to http://<Local IP Address>:<port number> . For example, if I was serving on localhost:8080 and my local IP address is 123.45. 67.890, on my mobile device's browser I would navigate to http://123.45.67.890:8080 .

How can I access my localhost remotely?

Open any browser you like and type http://localhost:9999 and it will work like a charm.

Can I connect an Android phone to a PC using USB?

With a USB cable, connect your phone to your computer. On your phone, tap the "Charging this device via USB" notification. Under "Use USB for," select File Transfer. A file transfer window will open on your computer.


2 Answers

Google posted a solution for this kind of problem here.

The steps:

  • Connect your Android device and your development machine with USB debugging enabled
  • Open Chrome in your development machine, open new tab, right click in the new browser tab, click inspect
  • Click the three dots icon on right top side three dots, -> More Tools, Remote Devices.
  • Look at bottom of the screen, make sure your device name is appeared on the list with Green colored dot.
  • Look below at the settings part, check the Port forwarding mark
  • Add rule. Example, if your python web server is running on your machine localhost:5000 and you want to access it from your device port 3333, you type 3333 on the left part, and type localhost:5000, and click add rule.
  • Voila, now you can access your web server from your device. Try open new browser tab, and visit http://localhost:3333 from your device
like image 173
Alfat Saputra Harun Avatar answered Oct 10 '22 04:10

Alfat Saputra Harun


I finally solved this problem. I used Samsung Galaxy S with Froyo. The "port" below is the same port what you use for the emulator (10.0.2.2:port). What I did:

  1. first connect your real device with the USB cable (make sure you can upload the app on your device)
  2. get the IP address from the device you connect, which starts with 192.168.x.x:port
  3. open the "Network and Sharing Center"
  4. click on the "Local Area Connection" from the device and choose "Details"
  5. copy the "IPv4 address" to your app and replace it like: http://192.168.x.x:port/test.php
  6. upload your app (again) to your real device
  7. go to properties and turn "USB tethering" on
  8. run your application on the device

It should now work.

like image 40
Zsivics Sanel Avatar answered Oct 10 '22 03:10

Zsivics Sanel