SDF and WiFi: Connecting to APs

One of the more common question groups I see about the Smart Device Framework really revolves around connecting to an access point.  Connecting to an Open AP is pretty straightforward, but as soon as you need to start adding encryption, privacy, passcodes and all that fun stuff, people tend to go off the rails.  And I can’t blame them, the WirelessZeroConfigNetworkInterface class isn’t the most intuitive interface.  What I’ve done in my own projects is to create a WiFiService class that wraps all of this (and a lot of other stuff too).  I’ll try to get that full service to a publishable state in the not-too-distant future, but the core pieces for connectivity boil down to these few methods:


 

public bool ConnectToOpenNetwork(string ssid)
{
if (!WiFiSupported) throw new NotSupportedException(“No WZC WiFi Adapter detected on this device”);

return ConnectToNetwork(ssid, null, false, AuthenticationMode.Open, WEPStatus.WEPDisabled);
}

public bool ConnectToOpenNetwork(IAccessPoint accessPoint)
{
if (!WiFiSupported) throw new NotSupportedException(“No WZC WiFi Adapter detected on this device”);

return m_wzc.AddPreferredNetwork(accessPoint);
}

public bool ConnectToWEPNetwork(string ssid, string wepKey)
{
if (!WiFiSupported) throw new NotSupportedException(“No WZC WiFi Adapter detected on this device”);

return ConnectToNetwork(ssid, wepKey, false, AuthenticationMode.Open, WEPStatus.WEPEnabled);
}

public bool ConnectToWEPNetwork(IAccessPoint accessPoint, string wepKey)
{
if (!WiFiSupported) throw new NotSupportedException(“No WZC WiFi Adapter detected on this device”);

return ConnectToWEPNetwork(accessPoint.Name, wepKey);
}

///


/// This connects to a WPA network using TKIP encryption.
///

///
///
///
public bool ConnectToWPANetwork(string ssid, string passphrase)
{
if (!WiFiSupported) throw new NotSupportedException(“No WZC WiFi Adapter detected on this device”);

return ConnectToNetwork(ssid, passphrase, false, AuthenticationMode.WPAPSK, WEPStatus.TKIPEnabled);
}

///


/// This method will only connect to a WPA2 network using AES.
/// If you need fallback to WPA (using TKIP), use the overload that takes in an AccessPoint
///

///
///
///
public bool ConnectToWPA2Network(string ssid, string passphrase)
{
if (!WiFiSupported) throw new NotSupportedException(“No WZC WiFi Adapter detected on this device”);

return ConnectToNetwork(ssid, passphrase, false, AuthenticationMode.WPA2PSK, WEPStatus.AESEnabled);
}

///


/// Connects to a WPA or WPA2 (AES or TKIP)
///

///
///
///
public bool ConnectToWPANetwork(IAccessPoint accessPoint, string passphrase)
{
if (!WiFiSupported) throw new NotSupportedException(“No WZC WiFi Adapter detected on this device”);

// quick validation
var valid = accessPoint.AuthenticationMode == AuthenticationMode.WPA
|| accessPoint.AuthenticationMode == AuthenticationMode.WPAPSK
|| accessPoint.AuthenticationMode == AuthenticationMode.WPA2
|| accessPoint.AuthenticationMode == AuthenticationMode.WPA2PSK;

if (!valid)
{
throw new InvalidOperationException(“The provided AP is not set up for WPA”);
}

return ConnectToNetwork(accessPoint.Name, passphrase, false, accessPoint.AuthenticationMode, accessPoint.Privacy);
}

private bool ConnectToNetwork(string ssid, string passphrase, bool adhoc, AuthenticationMode mode, WEPStatus encryption)
{
EAPParameters eap = null;

switch (mode)
{
case AuthenticationMode.WPA:
case AuthenticationMode.WPAPSK:
case AuthenticationMode.WPA2:
case AuthenticationMode.WPA2PSK:
eap = new EAPParameters()
{
Enable8021x = true,
EapType = EAPType.Default,
EapFlags = EAPFlags.Enabled,
};
break;
}

// stop scanning while connecting
var wasScanning = Scanning;
StopScanning();

try
{
lock (m_syncRoot)
{
return m_wzc.AddPreferredNetwork(ssid,
!adhoc,
passphrase,
1,
mode,
encryption,
eap);
}
}
catch (ArgumentException ae)
{
return false;
}
finally
{
if (wasScanning) StartScanning();
}
}


I apologize that the above code won’t just compile right out of the box – it’s got some dependencies to other bits in the Service class, but my hope is that it at least gives the essence of what needs to be done to get the connectvitiy you need working.

One thought on “SDF and WiFi: Connecting to APs”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s