0

I am working on a Xamarin.Android appplication on a tablet device and am working to build an activity that has the capability to connect to any available wifi networks and set separate static IPs for each of them when communicating with the device.

The closest I have come to the solution till now is Android.Net.Wifi.WifiManager.ConnectionInfo.IpAddress but that is only a getter, and that to the currently connected wifi network:

public virtual int IpAddress { get; }

Is this at all possible? Thanks!

SauravBhattacharya
  • 631
  • 3
  • 9
  • 24

1 Answers1

0

For getting the list of available wifi networks, you can use the ScanResults Property of Android.Net.Wifi.WifiManager Class. For example:

var wifiManager = (WifiManager)GetSystemService(Context.WifiService);
//open wifi
if (!wifiManager.IsWifiEnabled)
    wifiManager.SetWifiEnabled(true);
var wifiList = wifiManager.ScanResults;

To do this, you will need to enable all wifi related capabilities in your app's manifest like:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

To set the static IPs, there is no open API can do this work. And the Apis like Android.Provider.Settings.System.WifiStaticGateway are obsolete, usually we use reflection in tradition Java android application to set the WifiConfiguration like in this case: How to configue a static IP address, netmask, gateway programmatically on Android 3.x or 4.x.

Although here in xamarin, the default WifiConfiguration is like this:

{* ID: -1 SSID: null BSSID: null PRIO: 0 KeyMgmt: Protocols: AuthAlgorithms: PairwiseCiphers: GroupCiphers: PSK: IP assignment: UNASSIGNED Proxy settings: UNASSIGNED {LinkAddresses: [] Routes: [] DnsAddresses: [] Domains: nullMTU: 0} }

but after some researching, I found that fields like ipAssignment in default Android WifiConfiguration.java are not available in xamarin.android app. We don't know how Xamarin.Android encapsulate its WifiConfiguration.

I couldn't find a way to change its IP assignment and Proxy settings, so I personally doubt setting static IP address can be done unless we know how WifiConfiguration of Xamarin.Android looks like.

Community
  • 1
  • 1
Grace Feng
  • 16,564
  • 2
  • 22
  • 45