首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DeviceInformation PairAsync不在WPF中工作

DeviceInformation PairAsync不在WPF中工作
EN

Stack Overflow用户
提问于 2017-07-19 12:59:54
回答 3查看 2.7K关注 0票数 3

我正在做配对测试与下面的代码在WPF应用程序,但它总是失败的状态。

要使用BluetoothLe库,我刚刚添加了引用(C:\Program (X86)\WindowsKits\10\UnionMetadata\Windows.winmd)

代码语言:javascript
复制
if (!DeviceInformation.Pairing.IsPaired)
{
  Logger.Info($"{DeviceInformation.Name} Try Pairing");
  var result = await DeviceInformation.Pairing.PairAsync(DevicePairingProtectionLevel.None);

  Logger.Info($"{result.Status}");
}

奇怪的是

  1. 使用相同代码的UWP应用程序,配对是可以的。
  2. 取消配对在UWP和WPF应用程序中都是可以的。
  3. 不同的是,UWP应用程序总是弹出系统对话框来确认配对和取消解析,但是WPF应用程序没有显示任何对话框。

有人能帮我吗?

解决了!谢谢。我只是用定制的方法。

代码语言:javascript
复制
public async void Pair()
{
    if (!DeviceInformation.Pairing.IsPaired)
    {
        Logger.Info($"{DeviceInformation.Name} Try Pairing");
        DeviceInformation.Pairing.Custom.PairingRequested += CustomOnPairingRequested;

        var result = await DeviceInformation.Pairing.Custom.PairAsync(
              DevicePairingKinds.ConfirmOnly, DevicePairingProtectionLevel.None);
        DeviceInformation.Pairing.Custom.PairingRequested -= CustomOnPairingRequested;

        Logger.Info($"{result.Status}");
    }

}


private void CustomOnPairingRequested(
      DeviceInformationCustomPairing sender, 
      DevicePairingRequestedEventArgs args)
{
    Logger.Info("Test");
    args.Accept();
}
EN

回答 3

Stack Overflow用户

发布于 2017-11-05 16:23:56

在类似的代码中,我遇到了类似的问题--尽管这不是您所要求的,但我认为这对遇到这个问题的其他人可能是有用的:

我的问题是,args.Accept()似乎对配对过程没有任何影响,有时配对会失败,有时会超时。

虽然我不知道为什么,但原因是我从Accept()内部调用了App.Current.Dispatcher.InvokeAsync(),而不是直接调用它。在Task.Run()中调用也会很好。

票数 3
EN

Stack Overflow用户

发布于 2017-07-19 16:22:13

目前“经典”桌面Windows应用程序不支持这种配对功能。您可以尝试将您的应用程序与桌面桥进行转换,也可以尝试通过DeviceInformationCustomPairing进行配对,但它要求您拥有UI。

(资料来源:本MSDN讨论)

票数 2
EN

Stack Overflow用户

发布于 2018-05-12 06:34:11

正如MS所指出的,非UWP程序(如桌面和控制台应用程序)不支持应用内配对。然而,正如彼得·托尔所暗示的那样,您可以“尝试通过DeviceInformationCustomPairing进行配对”。

这段代码适用于我;但是,只适用于DevicePairingKinds.ConfirmOnlyDevicePairingKinds.ProvidePin (其他选项会导致RequiredHandlerNotRegistered错误,但没有其他处理程序可以注册):

代码语言:javascript
复制
DeviceInformationCustomPairing p = DeviceInformation.Pairing.Custom;
p.PairingRequested += PairingRequestedHandler;
var pairingResult = await p.PairAsync(DevicePairingKinds.ConfirmOnly);
//or:
//var pairingResult = await p.PairAsync(DevicePairingKinds.ProvidePin);

处理程序可以从官方样品中提取,也可以使用这个非常简化的版本:

代码语言:javascript
复制
private static void PairingRequestedHandler(DeviceInformationCustomPairing sender, DevicePairingRequestedEventArgs args)
{
    switch (args.PairingKind)
    {
        case DevicePairingKinds.ConfirmOnly:
            // Windows itself will pop the confirmation dialog as part of "consent" if this is running on Desktop or Mobile
            // If this is an App for 'Windows IoT Core' or a Desktop and Console application
            // where there is no Windows Consent UX, you may want to provide your own confirmation.
            args.Accept();
            break;

        case DevicePairingKinds.ProvidePin:
            // A PIN may be shown on the target device and the user needs to enter the matching PIN on 
            // this Windows device. Get a deferral so we can perform the async request to the user.
            var collectPinDeferral = args.GetDeferral();
            string pinFromUser = "952693";
            if (!string.IsNullOrEmpty(pinFromUser))
            {
                args.Accept(pinFromUser);
            }
            collectPinDeferral.Complete();
            break;
    }
}

常量变量pinFromUser只是一个例子。显然,它必须从用户处请求!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45191412

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档