Giter Club home page Giter Club logo

managednativewifi's Introduction

Managed Native Wifi

A managed implementation of Native Wifi API

Requirements

  • Windows 7 or newer
  • .NET Framework 4.5.2

Methods

Available methods including asynchronous ones based on TAP.

Method Description
EnumerateInterfaces Enumerates wireless interface information.
ScanNetworksAsync Asynchronously requests wireless interfaces to scan (rescan) wireless LANs.
EnumerateAvailableNetworkSsids Enumerates SSIDs of available wireless LANs.
EnumerateConnectedNetworkSsids Enumerates SSIDs of connected wireless LANs.
EnumerateAvailableNetworks Enumerates wireless LAN information on available networks.
EnumerateBssNetworks Enumerates wireless LAN information on BSS networks.
EnumerateProfileNames Enumerates wireless profile names in preference order.
EnumerateProfiles Enumerates wireless profile information in preference order.
SetProfile Sets (add or overwrite) the content of a specified wireless profile.
SetProfilePosition Sets the position of a specified wireless profile in preference order.
RenameProfile Renames a specified wireless profile.
DeleteProfile Deletes a specified wireless profile.
ConnectNetwork Attempts to connect to the wireless LAN associated to a specified wireless profile.
ConnectNetworkAsync Asynchronously attempts to connect to the wireless LAN associated to a specified wireless profile.
DisconnectNetwork Disconnects from the wireless LAN associated to a specified wireless interface.
DisconnectNetworkAsync Asynchronously disconnects from the wireless LAN associated to a specified wireless interface.
GetInterfaceRadio Gets wireless interface radio information of a specified wireless interface.
TurnOnInterfaceRadio Turns on the radio of a specified wireless interface (software radio state only).
TurnOffInterfaceRadio Turns off the radio of a specified wireless interface (software radio state only).

Usage

To check SSIDs of currently available wireless LANs, call EnumerateAvailableNetworkSsids method.

public static IEnumerable<string> EnumerateNetworkSsids()
{
    return NativeWifi.EnumerateAvailableNetworkSsids()
        .Select(x => x.ToString()); // UTF-8 string representation
}

In general, a SSID is represented by a UTF-8 string but it is not guaranteed. So if ToString method seems not to produce a valid value, try ToBytes method instead.

To connect to a wireless LAN, call ConnectNetworkAsync asynchronous method.

public static async Task<bool> ConnectAsync()
{
    var availableNetwork = NativeWifi.EnumerateAvailableNetworks()
        .Where(x => !string.IsNullOrWhiteSpace(x.ProfileName))
        .OrderByDescending(x => x.SignalQuality)
        .FirstOrDefault();

    if (availableNetwork == null)
        return false;

    return await NativeWifi.ConnectNetworkAsync(
        interfaceId: availableNetwork.Interface.Id,
        profileName: availableNetwork.ProfileName,
        bssType: availableNetwork.BssType,
        timeout: TimeSpan.FromSeconds(10));
}

This method returns true if successfully connected to the wireless LAN in contrast to its synchronous sibling, ConnectNetwork method, returns true if the request for the connection succeeds and doesn't indicate the result.

To refresh currently available wireless LANs, call ScanNetworksAsync method.

public static Task RefreshAsync()
{
    return NativeWifi.ScanNetworksAsync(timeout: TimeSpan.FromSeconds(10));
}

This method requests wireless interfaces to scan wireless LANs in parallel. It takes no more than 4 seconds.

To delete an existing wireless profile, use DeleteProfile method. Please note that a profile name is case-sensitive.

public static bool DeleteProfile(string profileName)
{
    var targetProfile = NativeWifi.EnumerateProfiles()
        .Where(x => profileName.Equals(x.Name, StringComparison.Ordinal))
        .FirstOrDefault();

    if (targetProfile == null)
        return false;

    return NativeWifi.DeleteProfile(
        interfaceId: targetProfile.Interface.Id,
        profileName: profileName);
}

To check wireless LAN channels that are already used by surrounding access points, call EnumerateBssNetworks method and filter the results by signal strength.

public static IEnumerable<int> EnumerateNetworkChannels(int signalStrengthThreshold)
{
    return NativeWifi.EnumerateBssNetworks()
        .Where(x => x.SignalStrength > signalStrengthThreshold)
        .Select(x => x.Channel);
}

To turn on the radio of a wireless interface, check the current radio state by GetInterfaceRadio method and then call TurnOnInterfaceRadio method.

public static async Task<bool> TurnOnAsync()
{
    var targetInterface = NativeWifi.EnumerateInterfaces()
        .FirstOrDefault(x =>
        {
            var radioSet = NativeWifi.GetInterfaceRadio(x.Id)?.RadioSets.FirstOrDefault();
            if (radioSet == null)
                return false;

            if (!radioSet.HardwareOn.GetValueOrDefault()) // Hardware radio state is off.
                return false;

            return (radioSet.SoftwareOn == false); // Software radio state is off.
        });

    if (targetInterface == null)
        return false;

    try
    {
        return await Task.Run(() => NativeWifi.TurnOnInterfaceRadio(targetInterface.Id));
    }
    catch (UnauthorizedAccessException)
    {
        return false;
    }
}

Please note that this method can only change software radio state and if hardware radio state is off (like hardware Wi-Fi switch is at off position), the radio cannot be turned on programatically.

History

Ver 1.4.0 2018-9-2

  • Add: Methods to provide additional information; EnumerateInterfaceConnections, EnumerateAvailableNetworkGroups, EnumerateProfileRadios.
  • Breaking change: Radio information related to wireless profiles can be obtained by EnumerateProfileRadios instead of EnumerateProfiles.

Ver 1.0.0 2015-1-30

  • Initial commit

License

  • MIT License

managednativewifi's People

Contributors

emoacht avatar zerglrisk avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.