2017-07-19 64 views
2

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

要使用BluetoothLe库我刚添加引用(C:\ Program Files文件(x86)的\的Windows套件\ 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配对使用相同的代码即可。

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

回答

2

我碰到了类似的代码,类似的问题 - 即使它不是你问什么了,我想可能是别人谁遇到这种有用问题:

我的问题是,参数。接受()似乎没有任何影响配对过程中,有时配对会失败,有时会超时。

虽然我不知道为什么,之所以说是我调用接受()App.Current.Dispatcher.InvokeAsync()内,而不是直接调用它。调用Task.Run()也可以正常工作。