DeviceInformation PairAsync 在 WPF 中不起作用

San*_*hoi 5 c# wpf bluetooth-lowenergy uwp

我正在 WPF 应用程序中与下面的代码进行配对测试,但它总是失败并显示 Failed status 。

要使用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}");
}
Run Code Online (Sandbox Code Playgroud)

奇怪的是

  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();
}
Run Code Online (Sandbox Code Playgroud)

Pet*_*SFT 2

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

(来源:本 MSDN 讨论