Trying to use the WiFi Adapter on a Raspberry Pi 3, using Windows 10 IoT
Code I'm trying to run:
private async Task<IEnumerable<string>> ScanNetworksAsync()
{
var access = await WiFiAdapter.RequestAccessAsync();
if (access != WiFiAccessStatus.Allowed)
{
throw new Exception("Not Allowed to use WiFi");
}
var wifi = WiFiAdapter.FindAllAdaptersAsync().AsTask().Result[0];
await wifi.ScanAsync();
return wifi.NetworkReport.AvailableNetworks.Select(n => n.Ssid);
}
I do have the capability defined in the Package.appxmanifest:
<DeviceCapability Name="wiFiControl" />
When it tries to execute wifi.ScanAsync()
, it just throws this error:
Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
What am I missing or doing wrong?
Figured it out. It's something unclear or just missing from the documentation.
All the wifi commands such as ScanAsync()
ConnectAsync()
can not be run in the UI thread.
I was running them in a separate thread, but I was still blocking the UI anyway (didn't care cause it's just a test app). Apparently that's not allowed.
I found a comment in the sample app:
// RequestAccessAsync must have been called at least once by the app before using the API
// Calling it multiple times is fine but not necessary
// RequestAccessAsync must be called from the UI thread
Which implies RequestAccessAsync()
only works if run in the UI thread. I tested it both ways, and it seems to work no matter where it's run.