Recently, I purchased myself an AR.Drone from Parrot. And, as I mentioned in this article, the new quadricopter is AWESOME! If you haven’t picked one up yet, you need to! (Amazon link). Anyways, I have begun working on my own C# application that will allow me to control the quadricopter from my computer. While working on that project, I have come across a few side-technologies that I would like to share. For instance, in order to control your drone, you have to first connect to it via WiFi, just like you would any Wireless Access Point. For my project, the first piece that needed to be in place was a way to scan for available WAPs (drones) and that is what I want to share with you today.
Now, I’ve never used C# to scan for wireless networks before. So, as always, I started Googling around to research how to do this or at least where to begin. Luckily for me, there is already something out there that makes this step a whole lot simpler. Instead, of reinventing the wheel, there is a project over at CodePlex called the “Managed Wifi API” (http://managedwifi.codeplex.com/). The API is basically 2 files that give you direct access to your wireless adapter. The API is written entirely in C#. So, integrating this with my app was a breeze.
To get started, you need to head over to the http://managedwifi.codeplex.com/ page and download the API using the big green “Download” button on the right. When you click the button, an End User License Agreement will popup. After reading the EULA, go ahead and click the “Accept” button to begin the download. It’s a tiny file. So, it won’t take more than a second or two to download. After you have the API downloaded, go ahead and unzip it to somewhere on your filesystem.
Once you have the API downloaded and extracted, you have 2 options of using it. The first option is to open the accompanying project and compile the project into a DLL which can be imported into your own project. The second option it to copy the 2 .cs files directly into your project so that they get compiled directly into your application. This is the approach that I went with for my ARDrone controller app.
To use the API, fire up Visual C# and create yourself a new Windows application. (Note: You can also create a console app if you’d rather and simply write the networks to the console). Next, choose how you want to use the API as described in the previous paragraph. For this example, we’ll simply copy the 2 .cs files into our new project and call them locally. Then, from your Solution Explorer, double click Form1.cs to open the form in the editor view. From there, add yourself a new list view box and a button to refresh the list as shown below.
Once you’ve added your controls, double-click on your Refresh button to add a new click event handler. This will take you over to your code editor and will auto-insert the click event handler function. Before adding anything to this function, jump to the top of the code and add a reference to the NativeWifi namespace with your using statement.
using NativeWifi;
Next, go back to your Refresh button click event handler and create a new “WlanClient” object. This object will give you access to your wireless adapter interfaces. Notice I said “interfaces”, as in plural because there’s a chance you might have more than 1. If so, create a foreach loop to iterate thru each of those interfaces. While iterating thru the interfaces, you’ll have the ability to get a list of all the networks that each interface can “see”. Since you already have this list, go ahead and iterate over it as well using another foreach loop. This is what your code should look like so far:
WlanClient client = new WlanClient();
foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
{
Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0);
foreach (Wlan.WlanAvailableNetwork network in networks)
{
}
}
If everything looks good, you are now ready to grab each network and add it to the list view you created earlier. For that, you will begin by grabbing the SSID from each network which you’ll need to encode as ASCII in order to get the network name.
Wlan.Dot11Ssid ssid = network.dot11Ssid;
string networkName = Encoding.ASCII.GetString(ssid.SSID, 0, (int)ssid.SSIDLength);
Once you have the name of your network, you’re ready to add all of the found networks to your list view by creating a new ListViewItem, setting each column, and adding that object to the items array of your list view.
ListViewItem item = new ListViewItem(networkName);
item.SubItems.Add(network.dot11DefaultCipherAlgorithm.ToString());
item.SubItems.Add(network.wlanSignalQuality + “%”);
lstNetworks.Items.Add(item);
That’s it! You’re now ready to test your application. Before you do, make sure that your wireless adapter is enabled. Then, just run the app and click the “Refresh” button. If everything worked accordingly, you should see a list of networks in your area.
Here is the final code that made this tutorial possible. Feel free to leave any questions or suggestions in the comments below.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NativeWifi;
namespace WifiMonitor
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void btnRefresh_Click(object sender, EventArgs e)
{
lstNetworks.Items.Clear();
WlanClient client = new WlanClient();
foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
{
Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0);
foreach (Wlan.WlanAvailableNetwork network in networks)
{
Wlan.Dot11Ssid ssid = network.dot11Ssid;
string networkName = Encoding.ASCII.GetString(ssid.SSID, 0, (int)ssid.SSIDLength);
ListViewItem item = new ListViewItem(networkName);
item.SubItems.Add(network.dot11DefaultCipherAlgorithm.ToString());
item.SubItems.Add(network.wlanSignalQuality + "%");
lstNetworks.Items.Add(item);
}
}
}
}
}


