WiFiDirectDevice.FromIdAsync()

时间:2019-03-13 23:50:56

标签: c# uwp wifi-direct

我正在尝试创建WifiDirect广告商,以便将我的PC与Android手机配对和连接。跟着this Wifi Direct tutorial,一起,我已经到了我的PC正在宣告连接的地步,我可以看到可用于在电话上进行连接的选项。但是,当我选择连接计算机时,程序会在System.InvalidCastException: 'Specified Cast is not valid.'行上引发WiFiDirectDevice.FromIdAsnc()错误。

下面是相关的OnConnectionRequested函数,在发布开始之前将其添加为事件侦听器。我从教程(15:30)中复制了逐行的方法行,所以我不确定在哪里出错。任何帮助将不胜感激。

private async void OnConnectionRequested(WiFiDirectConnectionListener
        sender, WiFiDirectConnectionRequestedEventArgs args)
{
    WiFiDirectConnectionRequest connectionRequest  = args.GetConnectionRequest();
    //Problem line is here:
    WifiDirectDevice wfdDevice = await WiFiDirectDevice.FromIdAsync(connectionRequest.DeviceInformation.Id);
    var EndpointPairs=wfdDevice.GetConnectionEndpointPairs();
    //TODO establish socket connection using above IP addresses
}

private void StartAdvert ()
{
    publisher = new WiFiDirectAdvertisementPublisher();

    //Sets the listen state to normal, so it can be discovered when app is in foreground
    publisher.Advertisement.ListenStateDiscoverability = WiFiDirectAdvertisementListenStateDiscoverability.Normal;

    listener = new WiFiDirectConnectionListener();
    listener.ConnectionRequested += OnConnectionRequested;
    publisher.Start();
}

0 个答案:

没有答案