Reputation: 238
I'm doing pairing test with code below in WPF app, but it always failed with Failed status .
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}");
}
strange thing is
pairing is okay with UWP App with same code.
unpairing is ok in both UWP and WPF app.
The difference is that UWP app always pops up system dialog to confirm pairing and unparing, but WPF app doesn't show any dialog.
Can anybody help me?
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();
}
Upvotes: 6
Views: 3619
Reputation: 1975
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:
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.
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.
Upvotes: 3
Reputation: 7667
As stated here by a MS guy, in-app pairing is not officially supported for non-UWP programs (like Desktop and Console applications). However, as also hinted by Peter Torr, you can "can try doing the pairing yourself via DeviceInformationCustomPairing".
This code worked for me; however, only for DevicePairingKinds.ConfirmOnly
and DevicePairingKinds.ProvidePin
(The other options cause a RequiredHandlerNotRegistered
error, but there is no other handler which can be registered.):
DeviceInformationCustomPairing p = DeviceInformation.Pairing.Custom;
p.PairingRequested += PairingRequestedHandler;
var pairingResult = await p.PairAsync(DevicePairingKinds.ConfirmOnly);
//or:
//var pairingResult = await p.PairAsync(DevicePairingKinds.ProvidePin);
The handler can be taken from the official samples, or you use this very simplified version:
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;
}
}
The constant variable pinFromUser
is only an example. It must obviously be requested from the user!
Upvotes: 4
Reputation: 12019
The pairing functionality is not supported for "classic" Desktop Windows apps at this point in time. You could try converting your application with the Desktop Bridge, or you could try doing the pairing yourself via DeviceInformationCustomPairing
but it requires you to own the UI.
(Source: this MSDN discussion)
Upvotes: 3