Accessing an API on an Android device over the Internet - android

I have a use-case for Android devices that I can't seem to wrap my head around. The gist of it is I want to be able to send an API command from my server to my Android app over the Internet. The solution already works if my server application is hosted on the LAN: each Android device has an IP, and I send the command to each device's IP. What if my server is on the cloud? Is there a way for my server application to connect to my Android device directly without the need for a local server?
In this use-case, the Android devices are connected to network through either WiFi or ethernet, and the network has internet access.
Any suggestion would be wonderful, thank you!

When you are in local area network, both your server and client (Android device) are assigned with virtual IPs so there is no problem with see each other.
But when your move your server application to cloud, it cannot see your Android device anymore since your device is hidden by NAT. Only the gateway(your wifi router) IP can be seen by the server.
Some ways you can try:
Assign a static IP for your Android device, and configure your gateway to make a port forwarding to redirect the network traffic to your device. Then your server can see your device by sending to your-gateway-ip:port. But this is usually not a preferred way, unless you can always have full control of the client side network environment.
Change the application protocol to something like MQTT. In such case both your server and client can connect to this "broker" server so exchanging data is possible regardless of your client is under NAT or not. However, you may need to setup this extra broker server.
Find other third party notification services to do this server to client communication, such as Google Firebase.

Related

If I connect 2 android devices via WiFi-Direct - can one share its Internet connection with the other?

I am building an SDK for a client to connect 2 android devices for exchanging data (strings, commands...) via various channels, such as Bluetooth, USB cable, WiFi.
One of the devices is a standard android phone, with a SIM card and data, thus able to access the internet. The other has no SIM card.
Currently using WiFi I am opening a HotSpot on the phone device, and so the non-SIM device can access the internet via the HotSpot.
Now my client wants me to connect via WiFi-Direct, too.
So my question is - once I connect the two devices via WiFi-Direct, will I be able to access the internet on the non-SIM device, using the SIM/data on the "normal" phone?
Thx
Well, Wi-Fi direct can create hotspot independent of the legacy Wi-Fi hotspot BUT this hotspot doesn't share internet and any request to external ip will be dropped.
But for android you can use NetShare app to do that, you can download it from
here.
it works as follows:
• in the client side NetShare use the vpn service to catch all internet traffic of the device and send them to NetShare in the server device and wait for the response.
• in the server side NetShare run a server with specific port to receive the internet packets sent from the client side in
previous step, it send these packets to the internet, after receiving the reponse from the internet it will send these responses to the NetShare in the client side which in turn provide it to the back to the client device.
for more details see the official website

Server on Android reachable from devices not on local network?

I have been trying to set up an app that allows communication between two devices, a server and a client. I have established the connection and can send messages between the two as long as they are connected to the same source of internet. When I connect the devices to two different sources, I am not able to establish a connection between the two. It works by entering the IP address of the server you want to connect to.
For example, if my server device is connected to my home Wi-Fi and my client device is connected to the same Wi-Fi router, communication works as supposed to. But, when I connect the server to Wi-Fi and my client to cellular data (or different Wi-Fi router), the client is not able to find the server.
From all the searching I've done, I found that it is because my server is bound to my local host. So my question is, How can I make my server reachable from anywhere regardless of what network you are connected to?
I used this tutorial to get my server running:
http://android-er.blogspot.com/2014/02/android-sercerclient-example-server.html
The very first comment is a question that asks why it only works on a local network and someone answered saying you have to set up port forwarding on your router. From what I understand, this will work only if the server device is always connected to the same router. This will not be the case for my app. The server will have a new IP address as the device changes networks. I want this IP address to always be reachable no matter what network the server is connected to.
You will need to create a relay server. This would be set up using a java application on the PC and it would manage virtual "rooms" that then relay the messages to the clients. The computer you run your relay server off of will need port forwarding but the clients will not. Much of the code from that tutorial could be applied to this concept.
See: How to create a java Server that accepts client connections and then build a relay connection for a client pair
If your server is behind a firewall, you will not be able to reach it unless that port is open on the firewall. The only way to avoid this is to have a non-firewalled server.

Testing the server side code before deploying

I need to send an image file from the mobile to the server(where the server will save it to the hard disk). I have wrote both the android mobile part and the server side. I need to test the code before deploying to the server. Will the code work if I connect the mobile to the WI-Fi network through which I can have a LAN connection to the system? Is there any way I can test the code with out using a WI-Fi connection(ie Connecting phone to the system via the usb cable and then forming a LAN or something)? Please voice your valuable opinion in these stuff.
When I was testing my server-side I didn't find any other way rather than connecting via wifi, this way it was working just fine (in your code you need to use ipv4, not localhost, just as reminder). I'm not good with network stuff, but I don't think it's possible using usb to create some kind of LAN.
Have u tried from emulator? How to connect to my http://localhost web server from Android Emulator in Eclipse

How do I get my Android app to communicate with the server over a VPN?

I have an android application that talks to a server app running on grails (Groovy on Grails).
Android app basically establishes a connection with this address:
url="http://192.168.2.53:8080/tma/majBtm/androidToDesktop";
It all works fine when both the server and the android phone are connected in the same lan network.
Now the problem arises when I want the phone to talk to the server while the phone is outside the local network (I need it to use edge).
I came across VPN networks which I think might help me out. (the server has dynamic IP)
So what i did was go to dyndns.com and created a new hostname and have the app successfully run on the server. The host I created was - tmagrails.dyndns.com
How do I make my phone connect to this network? What changes am I supposed to make in order to get things working?
Also, I really want to know if this is the right approach. If not, is there any way to get things going for me?
Please advice.
First you need to set up port forwarding so that port 8080 is forwarded to your 192.168.2.53 ip address. A google search on your router model should tell you how to do this. If you want to make your app more secure you will also need to look at SSL and TLS While this won't garantee your app is secure it will stop the network traffic to your app being easily read if you were to access it from an open wifi hotspot.

Using Android phone as wifi web server

I got this crazy idea that I don't know is possible or not... what I want to do is set up my phone as wifi access point, then allow people to connect to it, but handle all incoming http traffic myself.
So:
phone wifi in access point mode; open wifi network (this tidbit works - yes I know it's not officially supported but then I have no intention to distribute put this app on the open market or so, it works on my phone and that's good enough for me).
client can connect to the phone (e.g. my laptop: this also works).
when client tries to open an http connection to any random server, this has to be intercepted by my app and handled by a local web server. This is the point that I'm stuck on.
The web server part shouldn't be too hard; there are (open-source) web servers available. But getting the traffic to that web server, there is the problem.
The behaviour I'm after is a bit similar to what you get when connecting to some open hotspot, like hotel hotspots, that then redirect you to a login page. This intercepting of the connection request (I suspect I have to look at DNS requests?) is what I'm after.
I have seen an app that is doing this but they require the phone to be rooted. I'm hoping to find a way to do it without rooting the phone.
Wifi hotspots would usually use IPTables to redirect port 80/443 traffic to a local web server.
I have run squid on linux machines in the past on port 3128, then sent all port 80/443 traffic to squid. Then I used squid redirect any url that I didnt explicitly allow to a local web server.
IPTables is accessible on rooted android devices only though.
And I doubt there are many proxy servers available on android, so you would have to send HTTP traffic to a proxy server over the network connection.
Like has been said, you'll need IPTables, but you'll also need a proxy server, like Squid.
Google "transparent proxy with squid", or check here:
Linux: Setup a transparent proxy with Squid in three easy steps
you do not need to have a proxy, you just need a full LAMP stack on your device and IPtables, you can mark all packets that have not been authenticated to forward through to your local page, then using php change the rule for that IP address once the address has authenticated, you could do the same for mac addresses if you wish, you would then also need a daemon that periodically reset each exception

Categories

Resources