Connecting Localhost with external device for testing - android

I am developing android app using Google Cloud Messaging. I am using external server xampp and testing it on external mobile(no emulator as it is slow). This is basically chatting app. I am facing problem in getting connection.
I know i need to use http://10.0.2.2:8080 ip for testing if I am using emulator in my client code.
and somehow I need to use router ip http://192.168.x.xx:8080 if I want to test it on external device. I need some guidance as how to configure things to make it possible.
Thanks
Deepak

You can do it with Google Chrome port forwarding as mentioned below
Step 1:
Enter chrome://inspect/#devices as mentioned below in your desktop chrome window
Chrome window will also show the device name, model number, and the ip address.
Chrome Remote Debugging Devices Detection
Step 2:
Add the port in the ports to forward it and to be accessible on the device as shown below.
Click on the button Port Forwarding and check the Discover USB devices.
You will get a small modal as below, where you can enter your localhost address and access it via app on the external devices easily.
Then, after adding the address, check the Enable Port Forwarding.
You are all set done for accessing it on the external device.
Chrome Port Forwarding Settings for Remote Debugging on Android
Chrome on your Android external device also should be open simultaneously to view the output.
Step 3:
Then use the url opened up in the device in the App to directly test on the External Device
Desktop(Development Machine)
Android(External Device)
Note:
1.The device should be connected with a USB cable to the development
computer which has forwarded the port, else the connection is lost.
2.This mode is not available in Incognito Mode in Chrome

If its external but still local (not on the internet), then both have to be on the same network.
make sure you can access the app its self using the ip address.
Try to enter it into your browser, if you can access the app, then proceed to using it as the ip in android.
you can use tools like Connectify(windows), ap-hotspot(linux) to setup a network ip. which your phone can connect to.

Related

Debug mobile app from Chrome - localhost not resolved

I remember with another phone I was able to debug my local website dev, now there is a problem preventing me to open localhost in my mobile and cannot find how.
Looking at:
Google Chrome for Android Remote Debugging - "localhost:9222" Not Available
my phone is connected, on localhost:9222 I can see a list of tabs.
I can access tabs loading site from remote, but cannot load my localhost.
In one case, error displayed:
408 - Timeout.
On another , simply chrome display site is not reachable.
ERR_NAME_NOT_RESOLVED
I tried both with port-forward enabled as well as not (if enabled, listening to 8081, 8080).
Phone is connected via USB but seems cannot reach my localhost address of the laptop, still on same wifi spot.
EDITED
I remember with another phone I was able to debug my local website
dev, now there is a problem preventing me to open localhost in my
mobile and cannot find how.
I was doing with Bonjour- the "Bonjour host" is the name Mac assign to your user machine user.local. I was able to connect former android host, but now it cannot be solved anymore unless of IP.
The two devices should be on the same network and it won't be localhost in that case, but you have to replace localhost with the IP of the server PC!
So instead of localhost:9222, it would be 192.168.1.10:9222 giving that 192.168.1.10 is your IP address!

(Unity) Network manager not working on android device

I added GameObject. To this object I added two components. (Network Manager and Network Manager Hud). Created cube and added Network Identity(Local Pl.Au). In Player Prefab added cube and deleted from scene. Finally I build&Run on windows. And it works well on windows. when I connect my real Android phone it installed successfully but when I tried press Lan Host or Lan Client it is not responding.The phone is connected to the laptop using a USB cable. What am I missing?
LAN Host or LAN Client will work on your PC, because your PC knows what a LAN is.
Your android device, on the other hand, does not.
To match your android device with another device or your PC, you will have to use a match making service.
To develop and try your app, you can use Unity's match making Service, which can be used in a limited fashion with a Unity Personal license.
When using the NetworkManagerHUD tool, the "Enable Match Maker" button will take you there.
You will need to do a bit of setup to turn on Matchmaking Service on your unity account and server before you can try.
Go to Window > Services.
Perform the necessary steps to create or use your current organization and project, and enable Multiplayer.
You can use LAN on Andriod, you just have to enter the IP address of the Host device into the box where it says "localhost". To be clear, replace "localhost" with IP address of host device( can be andriod or PC)

Android Simulator how does it connect to local IP addresses?

I am trying to implement an Android application in Windows 7 using Eclipse. I am trying to connect the Android simulator to the local test server in my company, but for some reason, it cannot connect to the test server.
If there is any settings or configurations for this, please let me know.
I have tried to do the same from a MAC machine using iPhone emulator and I am facing the same problem. How would this emulator connect to the local servers in my company as currently it all goes to live servers? What configurations are required to be done on the simulators, and how?
If I try to connect to the test server from normal Windows machine browser, I am able to successfully connect to it through the web browser; but when I try to do the same from the Android emulator browser, it cannot connect to it.
The emulator points to the live network and not the local network in my company. This is strange and I know that I need to do some settings for it, but I am not sure where these settings are done and how.
If accessing local computer:
http://localhost:8080/MyLocalServer.html // URL to use in computer browser
http://10.0.2.2:8080/MyLocalServere.html // URL to use in emulator browser
Also try using a local IP to connect to any local servers. Do not use host names.
Dont:
http://mylocalserver.org/
Do:
http://192.168.1.125:portnumber
You have to set up IP-based hosts instead of name-based.
Emulator its - VM. This use virtual network connection. I think you need before chech this connection (this connection may bee stay as NAT, Breedge, Native IP adress, Proxy).
Since you do not work iPhone emulator, most likely you, IP adress virtual network connectionб which uses Emulator, does not match the address area of the local network, and routing occurs
This may help you...
Taken from the android docs:
If you need to refer to your host computer's localhost, such as when you want the emulator client to contact a server running on the same host, use the alias 10.0.2.2 to refer to the host computer's loopback interface. From the emulator's perspective, localhost (127.0.0.1) refers to its own loopback interface.
http://developer.android.com/guide/faq/commontasks.html#localhostalias

How to access a computer from a USB-cable-android-connected device?

I am trying to deploy and test my application on an android device which is then connected to my laptop with a usb-cable. Now, how can I target a webapp deployed on my laptop ?
knowing that my wifi is down and that I don't have the right to change router configuration to do some IP port forwarding tricks (in case you would like to suggest me to use the mobile network e.g 3G, go on internet and get back in via our router external IP address ==> which is also a little bit heavy given what we want to do here)
According to the android documentation, "the emulator" can use the ip address 10.0.2.2 to access the hosting computer but there is nothing for devices...
thanks in advance guys,

Accessing localhost of PC from USB connected Android mobile device

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)
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 , -> 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
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:
first connect your real device with the USB cable (make sure you can upload the app on your device)
get the IP address from the device you connect, which starts with 192.168.x.x:port
open the "Network and Sharing Center"
click on the "Local Area Connection" from the device and choose "Details"
copy the "IPv4 address" to your app and replace it like: http://192.168.x.x:port/test.php
upload your app (again) to your real device
go to properties and turn "USB tethering" on
run your application on the device
It should now work.
I've read numerous forums and tried play apps but not found a solution until now.
My scenario I believe is similar to yours, but I will clarify to help others. I have a locally hosted website and web services to be used by my android application. I need to have this working on the road for demonstration with only my laptop and no network connection.
Note: Using my iPhone as a wifi hotspot and connecting both my pc and my android device worked, but the iPhone 4S connection is slow and dropped out regularly.
My solution is as follows:
Unplug network cables on PC and turn off wifi.
Turn off wifi on android device
Connect android to pc via USB
Turn on "USB Tethering" in the android menu. (Under networks->more...->Tethering and portable hotspot")
Get the IP of your computer that has been assigned by the USB tether cable. (open command prompt and type "ipconfig" then look for the IP that the USB network adapter has assigned)
Open a browser on the PC using the IP address found instead of localhost to test. i.e. http://192.168.1.1/myWebSite
Open a browser on the android and test it works
I did this on a windows computer and it worked perfectly!
Turn on USB Tethering in your mobile.
Type ipconfig in the command prompt in your computer and find the ipv4 for "ethernet adapter local area connection x" (mostly the first one)
Now go to your mobile browser, type that ipv4 with the port number of your web application. eg:- 192.168.40.142:1342
It worked with those simple steps!
Here is a piece of my Android app's code:
This app is able to communicate with a HTTP get-post model between a servlet running on a server and an Android device plugged in USB-Debuggable mode (because the app was in developing progress).
I also can run the app over Wi-Fi when the server, Tomcat Apache 7, running on (when the app development was finished).
To get the IP address of yours
Go to Command Prompt
Type ipconfig
Hit enter
In the list, IPv4 Address is your IP.
Make sure you have adb installed on the computer, USB debugging enabled on the phone, and the phone has allowed access to the computer. Plug the phone into the computer via USB cable, and make sure it's visible (it should show up in the Bash command adb devices.
In your computer's Chrome browser, open chrome://inspect/#devices, click the "Port forwarding" button, check "Enable port forwarding", and add the port on the computer that you want to be accessible from the phone (detailed instructions here). You'll need to keep open the tab running chrome://inspect/#devices.
In your phone's browser, navigate to localhost:[port_number], and it should display whatever is running on the computer.
This works on Windows and Ubuntu Linux, and should work on Mac as well.
How to Easily access LocalHost in Actual Android Device -> Connect your pc with the android device via USB
Go to Chrome inspection click 'f12' or Control+Shift+C
Chrome Inspection tool
Check the bottom of the chrome inspection tool.
Now go to settings in Remote Device Tab.
Remote Devices Tab
check on "Discover USB Device" option as well as check on "Port Forwarding" option.
Now Click on Add Rules, Enter Any Device Port e.g(4880) and in Local Address Enter the Actual Address of the local host in my case e.g (127.0.0.1:480)
After Adding the Rule go to your android studio -> inside your code URL(http://127.0.0.1:4880). Remember to change the port from 480 -> 4880.
Go to Remote Device Tab in Chrome and Click on your connected Device. Add New URL(127.0.0.1:4880)
Inspect the Android Device Chrome Browser
Check your Actual Device Chrome Browser and start Debugging the code on Actual Android device.
Check for the USB connection type options. You should have one called "Internet pass through". That will let your phone use the same connection as your PC.
I very much liked John's answer, but I'd like to give it with some changes to those that want to test some client//server configuration by running a client TCP on the USB connected Mobile and a server on the local PC.
First it's quite obvious that the 10.0.2.2 won't work because this is a REAL hardware mobile and not a simulator.
So Follow John's instructions:
Unplug all network cables on PC and turn off wifi.
Turn off wifi on the android device
Connect android device to pc via USB
Turn on the "USB Tethering" (USB Modem/ USB Cellular Modem / USB שימוש כמודם סלולרי) in the android menu. (Under networks->more...->Tethering and portable hotspot")
This USB connection will act as a DHCP server for you single PC connection, so it'll assign your PC a dedicated (dynamic) IP in its local USB network. Now all you have to do is tell the client application this IP and port.
Get the IP of your PC (that has been assigned by the USB tether cable.)
(open command prompt and type "ipconfig" then look for
the IP that the USB network adapter has assigned, in Linux its ifconfig or Ubuntu's "Connection information" etc..)
Tell the application to connect to that IP (i.e. 192.168.42.87) with something like (Java - client side):
String serverIP = "192.168.42.87";
int serverPort = 5544;
InetAddress serverAddress = InetAddress.getByName(serverIP);
Socket socket = new Socket(serverAddress, serverPort);
...
Enjoy..
Hello you can access your xampp localhost by
Control panel -->
windows defender firewall -->
Advance setting (on left side) --> Inbound Rules --> New Rule --> Port --> in specific local port write your Apache ports --> next --> next
then you can access your localhost by using local PC IP address:
Zsivics Sanel's answer above work fine for USB Debugging but if you want to access your localhost on device for Wifi ADB you can use command
adb reverse tcp:port tcp:port
and use the link http://localhost:port/ on your android to access the http://localhost:port/ of your pc.
This works for USB Debugging as well :)
The detailed answer with explaination is here.
Connecting android phone via USB cable and accessing http server on the Computer.
Firewall - switch it off, once everything works you can add a rule
under Inbound rules, enable Apache HTTP Server (UDP and TCP) (or whatever HTTP server you're using) Warning - if you switch off the firewall, your computer becomes vulnerable to internet attacks. So switch off internet and disconnect from network first. You don't need to be on the Internet for this to work, as you're connecting two devices on local network.
Plug in the USB cable. If networking window pops up, select Home Network. If it doesn't pop up it's probably set to Public (which won't work), so go to: Control Panel\Network and Internet\Network and Sharing Center (in win7) and set it to Home Network there. It might give you a next screen after you press it to share Pictures, Music... just click cancel on it. On the main screen Home Network will still be active.
You need the Computer's IP, localhost or other aliases don't work. To get the IP of your machine, start the Windows command line shell (press Windows Key + R and type cmd), the black command prompt window should pop up, type: "ipconfig" and hit enter. Now look for
IPv4 Address. . . . . . . . . . . : 192.168.###.###
That's the IP that your phone assigned to your computer
Now you should be done, in the phone's browser, try accessing a test html file from your computer's server it should display fine.
Now that everything is working you'll probably want to enable the firewall and Internet access on your computer. Now try accessing your http server again. It probably won't work. Depending on your setup you might get a popup asking you to add a rule to allow it, but if you don't, just add it yourself. Start the windows firewall and go to Inbound Rules (because it's a server) and add New Rule. Select "Program". Locate the exe of your http server, and in the next screen make sure Allow is selected. Try connecting again, and now it should work. If not it's probably because you selected wrong exe. You can even go more advanced to restrict outside usage (because now everyone on the internet can access it if they have your Internet IP address) To restrict access you can right-click on the new rule you've just created, click Properties, and go to Scope tab and change things there accordingly.
Note for Android developers, now in your android code, you have to use "http://", otherwise it won't work. For example: MyAsyncTask.execute("http://192.168.123.123/test.html")
Part II -- Setting IP to fixed/static.
Now that things work, every time you unplug the cable and plug it back in, your IP will change. And if you are using it somewhere like inside a code, you'll have to update it every time! Solution I've come up with is to set the IP to static.
Having the phone connected and tethered, go back to Network and Sharing Center and click on the Local Area connection "Connections:"
On the next screen select Properties, then highlight IP 4 and press properties there and click "Use the following IP address"
Put Default Gateway and Subnet Mask exactly as it was in the ipconfig, and for the IP Address, only change the last 3 digits to anything from 1 to 255
That IP will be your new fixed/static IP and it will remain the same next time you plug the phone.
Problem I faced maybe it will help some one, I was working long time where my pc and android device connected to the same WiFi network and the android device connected via the IPV4 address of the pc with opened port 8080 trought the fire wall.
ONE DAY :
I installed Genymotion emulator in order to debug UI on different screens.
THANKS to Genymotion/Virtualbox network configurations it ruined my WiFi adapter.
The fix was to reconfigure IPV4/TCP settings on the WiFi netwrok adapter :

Categories

Resources