none
UWP的后台任务中是否不支持扫描可用WiFi操作 RRS feed

  • 问题

  • var access = await WiFiAdapter.RequestAccessAsync();
                if (access != WiFiAccessStatus.Allowed)
                {
                    throw new Exception("WiFiAccessStatus not allowed");
                }
                else
                {
                    WiFiAdapter _adapter;
                    var wifiAdapterResults = await DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector());
                    if (wifiAdapterResults.Count > 0)
                    {
                        Debug.WriteLine(wifiAdapterResults[0].Id);
                        _adapter = await WiFiAdapter.FromIdAsync(wifiAdapterResults[0].Id);
                        
                    }
                    else
                    {
                        throw new Exception("WiFi Adapter not found.");
                    }
                    await _adapter?.ScanAsync();
                    Debug.WriteLine("BackgroundTask "+_adapter.NetworkReport.AvailableNetworks.Count);
                    foreach (var item in _adapter.NetworkReport.AvailableNetworks)
                    {
                        Debug.WriteLine(item.Ssid);
                    }
              }

    调试的时候没有得到输出

    wifi的权限也获取了

     <Capabilities>
        <Capability Name="internetClient" />
        <uap:Capability Name="userAccountInformation" />
        <DeviceCapability Name="location" />
        <DeviceCapability Name="wifiControl" />
      </Capabilities>


    2017年3月7日 4:39

答案

  • 贴上我从英文论坛上获得的答案

    Hello,

    We can use  DeviceUseTrigger to test whether the device support WiFI scan feature in background.

    https://docs.microsoft.com/en-us/windows/uwp/launch-resume/access-sensors-and-devices-from-a-background-task

    I did a test with the following code.  We can put them in “MainPage.xaml.cs” and the method  “OnNavigatedTo”

    var access = await WiFiAdapter.RequestAccessAsync(); if (access != WiFiAccessStatus.Allowed) { throw new Exception("WiFiAccessStatus not" + " allowed"); } var result = await DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector()); if (result.Count >= 1) { foreach (KeyValuePair<Guid, IBackgroundTaskRegistration> tasks in BackgroundTaskRegistration.AllTasks) { if (tasks.Value.Name == "ScanBackground") { tasks.Value.Unregister(true); break; } } var builder = new BackgroundTaskBuilder(); builder.Name = "ScanBackground"; builder.TaskEntryPoint = (typeof(ScanBackgroundTask)).ToString(); DeviceUseTrigger use = new DeviceUseTrigger(); builder.SetTrigger(use); var access2 = await BackgroundExecutionManager.RequestAccessAsync(); Debug.WriteLine(access2.ToString()); var task = builder.Register();

    //https://docs.microsoft.com/en-us/uwp/api/windows.applicationmodel.background.devicetriggerresult //Indicates whether the device background task operation was allowed or denied, //and if it was denied specifies the reason.

    DeviceTriggerResult result2 = await use.RequestAsync(result[0].Id); Debug.WriteLine(result2.ToString());

    I found the result of the "result2" is DeniedBySystem. So it is not allowed for us to scan Wifi in background task.

    Best regards

    Barry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • 已标记为答案 x wang 2017年8月16日 5:13
    • 已编辑 x wang 2017年8月16日 5:14
    2017年8月16日 5:13

全部回复

  • 你好,

    这个看着应该是对的。

    建议你可以运行以下官方的示例:https://developer.microsoft.com/en-us/windows/iot/samples/wificonnector

    写的比较详细。我刚刚在14393的系统上调试过,可以运行。


    Keep Fighting

    2017年3月9日 1:39
  • 感谢您的回复。

    链接里是IOT设备,IOT设备与普通的设备有不同的地方,请求用户权限的代码可以写在任何线程上。下面这段话是那个官方示例上的。

    Note, in a headed app, the method must be called from the UI thread. However, in an IOT headless app (Background Application), which doesn’t have a UI thread, the method can be called from any thread.

    我写了一个关于这个问题的Demo,在UI线程上获得了用户的权限后,在后台任务中扫描WiFi,获取adapter正常运行,到ScanAsync方法抛出了 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) 而不是UnAuthorized的异常

    是不是说明后台任务中不允许扫描WiFi?我看了MSDN上的文档中都没有提到这个,向您确认一下,再次感谢

    2017年3月9日 14:33
  • 抱歉,

    回复迟了。

    我看了MSDN,上面也没有明确说ScanAsync不能在后台中使用。但是AvailableNetworksChanged这个事件是无法在后台任务中使用的。

    我觉得你可以去英文论坛上问问:https://social.msdn.microsoft.com/Forums/en-US


    Keep Fighting

    2017年3月12日 1:27
  • 贴上我从英文论坛上获得的答案

    Hello,

    We can use  DeviceUseTrigger to test whether the device support WiFI scan feature in background.

    https://docs.microsoft.com/en-us/windows/uwp/launch-resume/access-sensors-and-devices-from-a-background-task

    I did a test with the following code.  We can put them in “MainPage.xaml.cs” and the method  “OnNavigatedTo”

    var access = await WiFiAdapter.RequestAccessAsync(); if (access != WiFiAccessStatus.Allowed) { throw new Exception("WiFiAccessStatus not" + " allowed"); } var result = await DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector()); if (result.Count >= 1) { foreach (KeyValuePair<Guid, IBackgroundTaskRegistration> tasks in BackgroundTaskRegistration.AllTasks) { if (tasks.Value.Name == "ScanBackground") { tasks.Value.Unregister(true); break; } } var builder = new BackgroundTaskBuilder(); builder.Name = "ScanBackground"; builder.TaskEntryPoint = (typeof(ScanBackgroundTask)).ToString(); DeviceUseTrigger use = new DeviceUseTrigger(); builder.SetTrigger(use); var access2 = await BackgroundExecutionManager.RequestAccessAsync(); Debug.WriteLine(access2.ToString()); var task = builder.Register();

    //https://docs.microsoft.com/en-us/uwp/api/windows.applicationmodel.background.devicetriggerresult //Indicates whether the device background task operation was allowed or denied, //and if it was denied specifies the reason.

    DeviceTriggerResult result2 = await use.RequestAsync(result[0].Id); Debug.WriteLine(result2.ToString());

    I found the result of the "result2" is DeniedBySystem. So it is not allowed for us to scan Wifi in background task.

    Best regards

    Barry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • 已标记为答案 x wang 2017年8月16日 5:13
    • 已编辑 x wang 2017年8月16日 5:14
    2017年8月16日 5:13