DeviceInformation PairAsync not working in WPF

I am doing pairing with below code in WPF application but it always failed with Failed status.

To use the BluetoothLe library I just added a link (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

  • pairing is fine with a UWP app with the same code.

  • unairing is fine in both UWP and WPF app.

  • The difference is that the UWP app always pops up a system dialog to confirm pairing and jaggedness, but the WPF app does not display the dialog.

Can anyone help me?

Resolved! Thank. I just used a custom parry.

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

      

+3


source to share


3 answers


The pairing functionality is not supported for "classic" Windows desktop applications at this time. You can try converting your app with Desktop Bridge , or you can try pairing yourself through DeviceInformationCustomPairing

, but this requires a mastery of the user interface.



(Source: this MSDN discussion )

+3


source


I ran into a similar issue with similar code - and while not exactly what you asked for, I think it might be helpful for others who come across this question:

My problem was that it args.Accept()

didn't seem to affect the pairing process in any way, sometimes pairing failed and sometimes it was timed out.



Even though I'm not sure why, the reason is that I called Accept()

from App.Current.Dispatcher.InvokeAsync()

, instead of calling it directly. Calling inside Task.Run()

will work fine.

+3


source


As pointed out here by companion husband, in-app pairing is not officially supported for non-UWP programs (like Desktop and Console apps). However, as Peter Torr also hints at , you can "try to pair yourself via DeviceInformationCustomPairing ".

This code worked for me; however only for DevicePairingKinds.ConfirmOnly

and DevicePairingKinds.ProvidePin

(other parameters throw an error RequiredHandlerNotRegistered

, but there is no other handler to register.):

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

      

Constant variable pinFromUser

is just an example. Obviously it must be requested from the user!

+2


source







All Articles