是否可以通过非通用应用程序使用Wi-Fi Direct? [英] Is it possible to use Wi-Fi Direct from a non Universal application?

查看:294
本文介绍了是否可以通过非通用应用程序使用Wi-Fi Direct?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试编写一个简单的基于基于命令行的应用程序(使用Visual Studio 2015和Windows 10中的C#和.NET),以按照 Microsoft的通用示例,但是手动添加对必要的* .dll和* .winmd程序集的引用,而不是创建它们一个UniversalWindowsPlatform项目。 (来自引用程序集的System.Runtime.WindowsRuntime和来自Windows Kits?10\Union Metadata\Windows.winmd的Windows)

I've been trying to code a simple command-line based application (using C# and .NET from Visual Studio 2015 and Windows 10) to start a Wi-Fi Direct advertiser following Microsoft's Universal Samples, but manually adding references to the necessary *.dll and *.winmd assemblies instead of creating a UniversalWindowsPlatform project. (System.Runtime.WindowsRuntime from Refference Assemblies and Windows from Windows Kits\10\Union Metadata\Windows.winmd)

这是相关代码:

public void StartAdvertisement(WiFiDirectAdvertisementListenStateDiscoverability discoverability,
        bool listenToConnections)
    {
        if (mPublisher == null)
            mPublisher = new WiFiDirectAdvertisementPublisher();

        if (listenToConnections)
        {
            mListener = new WiFiDirectConnectionListener();
            mListener.ConnectionRequested += OnConnectionRequested;
        }

        mPublisher.StatusChanged += OnStatusChanged;
        mPublisher.Advertisement.IsAutonomousGroupOwnerEnabled = true;
        mPublisher.Advertisement.ListenStateDiscoverability = discoverability;
        mPublisher.Start();
    }

async void OnConnectionRequested(WiFiDirectConnectionListener sender,
        WiFiDirectConnectionRequestedEventArgs connectionEventArgs)
    {
    // Connection code
    }

广告客户开始运行OK(可以从其他设备找到它,并创建必要的网络接口),但是使用OnConnectionRequested方法其他设备尝试连接时不会被调用。我已经看到,要使用Wi-Fi Direct,通用Windows应用程序必须在其清单中添加邻近功能,但对于通用应用程序,则没有清单。

The advertiser starts OK (it can be found from other devices, and it creates the necessary network interface), but the OnConnectionRequested method doesn't get called when other devices attempt to connect. I've seen that for using Wi-Fi Direct, an Universal Windows Application must add to its manifest the proximity capability, but for a generic application, there is no manifest.

可以我只能通过引用必要的程序集来使用非通用Windows应用程序中的Windows 10 WiFi Direct API?

Can I use the Windows 10 WiFi Direct API from a non-universal Windows application only by referencing the necessary assemblies?

推荐答案

最终可以从非通用Windows应用程序使用WinRT API(即使没有明显声明使用邻近功能的情况下,也包括Wi-Fi Direct),但是在Windows 10中比在8或8.1中要难一些。

So, I finally could use the WinRT APIs (including the Wi-Fi Direct ones even without a manifest declaring the proximity capability use) from a non-Universal Windows application, but in Windows 10 is a little bit trickier than in 8 or 8.1.

编辑项目的* .csproj,以便在组内添加以下行...

Once you edit the *.csproj of your project to add the following line inside the group...

<TargetPlatformVersion>10.0.10240.0</TargetPlatformVersion>

您会在参考管理器中看到一个名为Windows的新部分,其中包含winmd库。它们都不是有用的,您可能需要的只是在两个必须添加浏览的库中:

you'll see a new section inside the Reference Manager called Windows, with winmd libraries. None of them will be useful, all you probably need is inside two libraries that you'll have to add browsing:

C:\Program Files (x86)\Windows Kits\10\Union Metadata\Windows.winmd  
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll

使用这两个引用,您将避免出现

With those two references you'll avoid problems like


'这样的问题:'XXXX类型在未引用的程序集中定义'

'The type XXXX is defined in an assembly that is not referenced'


命名空间XXXX在两个不同的程序集中定义。

'The namespace XXXX is defined in two different assemblies'.

但是我们还没有完成!特别是在Wi-Fi Direct中,一旦广告客户知道了广告,并且其他计算机尝试连接,如果您有WiFiDirectConnectionListener实例,则应将以下方法称为

But we're not done yet! Particularly in Wi-Fi Direct, once the advertiser is, you know, advertising, and some other computer attempts to connect, if you have an instance of WiFiDirectConnectionListener, the following method should be called

async void OnConnectionRequested(WiFiDirectConnectionListener sender, WiFiDirectConnectionRequestedEventArgs connectionEventArgs)

但是您却得到一个 System.BadImageFormatException 。这是因为System.Runtime.WindowsRuntime.dll的实际版本与清单中声明的​​版本有所不同,因此无法加载。

but instead you get a System.BadImageFormatException. That's because the System.Runtime.WindowsRuntime.dll actual version differs somehow from the one declared in its manifest, so it can't be loaded.

在在Visual Studio中,选择System.Runtime.WindowsRuntime引用并更改以下属性:将Local复制为false,将Specific Version复制为true。

Open the properties tool in Visual Studio, select the System.Runtime.WindowsRuntime reference and change the following properties: Copy Local to false and Specific Version to true.

现在它应该可以工作了!

Now it should work!

这篇关于是否可以通过非通用应用程序使用Wi-Fi Direct?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆