Question

I've tried to find a way to scan for available WiFi networks in F# and print out their names and signal strength, but i couldn't find anything useful. I found the NativeWifi API, and tried to use it, but I had no luck, so I don't know if it's the right way. I'm new to F#, it's a college assigment, so please help me if you know how to program this.

I have this for now, but I don't think that is any good, I don't know what I'm doing actually.

let mutable dwVersion : uint32 = 0u
let mutable hClient : nativeint =  0n
let handle = NativeWifi.Wlan.WlanOpenHandle(1u, 0n,  &dwVersion, &hClient)

let mutable pInterface : nativeint = 0n
let result = NativeWifi.Wlan.WlanEnumInterfaces(hClient, 0n, &pInterface)
Was it helpful?

Solution

It is not clear from your message if your college assignment prescribes pure F# solution, or you can base it on existing open source .NET libraries. Depending on this factor your mileage may vary...

Nevertheless, if you are allowed to use existing .NET libraries, then as Jack P already noted you may base your approach on Managed Wifi API. It consists of 2 C# classes: Wlan, which is pInvoke interop wrapper over Native WiFi API, and WlanClient, which represents per se managed .Net API for manipulating WiFi. If you would be able building the Managed Wifi API DLL named, say, interop.dll from CodePlex C# sources, then using it from F# to list available WiFi networks and their signal strength is almost trivial:

#if INTERACTIVE
System.Environment.CurrentDirectory <- __SOURCE_DIRECTORY__
#r @"..\Interop\bin\debug\interop.dll"
#endif

open NativeWifi

let getName (network: Wlan.WlanAvailableNetwork) =
    System.Text.Encoding.ASCII.GetString(
        network.dot11Ssid.SSID, 0, (int network.dot11Ssid.SSIDLength))

WlanClient().Interfaces.[0].GetAvailableNetworkList(
    Wlan.WlanGetAvailableNetworkFlags.IncludeAllAdhocProfiles)
|> Array.iter (fun network ->
   printfn "WiFi SSID %s with strength %i" (getName network) network.wlanSignalQuality)

Running this at home in FSI I got the following teaser output as a proof of concept:


--> Referenced 'c:\...\..\Interop\bin\debug\interop.dll'

WiFi SSID ASUS_2G with strength 68
WiFi SSID MrDarkAngel with strength 31
WiFi SSID E0C95 with strength 30
WiFi SSID B4C0 with strength 25

val getName : network:NativeWifi.Wlan.WlanAvailableNetwork -> string
val it : unit = ()

It was very easy to put above together, thanks to wonderful interop abilities of F#. Now the task on your side is somewhat easier, but still you should be prepared to explain to your professor why and how this interop machinery works in order to succeed with your college assignment. Good luck! (c8

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top