I'm writing an Android Kiosk application for a closed business environment. My devices are connecting over Ethernet to a local web server to show webpages.
Now the problem I have is, that in the night, the server will be switched off, and if that happens, my DHCP server is off too. If my Android starts up in this situation, it won't get an IP address. My Kiosk application tries to connect to the server all the time (At this point I don't care if it does, when it's in a Power saving mode or not).
To make it more complicated, it's not just one site, my app will run on, so using static IP addresses for the server won't work. I managed to get a DHCP and DNS server, where I can define my server host name, which will be the same at all sites.
BUT, Android has a bug resolving not fully qualified domain names (Link), so my application can't directly connect to the host name (e.g. http://hostname/index.html), but has to resolve the IP address of the hostname manually and then connecting to that IP address. As the only solution I could find was dnsjava (Link), I used that to resolve the IP.
import org.xbill.DNS.*;
InetAddress addr = Address.getByName("hostname");
ipAddress = addr.getHostAddress();
This actually works, BUT here's the thing:
If Android and my starts up with no IP, it of course can't resolve the hostname. However if my DHCP/DNS server starts up, the device won't get an IP address unless I tell him to do so:
runShellCommand("killall dhcpcd");
runShellCommand("ifconfig eth0 192.168.99.99");
runShellCommand("ifconfig eth0 0.0.0.0");
runShellCommand("dhcpcd eth0");
runShellCommand will ... run a shell command and actually works well.
Anyway, this will get a new IP address from the DHCP (proven by the Log files of the DHCP server), but the hostname will not be resolved.
Restarting the app won't work, even using another app for testing (Ping & DNS) won't work.
The only way it works is when in Settings->Ethernet I unclick DHCP and click it again. Somehow this flushes the DNS cache?! But how to do that programmatically?
BTW: I tested to start my App and Android when the DHCP/DNS server is online, it will connect and work and when I switch off the server, my Android always answers DNS lookups with this IP address, even I don't have a local IP anymore.
For me it seems, that Android does do a flush when changing the Settings DHCP to fixed IP and back.
I hope someone can help!