Local network ARP requests from Android app - android

I'm working on an app interacting with Network Enabled devices on a local network.
I'm currently working with IP addresses. However, on the long run, IP addresses will eventually change, breaking the connection. I'd like to store MAC addresses instead, and init my app by sending ARP requests and fetching the matching IP address for each stored MAC.
Is this the correct way to do this?
If so, which library should I use for such a task, and do any of you have a code example?
Many thanks!

You will probably need to access low-level components which Android & Java don't allow you to. Maybe with a rooted device ?

Related

Get the MAC address of a local network device knowing its IP

I have a device on my local network. I know its MAC address, and want to find its IP. Because android doesn't seem to allow sending direct ARP queries (unless I'm mistaken), I've decided to take another approach: I scan all available devices on the local network.
That's where I'm stuck. I now have a list of IPs, and would like to check their MAC address to find which one is the one I'm looking for.
Could any of you please recommend a way to proceed?

How to use Android_ID to communicate with other device?

My router shows
14:AA:BB:CC:DD:46 android-################ 192.168.3.112
AC:AA:BB:CC:FF:46 MY-LAPTOP 192.168.3.112
AC:AA:BB:CC:FF:AC NAS 192.168.3.112
Now if I use ftp://NAS/ it works, but
if I use ftp://android-################:7721/ it does not work, again ftp:/192.168.3.112:7721/ works, I don't understand what is the use of Android_ID then.
I thought since Android IP keeps varying on router we can find android by using Android_ID, but that's not the case.
Can anyone explain why is it happening?
Your Android device uses DHCP to get an IP address from the router on your network. While a router will try to use the same IP address across sessions, there's no guarantee you'll always be handed the same IP address. You generally do not want to make any assumptions about it not changing.
Assuming you have client software that's trying to talk to a service running on the Android device you need to implement some sort of network discovery mechanism. Android has a mechanism in the SDK that uses mDNS called "NSD". See https://developer.android.com/training/connect-devices-wirelessly/nsd. You could also use UDP broadcast.
"android-####" is a local hostname. Unless you have registered your Android device with a DNS server serving your local network you need to use IP addresses (and in that case you'd need to configure your router to also give your Android device the same IP address).

Finding unused IP addresses

I have tried searching for this, but nothing has met what I need so far. I am writing an FTP server application for Android, but I do not know how to find an IP address for the server that is not in use.
I have so far been guessing and hardcoding in an address. Sometimes it would work and sometimes it wouldn't. How can I get my app to consistently generate and connect to an open, usable IP address?
Thanks!
The standard solution to the problem is to obtain a DNS name (usually at a small expense), and then use a dynamic DNS service to keep telling the DNS hosting provider the current IP address for the DNS name.
Then clients can lookup your IP address from a well-known name.
There is also an indirect solution to the problem. You can get a infrastructure provider to host the FTP service. For example, you could run a free Amazon Web Services micro instance. Whilst it is running it should be on a constant IP address. Then both your clients and your app can use this as a fixed intermediate relaying point. This also solves the transient nature of mobile devices which might not be available to respond at a particular time.

How can I send http messages from android app( physical device) to my local server without using internet connection?

I have an app that runs in an emulator. The app sends multiple messages to my local server, for manipulating data. While i was using the app in my emulator i was using:
http://10.0.2.2/path
to reach the server. I would like to use a physical device to connect to the server. Does somebody know how to do this. What adress i should use?
Assuming that your devices are on the same Network (connected to the same router) as your computer, you can expose your computer's web server (Apache, presumably?) and then point your app directly at the local network IP address that your computer has (usually something like 192.168.1.x). I have found that the least painless way to do this is to use MAMP or WAMP or LAMP depending on your OS, but of course I have no idea what your specific situation is, so that solution may in fact make things a lot more complicated rather than less.
In any event... your computer is on the network at a specific IP address, so it's just a matter of making sure that it's willing to serve this data on whatever port you are looking for it on. If you provide more specification as to your circumstances, we can give a more detailed answer.

How to communicate without prior knowledge of IP

Im developing an app which uses the tcp connection. currently im communicating using hard coded IP addresses as a sample, but in the real world this is not the case i think. We come across any mobile and start communicating/sharing etc without having any prior knowledge of other person's IP. In such a case how to get the IP address of those who are using my application. How can i communicate without the prior knowledge of the IP address. How to implement this. Help me in sorting it. Thanks in advance.
EDIT:
And in case if the user connects the internet thro' GPRS/3G connection then his IP address will be changing based on the service provider. How can i find that.
Given what you are commenting on other answers, if what you want are the IP addresses of other Android devices to do some kind of P2P game you should note that there is no reliable way to do that directly.
If the users are connected in the same LAN you could provide in your application some kind of discovery service using UDP broadcasts.
If the users are connected to the internet and have public IP addresses then you could use some intermediate server to register the users at startup and have them discover other users using that server.
But (and this is the most common case) if the users are connected to the Internet and have private IP addresses (like when they are connected with WIFI on their LAN) you need some kind of server that acts as a proxy for their requests because there is no way to make a direct TCP connection from a natted IP to another natted IP. There are a few solutions to solve this problem, you could start for example by learning something about XMPP and how it works.
Your app must use DNS. You will first need get your IP into the DNS system. You do this by signing up for a domain name & setting up A / AAAA records for hostnames with a hosting provider (or you could set up your own DNS server). You may even find some free DNS providers.
In your app, you can hard-code a fully qualified domain name that you previously set up, say - app.foo.com and use the android gethostbyname library call to fetch the IP address for you. The local DNS resolver will then go to its DNS server and fetch the IP address corresponding to app.foo.com.
Can you use DNS? You can still hard-code the domain name if you want, and the domain name can be configured to point to any IP addresses anytime later.

Categories

Resources