【问题标题】:DeviceInformation PairAsync not working in WPFDeviceInformation PairAsync 在 WPF 中不起作用
【发布时间】:2017-12-24 18:36:49
【问题描述】:

我正在 WPF 应用程序中使用以下代码进行配对测试,但它总是以 Failed 状态失败。

要使用 BluetoothLe 库,我刚刚添加了参考 (C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd)

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 应用不显示任何对话框。

谁能帮帮我?

解决了!谢谢你。 我刚刚使用了自定义配对。

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();
}

【问题讨论】:

    标签: c# wpf uwp bluetooth-lowenergy


    【解决方案1】:

    我用类似的代码遇到了类似的问题 - 即使它不是你所要求的,我认为它可能对遇到这个问题的其他人有用:

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

    尽管我不知道为什么,原因是我从App.Current.Dispatcher.InvokeAsync() 中调用了Accept(),而不是直接调用它。在Task.Run() 中调用也可以正常工作。

    【讨论】:

      【解决方案2】:

      “经典”桌面 Windows 应用程序目前不支持配对功能。您可以尝试使用 Desktop Bridge 转换您的应用程序,或者您可以尝试通过 DeviceInformationCustomPairing 自己进行配对,但这需要您拥有 UI。

      (来源:this MSDN discussion

      【讨论】:

        【解决方案3】:

        正如一位 MS 专家 here 所说,非 UWP 程序(如桌面和控制台应用程序)不正式支持应用内配对。但是,正如Peter Torr 所暗示的那样,您可以“尝试通过DeviceInformationCustomPairing 自己进行配对”。

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

        DeviceInformationCustomPairing p = DeviceInformation.Pairing.Custom;
        p.PairingRequested += PairingRequestedHandler;
        var pairingResult = await p.PairAsync(DevicePairingKinds.ConfirmOnly);
        //or:
        //var pairingResult = await p.PairAsync(DevicePairingKinds.ProvidePin);
        

        处理程序可以取自official samples,或者你使用这个非常简化的版本:

        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 只是一个例子。显然必须是用户请求的!

        【讨论】:

          猜你喜欢
          • 2017-04-20
          • 2011-02-26
          • 2012-08-06
          • 2011-02-21
          • 1970-01-01
          • 2014-10-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多