DeviceInformation PairAsync在WPF中不起作用 [英] DeviceInformation PairAsync not working in WPF

查看:76
本文介绍了DeviceInformation PairAsync在WPF中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WPF应用中的以下代码进行配对测试,但始终会以失败"状态失败.

I'm doing pairing test with code below in WPF app, but it always failed with Failed status .

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

To use BluetoothLe library I just added reference (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 App配对是可以的.

  1. pairing is okay with UWP App with same code.

取消配对都是可以的.

区别在于,UWP应用程序始终会弹出系统对话框以确认配对和取消配对,但WPF应用程序不会显示任何对话框.

The difference is that UWP app always pops up system dialog to confirm pairing and unparing, but WPF app doesn't show any dialog.

有人可以帮助我吗?

解决了!谢谢你.我只是使用自定义配对.

Solved! Thank you. I Just used custom paring.

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

推荐答案

我遇到了一个使用类似代码的类似问题-尽管这并非您所要求的,但我认为它可能对您有所帮助其他遇到此问题的人:

I've ran into a similar problem with a similar code - and even though it's not exactly what you've asked for, I think it might be useful to others who encounter this question:

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

My problem was that args.Accept() didn't seem to have any affect on the pairing process, sometimes the pairing would fail and sometimes it got timed-out.

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

Even though I'm not sure why, the reason for that was that I invoked Accept() from within App.Current.Dispatcher.InvokeAsync(), instead of calling it directly. Invoking within Task.Run() would work fine as well.

这篇关于DeviceInformation PairAsync在WPF中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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