Android - Override DNS for dynamic IP - android

I want to make use of my own DNS on Android in general. Whether the IP is static or taken via DHCP. So I put the
Settings.System.putString(getContentResolver(), Settings.System.WIFI_STATIC_DNS1, mIP);
But then I suppose it works only for static IP.
How can I override the DNS setting for every case?
Thanks in advance,
Mike

An alternate solution may be to have your application perform the DNS lookups itself using:
DNS Service Provider for the Java Naming Directory Interfaceâ„¢ (JNDI)

Related

Xamarin.Forms - Set WiFi Subnet Mask on Android Device

I ran into an issue where my Android app cannot make outgoing connections using the TcpClient class (question located here). While debugging and conducting further research, I found that my Android device (API level 22) is receiving a subnet mask of 0.0.0.0 when it connects to my WiFi network. My DHCP server is configured to use 255.255.255.0, so I believe this discrepancy could be the root of my other problem. This is my code:
//Connect to the WiFi network
int id = WiFiManager.AddNetwork(new WifiConfiguration()
{
Ssid = $"\"{ssid}\"",
PreSharedKey = $"\"{password}\""
});
WiFiManager.EnableNetwork(id, true);
WiFiManager.Reconnect();
//Retrieve subnet mask (for debugging)
int subnet_mask = WiFiManager.DhcpInfo.Netmask;
subnet_mask returns 0, and the formatted version of the DhcpInfo class shows:
{ipaddr 10.0.0.15 gateway 10.0.0.0 netmask 0.0.0.0 dns1 10.0.0.0 dns2 0.0.0.0 DHCP server
10.0.0.0 lease 43200 seconds}
With this being the situation, is there any way I can manually change the subnet mask (or "netmask") within the Android API? I have tried using a static IP configuration as shown here, but use of those settings was deprecated in API level 17. I have also tried setting the DhcpInfo.Netmask property manually, but it takes an int. I calculated one based on this answer, but it was too large and became a uint. Lastly, I looked through a variety of classes, including Android.Net.Wifi.WifiManager, to see if there was a way to change the netmask. I didn't find anything, but I might have just been looking in the wrong places.
Any help is greatly appreciated!
Update:
For security reasons, it seems like this is not possible with the standard Android API. However, is the Android NDK able to change the subnet mask? I understand it provides lower-level access to the device, and I do not need to put this app on the app store (it is for my use only).
For security reasons, Android OS may not allow you to do this.
I know as a user I would not want an app to change my network configuration.
It seems this may have been able to be done in the past: https://stackoverflow.com/a/7142316/2913599
but that API is obsolete/deprecated in API level 17: https://developer.android.com/reference/android/provider/Settings.System.html#WIFI_STATIC_DNS1
Docs say to use WifiManager instead, but that API does not allow changing the ip, dns, gateway, again for security reasons.
If you need to get these settings changed, perhaps you can provide an instruction page for the user so they can change them themselves.

How to Change WiFi to static IP and set IP Address Programmatically in Android 5?

I've tried the solution posted here How to configure a static IP address, netmask, gateway, DNS programmatically on Android 5.x (Lollipop) for Wi-Fi connection but have not been able to get it working.
It compiles and runs fine without crashing, but the IP address stays DHCP and the IP address is not set. Maybe it has to do with the way I am calling test() from another method? To call the test method it asks for an input parameter and the only this I have been able to get it to accept is 'this' so the method call would look like test(this).
Any help would be greatly appreciated.
EDIT:
I could be this part of the example that I screwed up:
WifiConfiguration wifiConf = ... /* Get Wifi configuration you want to update */
Does anyone know what the ... should be replaced with?

in Wifi Direct the group owner address always be 192.168.49.1 or it changes?

in Wifi Direct the group owner address always be 192.168.49.1 or it changes?
I'm new to android. please help me.
Thanks in advance.
cheers.....
Though I strongly believe that there should be no implementation where you need to directly rely on the IP Address of the GroupOwner, according to Android Source Code(Search for SERVER_ADDRESS in the file) the IP Address of the GroupOwner is 192.168.49.1 and is final and static.
Word of Caution: This might get changed anytime in the future, and hence, you must never rely on the value itself. You should always use isGroupOwner to find out whether a device is GroupOwner or not.
This seems to be IP address 192.168.49.1.Due to DHCP it will change-unless you have a static IP.

android resolve .local (mDNS)

I'm looking for a solution to resolve .local host names with Android 4.0.4 (no NSD, due to API level 15). On the device I don't have any service to discover, just the hostname.
Could you please provide an example how to resolve? I integrated the jmDNS, but don't know how to use for host name resolving.
win-cmd:
ping kcmeasurement.local
Pinging kcmeasurement.local [10.202.0.29] with 32 bytes of data:
Reply from 10.202.0.29: bytes=32 time<1ms TTL=64
...
Thank you,
Daniel
I had almost the same requirements as your question, apart from the requirement to use jmDNS, so I solved it with NSD. I realize this doesn't address your question exactly, but thought it might still be somewhat helpful for yourself and others to see how I solved it.
I setup an NSD discovery listener and an NSD resolve listener, and within the discovery listener code, added a filter for the target host name (e.g. "kcmeasurement", or in my case, "garagedoor").
There is a blog post here which explains in detail how to do that. Refer to steps 3-4, which are dealing with the Android App code required.
http://www.dodgycoder.net/2015/02/setting-up-bonjourzeroconfmdnsnsd.html
For your case, I would imagine you would have to do the equivalent process but just using the jmDNS library instead of NSD.
You should be able to use the InetAddress class to resolve the hostname for a given IP address. For example, using the IP address provided in the original question, try the following:
try
{
String hostname = InetAddress.getByName("10.202.0.29").getHostName();
}
catch (UnknownHostException e)
{
Log.e("MyApplication", "Attempt to resolve host name failed");
}
Since this is a network operation, make sure that it is not performed on the UI thread.
EDIT
You should be able to resolve a local hostname with jmDNS as follows:
InetAddress localHost = InetAddress.getByName("10.202.0.29");
JmDNS jmdns = JmDNS.create(localHost);
String localHostName = jmdns.getHostName();

how to change port 80 android

I made a webserver on my android device(using Nanohttpd). It's working fine over the port 8080
but I want to make it okay over the port 80,(I want to tape on my browser : http://192.168.x.x instead of http://192.168.x.x:8080/ ) but I'm not able to do it
This is how I call the method that creates the webserver:
httpServer = new NanoHTTPD(80,Environment.getExternalStorageDirectory());
Can someone tell me what service is running by default over the port 80 ?
How can i fix this problem ?
I finally found a way to fix that, for those who have the same problem, here is the solution
I rooted the phone using UnlockRoot
then I installed Port redirector from the play store and I forwarded the traffic from port 80 to port 8080, and now my web server is available from the address: 192.168.x.x
Hope it will help :)
Ports below 1024 are restricted on Unix like systems. You need superuser privileges to bind to these "well-known" ports.
List of these "well-known" ports on wikipedia
Check out line 89 of the server source:
https://github.com/NanoHttpd/nanohttpd/blob/master/core/src/main/java/fi/iki/elonen/NanoHTTPD.java
I don't even see a constructor that type-matches your arguments. That might be part of the problem.

Categories

Resources