I mentioned yesterday that I never got round to writing the bit of code for my location service client which would look for known wi-fi access points.
To recap, the idea is that my phone should be able to look for the access points within range, comparing the SSIDs against a list of known locations. If a match is found, it looks up the name and GPS coordinates of the location from the known store, and uses an HTTP request to send the update to the server.
It was really easy – the interesting bit of the code looks like this…
// get wifi adapters
AdapterCollection adapters = Networking.GetAdapters();
// look through each adapter in turn for the Wifi one
// (e.g. don't use ActiveSync, bluetooth etc.)
foreach (Adapter adapter in adapters)
{
if (adapter.IsWireless)
{
// look for access points known to the adapter
AccessPointCollection accessPoints = adapter.NearbyAccessPoints;
// refresh the list of known SSIDs?
if (blnRefreshList)
{
accessPoints.Refresh();
}
// look through each known access point in turn
foreach (AccessPoint ap in accessPoints)
{
// look for the access point name in our
// list of known locations
//
// access point name - the SSID - is
// ap.Name
if (blnMatchFound)
{
// found a SSID which matches a known location
// send the update to the server
// using the details from our list of known
// locations
// don't bother looking for any other matches
return;
}
}
}
}
Okay, so I admit… I cheated. Slightly.
I’m using the OpenNETCF shared source library because there isn’t really much support to do this in .NET Compact Framework, and I didn’t fancy having to write it myself 🙂
A good introduction to the capabilities of the library for this sort of thing can be found on the MSDN article: Building a Wi-Fi Discovery Application, which was a big help in putting the above code together quickly.
After a quick test, it seems to work fine. As a first stab, it’s fine. I’m not a big fan of polling for updates like this though, and would rather try and go for a more event-driven approach where the client is notified of updates when a new access point is seen. That’s something for another time, though – first I wanna finish off the GPS-checking code…
I’m glad you find the Smart Device Framework (or the OpenNETCF Shared Source library as you called it) useful 🙂