0

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).

Micah Vertal
  • 564
  • 1
  • 7
  • 16

1 Answers1

1

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.

Leon
  • 8,404
  • 2
  • 9
  • 52
  • I apologize for not responding @Leon. Is it possible to do this with the Android NDK? If so, how would I get started using it with Xamarin.Forms.Android? I have heard it allows more low-level access, and the caveat of not being able to publish it on the app store isn't a problem for me (the app is for my use only). – Micah Vertal Jan 25 '19 at 21:50
  • I am not familiar with Android NDK. So i do not give you result about yes or no, but Xamarin.Forms.Android do not achieve it.Xamarin.Forms.Android just be used to develop android App. – Leon Jan 29 '19 at 09:45
  • Okay. I will hold off on selecting this as the answer in case someone else has input on this situation. Thank you for your help. – Micah Vertal Feb 02 '19 at 23:48