首页 > 解决方案 > HRESULT 异常:0x80640012

问题描述

我正在尝试收听 connectionRequested 侦听器,

private WiFiDirectConnectionListener _listener;
...
_listener.ConnectionRequested += ConnectionRequestedAsync;

这是侦听器回调,

private async void ConnectionRequestedAsync(WiFiDirectConnectionListener sender, WiFiDirectConnectionRequestedEventArgs args) 
{
        WiFiDirectConnectionRequest request = args.GetConnectionRequest();
        if (request != null)
        {
            try
            {
                WiFiDirectDevice _device = await WiFiDirectDevice.FromIdAsync(request.DeviceInformation.Id);
                ...
            }
            catch (Exception e)
            {
                Debug.WriteLine(string.Format("Exception::\"{0}\"", e.Message));
            }
        }
}

在日志中发现异常(对于 call WiFiDirectDevice.FromIdAsync),

异常::“来自 HRESULT 的异常:0x80640012”

任何想法可能是什么原因?

标签: c#uwpwifiwifi-direct

解决方案


下面的代码对我有用,

try
{
    // IMPORTANT: FromIdAsync needs to be called from the UI thread
    if (Application.Current.Dispatcher.CheckAccess())
    {
        _wfdDevice = await WiFiDirectDevice.FromIdAsync(deviceID);
    }
    else
    {
        await Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(async () =>
        {
            _wfdDevice = await WiFiDirectDevice.FromIdAsync(deviceID);
        }));
    }

}
catch (Exception ex)
{
    Logger.E($"Exception in FromIdAsync: {ex.Message}, StackTrace: {ex.StackTrace}\n\r");
}

推荐阅读