How can I get netmask from Android device? - android

I need to get the netmask and ip from the Android so I can calculate all the IP's within my subnet range.
I've been searching and so far I've found this:
Programmatically getting the gateway and subnet mask details
The problem is that it doesn't work for all devices. For instance, it doesn't work in the emulator (any Android version) and it doesn't work either in my Archos 7HT with Android 1.5.
It returns 0 for netmask and gateway.
Is there any way to do it? Is there any system file which I could read that contains this info?? (something like this: http://www.flattermann.net/2011/02/android-howto-find-the-hardware-mac-address-of-a-remote-host/).
In my advanced wifi settings I can see these settings, so there must be a way to retrieve them...
Thanks.

Try with APIs InetAddress and NetworkInterface. Emulator has no WIFI, hence is not possible to get address with Wifi API.
Check this reply

Related

Possible to route network traffic to a different gateway programmatically?

I have an Android tablet with a USB to ethernet adapter. The network interface is configured and the default gateway works and allow internet access.
To fix an issue we are having I would like to change the default gateway to something that is incorrect which will cause the tablet to not be able to access the internet.
Then in our Xamarin application I would like to route the internet traffic through the USB to ethernet NIC like normal but I want to programmatically specify the correct gateway somehow without changing the gateway on the NIC. I can also remove the gateway altogether if that would do the same thing and block traffic. But in that case do I have to do anything special in the traffic to send the traffic to the 192.168.1.1 gateway?
Is this technically possible at all in Xamarin or Java/Android? I am also open to somehow configuring Android itself so that only our application has a valid routing table but I am not sure how to do that.
Thanks ahead of time!
I have not tried anything yet to fix this I'm just brainstorming issues.
The document is about WiFiManager. There is a method. But this method was deprecated in API level 29.
At present, the successful changes are all in Android 6.0 and earlier.
Here are two links you can refer to them:
How to configure a static IP address, netmask, gateway programmatically on Android 3.x or 4.x
Set static IP and gateway programmatically in Android 6.x (Marshmallow).

Android ignoring router's DNSMasq when using a "real" domain

If I use DNSMasq in my router to set a fake address to an ip, then the Android phone can find it no problem.
Example:
DNSMasq
address=/fake.notreal/192.168.1.112
On the Android phone using chrome to go to the address or ConnectBot to ping the address fake.notreal works. It finds the correct server.
But if I use a real address, then it no longer works (I need this for testing of a website I'm developing). Example:
address=/mycompany.com/192.168.1.112
This address goes to the actual real server.
How to I make the android device listen to the router?
I was able to "fix" this by going into the Wi-Fi settings on the phone and:
Long press on the current wi-fi network
Choose "Modify"
Change to a static IP
Give it a static IP (this isn't the fix, but it's required to do the fix)
Under DNS, make the first one your router (e.g. 192.168.1.1)
Save
Turn off wifi
Turn wi-fi back on
At that point, it'll first ask your router and that'll return the local address.
On your Android phone, if you have root access, if you add
line 192.168.1.112 mycompany.com to your /etc/hosts I think that should work. You'll bypass the DNS query and go straight to 192.168.1.112.
If you don't have root access to the phone try applying that to your router's /etc/hosts but I don't think that would work then.

How to set a static IP in Android Things running on Raspberry Pi 3

I wish to change the Ethernet IP Address of the Raspberry Pi3 board that is running Android Things OS. Currently it automatically fetches an IP Address available in the connected network. But I wish to assign a fixed/static IP to the device. Is it possible to set a static IP using ADB?
I don't know the solution based on Android-things.
But I usually try to prevent the use of fixed IP (unless the device runs forever) set in the device itself.
I prefer to use the DHCP of the modem to set the IP based on the MAC adress. This prevent some conflict.
You can see more information on this post
You can set a static IP usually through your router settings. Try accessing your router settings usually through the web browser at:
http://192.168.0.1/
Assuming you have never accessed this the username is usually admin, and the password is password. You can check the router name online to find the default username and password.
Navigate the menus to find a page which shows you he connected devices. See pic for an example.
Every routers interface is different.
If you have a setting that says DHCP see if you can change it to a reserved IP so that the IP address assigned doesn't change.
Remember different routers have different interfaces so you must do some searching about yours to find the setting.
You can do this from any computer connected to your internet, doesn't have to be on the raspberry to make the changes.

How can two machines detect each other when their ips keep changing?

Let me describe what I want to achieve and where I am stuck. It is okay to suggest an entirely different alternative.
I have an Android phone with NFC feature.
I have a macbook pro.
A wifi network.
Whenever I tap my phone on a certain NFC tag I want my Macbook to run a certain script.
So I did the following
An android application that is invoked whenever the device senses a particular NFC tag.
My Macbook runs a NodeJS HTTP server
Android app sends an HTTP request to NodeJS, NodeJS then invokes whatever script I wanted to run.
This setup works perfectly at my home where I can Set the IP address of my Macbook to whatever I want. But i want the same setup to work transparently when I am in starbucks (or workplace) and both my Android and Macbook are on the same Wifi network. (I dont want to enter the IP addresses manually).
When I was halfway through I realized that I could have used bluetooth feature on the Macbook but then I will probably have to write a sophisticated program on macbook to listen to the Android device and accept commands.
On Android, use Network Service Discovery. If you're using an earlier target, jmDNS is your friend. I have only used this to allow android devices to find each other, but this should be compatible with bonjour service on your Mac.
Solution #1
Use a network service discovery technology. I would perhaps suggest Zero Config which Apple implements as "Bonjour"
Solution #2
Have a third device with a known IP address for bookkeeping the other devices' IP (essentially what DNS is)
Solution #3
Setup your WiFi network to have static IPs : allocating predefined IPs to computer with specific MAC addresses (that way clients can still have "DHCP" but the router would always allocate the same local IP)
I'd look into dynamic dns. You can use that to automatically update dns names for a computer that changes IP addresses.

How to get an IP address from Mac address programmatically on Android?

Programmatically, I got Mac addresses of other Wifi enabled devices from my device on the same network. Same I need to apply here. Is there any apps or any other possibilities to get an IP addresses of each wifi enabled devices on the network by using those Mac addresses on Android?
If you're looking for an android app, you could try Network Discovery which work very well and can list all the devices on your local network with the corresponding IP and address.
If you're looking for code instead, well the application is available on github and does (amongst other things ) basically the same thing than explained by Yahel : it reads /proc/net/arp .
Note : I'm NOT the author of this application.
This person does the exact opposite so it should work for you.
The trick is to read the ARP cache table from the device.
http://www.flattermann.net/2011/02/android-howto-find-the-hardware-mac-address-of-a-remote-host/

Categories

Resources