Disabling Wifi on Windows 10/Surface Pro4 programatically? - c#

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();
}

Related

Background service to check internet connectivity in Xamarin forms C#

I have implemented a function in Xamarin (C# based code) that check for internet connectivity. I have access this method called CheckInternet() through a Dependency Service in the PCL.
What I want to achieve is to implement a sort of Background Service that runs when the app is running to keep checking internet connectivity. My app detects a list of items and send it to a server if there is an internet connection. If no, the item color remains red on my screen.
And once the internet connectivity is back, the items that have not been sent to the server are automatically sent.
Can someone help me to achieve this background service in C# ?
I think you can take a look to this plugin
and this event
/// <summary>
/// Event handler when connection changes
/// </summary>
event ConnectivityChangedEventHandler ConnectivityChanged;
CrossConnectivity.Current.ConnectivityChanged += (sender, args) =>
{
page.DisplayAlert("Connectivity Changed", "IsConnected: " + args.IsConnected.ToString(), "OK");
};
In addition to #alessandro-caliaro answer; I believe what you need to look for is how to implement backgrounding in your apps. Its slightly tricky to get it working in forms projects, as it needs plaform-specific implementation.
These are two articles that talk about this concept in detail (and illustrate how to implement resilient network calls using backgrounding techniques for each platform and also, have shared logic at PCL level).
http://arteksoftware.com/backgrounding-with-xamarin-forms/
https://xamarinhelp.com/xamarin-background-tasks/

Update Wifi networks programmatically

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();
}

Windows 10 Feedback Task for my App

Similar to this question which invokes the Windows 10 store to allow a user to write a review or rate an app, I'd also like to be able to invoke the Windows 10 Feedback app and allow users to provide feedback there.
I cannot seem to find much information on:
How this works in general. Can any old app use this service? (I
notice it just kind of shows whatever apps I have running)
How to invoke the Windows Feedback app with my package id
In short - not that I can see.
Other apps are invoked via protocol activation. I haven't seen this documented for the feedback app though so I have to err on the side of 'we haven't made this available yet' (I'm still checking though)
Here's an overall guide to the process http://blog.jerrynixon.com/2012/10/walkthrough-using-windows-8-custom.html?m=1
When I look in the registry under HKEY_CLASSES_ROOT\Extensions\ContractId\Windows.Protocol I see (shortened a tad)
[HKEY_CLASSES_ROOT\Extensions\ContractId\Windows.Protocol\PackageId\Microsoft.WindowsFeedback...\ActivatableClassId\App.AppX7eaybq6p4x7d4jgd6w6jk7r5dg6yhmbf.mca\CustomProperties]
"Name"="windows-feedback"
So - give that a try via launching windows-feedback
If I do Windows Key-R (run): windows-feedback://
it works fine so this should work:
var uri = new Uri(#"windows-feedback://");
var success = await Windows.System.Launcher.LaunchUriAsync(uri);
if (success)
{
// URI launched
}
else
{
// URI launch failed
}
Update
I've done some searching and it seems the magic parameter there is
windows-feedback:?contextid=522
That launches the NFL feedback for example. This is a predetermined number - I'm not sure how one gets on this list though.

Windows Phone - Audio Endpoint Device

I can't seem to find this anywhere.
I want to build an Audio Endpoint device that plugs into the Windows Phone Headphone Jack.
I know I need to start with what the phone is capable of receiving and detecting.
Ultimately I would like to use already in existence libraries however I have no heartache about writing my own.
My problem is I can't find any examples of how people access the Audio input on the phone outside of the built in microphone.
Is there a library for this?
You can detect when a headset is plugged in using the VOIP capabilities in Windows Phone 8.
First in the WMAppManifest.xml file, you need to enable ID_CAP_VOIP and ID_CAP_AUDIOROUTING
Then in the App, you need to capture the event
AudioRoutingManager.GetDefault().AudioEndpointChanged += AudioEndpointChanged;
public void AudioEndpointChanged(AudioRoutingManager sender, object args)
{
var AudioEndPoint = sender.GetAudioEndpoint();
switch (AudioEndPoint)
{
case AudioRoutingEndpoint.WiredHeadset:
MessageBox.Show("Headset connected");
break;
}
}
This will enumerate from this list (no custom endpoints allowed)
http://msdn.microsoft.com/en-us/library/windowsphone/develop/windows.phone.media.devices.audioroutingendpoint(v=vs.105).aspx
Sorry, but I can only answer the first part of your question about detecting the device, I'm not familiar with how the hardware device interfaces with the headphone jack to answer the rest.

How can I connect to a Server Side USB (HID) Device from within an ASP.NET Application?

I'm trying to write my own controller for a USB device instead of using the SDK that comes with the product (I feel the sdk is sub-par).
The USB Device is plugged into the SAME SERVER that this application is running on.
So I decided to head over to Nuget and grab the HidLibrary
PM> Install-Package hidlibrary
and I proceeded to follow the example found on GitHub.
First I went into my control panel to verify the VendorID and the ProductID
And I dropped it into my code.
Then I set a breakpoint on the line that grabs the device, but unfortunately it always comes back null.
using HidLibrary;
public class MyController : ApiController
{
private const int VendorId = 0x0BC7;
private const int ProductId = 0x0001;
private static HidDevice _device;
// POST api/<controller>
public string Post(CommandModel command)
{
_device = HidDevices.Enumerate(VendorId, ProductId).FirstOrDefault();
if (_device != null)
{
// getting here means the device exists
}
else
{
// ending up here means the device doesn't exist
throw new Exception("device not connected");
}
return null;
}
I'm hoping it's something silly, and not some deal-breaking permissions issue regarding connecting to a USB device directly from an IIS worker.
Despite your hopes to be something silly, it is not. You have some deal-breaking permission issues. If you will browse Mike O'Brien's code from GitHub of the Hid Library you will see that it calls Win32 API functions located in: kernel32.dll, setupapi.dll, user32.dll, hid.dll (Native.cs).
The enumeration itself it's done through setupapi.dll functions. It browse all the installed devices and filters what it's need it.
So... I think it's a security issue to execute kernel32.dll code directly from a web-app in IIS with anonymous authentication, don't you?
If you really need to communicate with that HID (who knows maybe it's a temperature sensor, or something else) I would do a separate Windows service and the IIS hosted web app would communication through WCF with this service. This service would like a proxy.
Put the same code in a console application and run it. That will help you verify if it's your code or environment.
If it's environment, try using Process Monitor to see if there are any hidden access errors. Also try enumerating all devices, not just looking for the one device you're after, just to see if you can do it in ASP.NET.
#Chase, unless this is an experiment - it is best not to attempt connecting to a device from IIS process. [It's a Pandora's box if you start down this path].
Best way to do this is to have another (WCF) service as proxy to the device and expose just what you need out of the service, hook it up with your app. Feel free to ask for an example if you think that would help.
I +1 #garzanti.

Categories

Resources