"Win10 app development" scans and connects to Wi-Fi networks

Source: Internet
Author: User

The old week takes everybody to "minesweeper" today, do not take it seriously, scan and connect to the designated wireless network, a little more fashionable call Wi-Fi.

Therefore, today's task requires that your device has at least 1 wireless network cards, the current old weeks have not seen a device with n wireless card. Like notebooks, tablets and other devices can, of course, the best notebook, because you have to program ah, or you hold a tablet writing code, although fun, but slow. But if you have a keyboard on your board, it's okay.

Oh, remember, before starting today's task, the old week first to tell you a story.

In the end of June 2015, Microsoft released the SDK for build 10158, you can download the installation, you can not install, you can be installed directly on the existing 10069, so in the VS project can choose a different version; however, the old week to save space, 10069 of the SDK is unloaded, and then loaded 10158 of the SDK, after the installation, found that the new generic platform project is still the default target 10069, of course, you can change the version number after the new project (this is the official website), but if I am careful to use the latest SDK, So after each new project to change the version number, more labor-intensive, and the old week is playing for ten years. NET people, have been accustomed to the high productivity of the development process, less suited to low-performance development.

So, the old week, anyway, just preview version, do not consider too much, first solve the immediate matter, the old week Hope: The new project will automatically default to 10158, in fact, it is not difficult, the point is that you have the guts to modify the VS installation directory files.

1th step, open the directory C:\Program Files (x86) \microsoft Visual Studio 14.0\common7\ide.

2nd step, did you see that there are two directories: Projecttemplates and Projecttemplatescache. VS Project Template It's in there, you know, right, just get rid of the project template, and you'll automatically use the latest version every time you create a new project. We just have to change the contents of the Projecttemplates directory, projecttemplatescache under the need to ignore it. When the Projecttemplates directory is changed, the content in Projecttemplatescache is updated automatically.

3rd step, open the Projecttemplates directory, and then find \csharp\windows root\windows uap\1033, because I use the C # language, so the directory is CSharp, this directory has several directories, is the UAP project several templates.

We only need to change two places: The version configuration of the project file, and if the project has a manifest file, change the manifest file as well.

4th, I take the blankapplication (blank application) project as an example, open the Application.csproj file (project file) with Notepad, then find the first <PropertyGroup> node and see the XML:

 <  targetplatformidentifier  Span style= "color: #0000ff;" >>  uap</  Targetplatformidentifier  >   <  targetplatformversion  >   10.0.10158.0  </ targetplatformversion  >   <  targetplatformminversion  >    10.0.10158.0   </ targetplatformminversion  >  

Change the above two versions to 10.0.10158.0.

Remember to save the file, because only the system has the overwrite permission, this time you are not saved, please open the file's Properties-Security page, and then click "Advanced".

Then, do not modify the owner account, in case you do not return, it will damage the security of the file, keep the default system for the owner. Click "Change Permissions" below.

Then add the account you are currently logged into and tick the "Full Control" permission.

So you have permission to modify the file, after the file is saved, please follow the above method, the current user just added to remove the permissions to ensure security.

4th, open the Package-managed.appxmanifest file, which is the manifest file, and then change the version number in the following XML:

  < Dependencies >    <  Name= "Windows.universal"  minversion= "10.0.10158.0" maxversiontested = "10.0.10158.0" />  </ Dependencies >

With this modification, your new project is oriented toward 10158, and several other modifications such as class libraries, runtime components, and unit tests are also used.

===========================================

OK, the above content is purely interlude, the following starts to sing the theme song.

This task is divided into two parts, the first part is scanning the wireless network, the second part is the connection.

1. Call the Requestaccessasync method of the Wifiadapter class to see if you are qualified for the scan operation. If you don't have permission, it's out of the reach.

2, call Wifiadapter.findalladaptersasync static method, you can put the wireless card on your device to find out, usually only 1, if there is no wireless card, then do not scan.

3, each Wifiadapter object represents a wireless network card. The Scanasync method of calling the Wifiadapter instance is now ready to begin scanning.

4. After scanning, A list of WiFi is available in the. Networkreport.availablenetworks of the Wifiadapter instance, and each network is represented by a Wifiavailablenetwork object, such as the SSID attribute, which is the SSID name of the network.

At this point, the scan phase is complete. The following is a connection to the specified network.

1. Call the ConnectAsync method of the Wifiadapter instance to try to connect to the specified wireless network.

2. The first parameter of the ConnectAsync method is a wifiavailablenetwork instance, which is the network object scanned earlier, and the second parameter is a value of Wifireconnectionkind enumeration type. Automatic indicates that the network is connected automatically; manual indicates that the next connection requires a manual connection.

3, if the Wi Fi network requires a password, when calling the ConnectAsync method, you need to specify a Passwordcredential object that contains the password for the connection.

OK, the theory is finished, let's practice.

First, get the Wifiadapter object that represents the wireless card.

Wifiadapter Wifiadt =NULL; protected Override Async voidonnavigatedto (NavigationEventArgs e) {//first you have to make sure you have permission to scan the wireless network.            varReqres =awaitWifiadapter.requestaccessasync (); if(Reqres = =wifiaccessstatus.allowed) {//find a wireless card on the device first//do not disable the wireless card, or you may not find the adapter, you know                varAdpts =awaitWifiadapter.findalladaptersasync (); //even if your device has 100 cards, it's usually just a single network.//so remove the first adapter                if(adpts. Count >0) {Wifiadt= adpts[0]; }            }        }

After you get to the wireless adapter, you can start the scan.

            // The sweep begins, please note!             await  wifiadt.scanasync ();             // show the list            of scanned wireless networks Wifilist.itemssource = WifiAdt.NetworkReport.AvailableNetworks;

Wifilist is a ListBox control, and the list of networks scanned is displayed on the ListBox control.

Next, implement the wireless network that connects the user to the selected in the ListBox.

            //Gets the selected networkWifiavailablenetwork Networkitem = Wifilist.selecteditem aswifiavailablenetwork;            Wificonnectionresult Connectionresult; if(Networkitem?.securitysettings.networkauthenticationtype! =Windows.Networking.Connectivity.NetworkAuthenticationType.None) {//Password Verification required                if(PwdWifi.Password.Length = =0) {Messagedialog dlg=NewMessagedialog ("Please enter a password. "); awaitdlg.                    Showasync (); return; }                //Set Passwordpasswordcredential pwd =Newpasswordcredential (); Pwd. Password=Pwdwifi.password; //Start ConnectionConnectionresult =awaitWifiadt.connectasync (Networkitem, wifireconnectionkind.automatic, PWD); }            Else            {                //no password required, directly connected to theConnectionresult =awaitWifiadt.connectasync (Networkitem, wifireconnectionkind.automatic); }            stringmsg=""; if(Connectionresult.connectionstatus = =wificonnectionstatus.success) {//If successfulmsg ="The connection was successful. "; }            Else            { //If you are not connected, please review your character .msg = $"connection failed with status: {Connectionresult.connectionstatus}"; }

Finally, this step, it is important, do not forget, if you do not have this step, you are unable to scan and connect operations.

Open the manifest file Package.appxmanifest, locate the Capabilities node, add a devicecapability element, and declare allow the application to do WiFi control.

  < Capabilities >    <  name= "internetclient"/>    <name = "wificontrol" />  </ Capabilities >

After you have entered, save the manifest file.

===========================================

The simple project has been completed, and the following is the time for the witness product.

Run the app and start mopping up your wireless network near you.

Select a verification method of none, that is, no password, or choose a network you know the password, click the "Connect to select Network" button below.

If the character is better, it will be connected.

If you have a password, enter the password.

Then the connection succeeds.

If you have a poor personality, you may not even be able to.

Well, today's mission is done. The old week is going to dinner.

Sample source Download: Http://files.cnblogs.com/files/tcjiaan/wifiSampleApp.zip

"Win10 app development" scans and connects to Wi-Fi networks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.