我正在做配对测试与下面的代码在WPF应用程序,但它总是失败的状态。
要使用BluetoothLe库,我刚刚添加了引用(C:\Program (X86)\WindowsKits\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}");
}奇怪的是
有人能帮我吗?
解决了!谢谢。我只是用定制的方法。
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();
}发布于 2017-11-05 16:23:56
在类似的代码中,我遇到了类似的问题--尽管这不是您所要求的,但我认为这对遇到这个问题的其他人可能是有用的:
我的问题是,args.Accept()似乎对配对过程没有任何影响,有时配对会失败,有时会超时。
虽然我不知道为什么,但原因是我从Accept()内部调用了App.Current.Dispatcher.InvokeAsync(),而不是直接调用它。在Task.Run()中调用也会很好。
发布于 2017-07-19 16:22:13
目前“经典”桌面Windows应用程序不支持这种配对功能。您可以尝试将您的应用程序与桌面桥进行转换,也可以尝试通过DeviceInformationCustomPairing进行配对,但它要求您拥有UI。
(资料来源:本MSDN讨论)
发布于 2018-05-12 06:34:11
正如MS所指出的,非UWP程序(如桌面和控制台应用程序)不支持应用内配对。然而,正如彼得·托尔所暗示的那样,您可以“尝试通过DeviceInformationCustomPairing进行配对”。
这段代码适用于我;但是,只适用于DevicePairingKinds.ConfirmOnly和DevicePairingKinds.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);处理程序可以从官方样品中提取,也可以使用这个非常简化的版本:
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只是一个例子。显然,它必须从用户处请求!
https://stackoverflow.com/questions/45191412
复制相似问题