Update Wifi networks programmatically - c#

I'm trying to create program that connects to certain wifi network when it's in range, even if already connected to another wifi.
I'm using SimpleWifi, and basically it works great. Except that it does not see new wifi networks before I clicked wifi icon in Windows 10 taskbar to show list of networks.
How can I force c# program to update wifi network list?
Currently using IEnumerable<AccessPoint> accessPoints = wifi.GetAccessPoints().OrderByDescending(ap => ap.SignalStrength); to update wifi networks, but as I said, it does not see new networks before refreshed manually from Windows.

It's almost 3 years ago but here is my take on this issue anyways.
In that library you can call:
SimpleWifi.Wifi.Disconnect()
Which I do before re-connect and get the list of access points again. This works sort of, new networks introduced after windows discovery does actually show up, but are way slower than if you click "wifi" button in Windows, which will bring up newly discovered networks right away.
If someone knows a solution to "trigger" Windows/Managed wifi connections to update it's list, just like you do in Windows, I would appreciate that

SimpleWifi, like other Wifi libraries have this feature built-in. And its required as Windows does not always show all the wifi networks available correctly, unless queried..
the sample code can be found here: https://pastebin.com/1iCp41SP
, not the most elegant code , but worked in a WPF project.
This part of the code scans/refreshes the Wifi List in SimpleWifi
var testClient = new WlanClient();
foreach (WlanInterface wlanIface in testClient.Interfaces)
{
wlanIface.Scan();
}

Related

Windows UWP Bluetooh multiple devices showing for single device

I am currently working on a C#-UWP app that needs to be able to discovery bluetooth devices (Not BLE) on the network and ones that have been previously connected to/paired.
Im sure anyone who is new to this task will have quickly found the documentation and example are of little help. I have learned more from Stackoverflow questions about peoples experimentations than from the docs and examples, but anyways.
My main question/problem is this: After setting up the device watcher to find bluetooth devices I found that I consistently get multiple additions of the same device but having a different bluetooth address (this is a device that was previously paired but not live on the network). After much investigate and brainstorming, we discovered that each device id is actually a pairing of the devices MAC address and the BT receivers MAC address.
The reason I was getting 3 device additions per 1 physical device is because I have connected to that same device with 3 different BT receiver dongles in the past. So my question is, is there anyway to make the device watcher return the device that corresponds to the currently active BT receiver dongle?
Otherwise I will need to find the currently active BT receivers MAC address and filter out the devices that do not have this, because otherwise the user will see 3 identical devices to select and only 1 of them will pass while the other 2 will fail.
While on this subject I would also like to mention that the device properties dont seem to be working. Before creating the watcher, I have a list of properties like this for example:
requestedProperties.Add("System.Devices.Aep.DeviceAddress");
requestedProperties.Add("System.Devices.Aep.IsConnected");
requestedProperties.Add("System.Devices.Aep.Bluetooth.Le.IsConnectable");
requestedProperties.Add("System.Devices.Aep.IsPresent");
requestedProperties.Add("System.Devices.Aep.ContainerId");
requestedProperties.Add("System.Devices.Aep.ModelId");
requestedProperties.Add("System.Devices.Aep.Manufacturer");
requestedProperties.Add("System.Devices.Aep.ProtocolId");
requestedProperties.Add("System.Devices.Aep.SignalStrength");
But when I debug the device that is added, it doesnt have any properties:
debug info showing no properties for added device
I would be useful to have this information.
Thank you for any input or suggestions.
Update
I found a quick solution that overcomes this problem (although i did find a lot more problems with the device watcher but that is probably a topic for another question).
For now I simply get the current BT adaptors MAC address and then check each incoming device if it has this MAC address in its pair. If it does not that means the device is paired with an old/unused BT adaptor.
/* NOTE:
Windows allows only 1 BT adapter to be active on 1 machine at a time
Therefore this function will either return the active dongle or a null if
there is no BT adapter on the machine or its disabled.
*/
BluetoothAdapter BTAdaptor = await BluetoothAdapter.GetDefaultAsync();
if (BTAdaptor == null)
{
// Log error
// return
}
//
// Code block to check if the BT adaptor can support the BT tech stack you are interested in.
//
// Format into hex with 12 characters (12 being the number of characters for MAC address)
string tempMac = BTAdaptor.BluetoothAddress.ToString("X12").ToLower();
// Pattern for MAC address.
string pattern = "(.{2})(.{2})(.{2})(.{2})(.{2})(.{2})";
string replace = "$1:$2:$3:$4:$5:$6";
m_strBTAdapterMAC = Regex.Replace(tempMac, pattern, replace);
Then when the device is added/updated/removed by the watcher event, check it:
// If device is not paired with the currently used BT adapter.
if (deviceInfo.Id.Contains(m_strBTAdapterMAC) == false)
{
// Device paired with old dongle, dont want to show the user.
return;
}
If anyone ever figures out how to make the device watcher just not give old devices, please let me know, its probably a better solution.

Is there a way to programatically force Windows to search for new WiFi nextworks?

I have a (C#) process which looks for the presence of an SSID, and connects if it appears.
As a process it works, but it does take a long time for the SSID to appear in the list. IF I click on the WiFi icon in Windows to show available networks, it appears to then search and the SSID appears. If I leave it for say 30-60 seconds, then it'll eventually appear.
So, is there a way to programatically do whatever it is that happens when you manually click on the WiFi icon in the taskbar?
Windows must be interrogated about possible connections to refresh the list. You can do it this way (taken from Update Wifi networks programmatically):
var client = new WlanClient();
foreach (WlanInterface wlan in client.Interfaces)
wlan.Scan();

Performance considerations when getting license information for WP app

Introduction
I have a Windows Phone 8.1 Silverlight (WP8.1 SL) based app in the store. Some users complain about performance issues when they have a bad network connection. I searched a bit and came up with the idea that it might be related to new LicenseInformation() that gives me the information of whether the app is running in Trial mode or not. The question is, whether this requires network information or not, and whether CurrentApp.LicenseInformation is a suitable replacement for a WP8.1 SL app.
Background and What I did so far
In general, the app does not need a network connection (no data to load, no advertisements, ...). To confirm that I used Fiddler to watch over the network sent by my phone. The result was that no network traffic is generated. However, the problem still persists.
After a lot of research and playing around I got the feeling that this issue might be related to the code part that checks on whether the app is in trial mode or not. I use the following code to check that.
var li = new LicenseInformation();
if (li.IsTrial()) {
...
}
I do this a couple of times during startup. So in case IsTrial() requires a network connection this could be the actual issue when there is only a bad connection available. But again, I couldn't find anything using Fiddler. The documentation (see here) for LicenseInformation does not mention whether a network connection is required or not.
Searching around I found that there is an updated interface available for both WP 8.1 SL and also W10M UWP.
var li = CurrentApp.LicenseInformation;
if (li.IsTrial) {
...
}
Its documentation clearly states that there is no network connection required for that (see here).
Even though the docs say that CurrentApp.LicenseInformation is also available on WP8 I also found some references that say that you only get a reliable answer for the IsTrial-question when using new LicenseInformation() (e.g. here).
Actual Questions
Is new LicenseInformation() required on WP8.1 SL, or can I use CurrentApp.LicenseInformation as well?
Does new LicenseInformation() require a network connection compared to CurrentApp.LicenseInformation?

Disabling Wifi on Windows 10/Surface Pro4 programatically?

I have a Surface Pro 4 that is being used in a medical environment and we would like to be able to disable to WiFi on the device when certain applications are running. Is this possible?
Disable is a relative term and as long as the device can't communicate over the wifi, even if technically active, that would meet the requirements. I wasn't able to find any way to do this though.
We could disable Wifi completely but would prefer to have it as an option for the users of the device when the specific applications aren't running.
The application in question is written in C#
You can do this with the Windows.Devices.Radios.Radio class by calling SetStateAsync(...).
There's a full example available at Microsoft's GitHub page, here's a snippet:
private async void SetRadioState(bool isRadioOn)
{
var radioState = isRadioOn ? RadioState.On : RadioState.Off;
Disable();
await this.radio.SetStateAsync(radioState);
NotifyPropertyChanged("IsRadioOn");
Enable();
}

Programmatically enabling Windows 8 Pro mobile broadband device

Background
The company I work on is developing a kiosk-like application for tablets running Windows 8 Pro (on desktop mode). The user shouldn't be able to access anything that isn't the application itself: charms will be disabled, the taskbar will be hidden behind the application, etc.
This also means the user shouldn't be able to change network settings, leaving the responsability to keep the device always connected to us. Up to now, I had success using the Mobile Broadband API to assure the device is connected whenever there's a mobile network available. It'll detect disconnect events and try to connect again.
The Problem
Although the user shouldn't be able to do it, I'm considering the case where the user follows this steps:
User opens right-side charm,
clicks on Settings,
clicks on Network,
clicks on More PC Settings,
clicks on Wireless, and
disables the mobile broadband device.
I would like to be able to revert this programmatically and enable it again.
The Attempts
I have tried some different ways to force 3G being reenabled. Most of them give me the same result: they supposedly enable the device without errors, but I still cannot use it. Enable-NetAdapter in Powershell doesn't throw errors, and the Enable method of Win32_NetworkAdapter appears to work, but no dice.
I thought maybe the method IMbnRadio::SetSoftwareRadioState could be what I'm after, but I can't get to it when the device is disabled. The method IMbnInterfaceManager::GetInterfaces throws a COMException claiming the element could not be found (HRESULT = 0x80070490).
MbnInterfaceManager mbnInterfaceManager = new MbnInterfaceManager();
IMbnInterfaceManager interfaceManager = (IMbnInterfaceManager)mbnInterfaceManager;
// The following line throws a COMException:
IMbnInterface[] interfaces = (IMbnInterface[])interfaceManager.GetInterfaces();
mobileInterface = interfaces[0];
mobileRadio = (IMbnRadio)mobileInterface;
uint requestId;
mobileRadio.SetSoftwareRadioState(MBN_RADIO.MBN_RADIO_ON, out requestId);
Is there a way to override user preferences set on "More PC Settings?"
I found a sketchy way to solve this. Keep in mind this is undocumented, wrong, shameless and immoral, and will probably break eventually. The client is aware of this, but prefers to keep the access to the OS limited.
The setting in case is saved in the Registry. At least in the computers I've checked, it's stored in HKLM\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\0022 in a key named RadioOff.
The Airplane Mode setting is also stored in the Registry, but in a different place. It's at HKLM\SYSTEM\CurrentControlSet\Control\RadioManagement\SystemRadioState in a key named (Default).
After changing these keys and rebooting, everything seems to work fine. I'll repeat though: you really shouldn't be doing this, especially the Airplane Mode thing.

Categories

Resources