C# 设备信息配对同步在WPF中不工作

C# 设备信息配对同步在WPF中不工作,c#,wpf,uwp,bluetooth-lowenergy,C#,Wpf,Uwp,Bluetooth Lowenergy,我正在WPF应用程序中使用下面的代码进行配对测试,但它总是以失败状态失败 为了使用蓝牙库,我刚刚添加了参考(C:\ProgramFiles(x86)\Windows Kits\10\UnionMetadata\Windows.winmd) 奇怪的是 使用相同代码的UWP应用程序可以进行配对 在UWP和WPF应用程序中取消配对都可以 区别在于UWP应用程序总是弹出系统对话框来确认配对和取消配对,但WPF应用程序不显示任何对话框 有人能帮我吗 解决了!非常感谢。 我只是用了定制切割 public a

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

为了使用蓝牙库,我刚刚添加了参考(C:\ProgramFiles(x86)\Windows Kits\10\UnionMetadata\Windows.winmd)

奇怪的是

  • 使用相同代码的UWP应用程序可以进行配对

  • 在UWP和WPF应用程序中取消配对都可以

  • 区别在于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();
    }
    

    此时,“经典”桌面Windows应用程序不支持配对功能。您可以尝试使用转换应用程序,也可以尝试通过进行配对,但这需要您拥有UI


    (来源:)

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

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

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

    正如微软的一位同事所说,对于非UWP程序(如桌面和控制台应用程序),官方不支持应用内配对。但是,正如所暗示的,您可以“尝试通过自己进行配对”

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

    处理程序可以取自,也可以使用此非常简化的版本:

    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
    只是一个示例。显然,它必须是从用户处请求的

    DeviceInformationCustomPairing p = DeviceInformation.Pairing.Custom;
    p.PairingRequested += PairingRequestedHandler;
    var pairingResult = await p.PairAsync(DevicePairingKinds.ConfirmOnly);
    //or:
    //var pairingResult = await p.PairAsync(DevicePairingKinds.ProvidePin);
    
    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;
        }
    }