Beispiel #1
0
        private async void OnPairingRequested(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' where there is no Windows Consent UX, you may want to provide your own confirmation.
                args.Accept();
                break;

            case DevicePairingKinds.DisplayPin:
                // We just show the PIN on this side. The ceremony is actually completed when the user enters the PIN
                // on the target device. We automatically except here since we can't really "cancel" the operation
                // from this side.
                args.Accept();

                // No need for a deferral since we don't need any decision from the user
                Debug.WriteLine("Please enter this PIN on the device you are pairing with: {0}", args.Pin);
                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();

                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    // NOT IMPLEMENTED - Make ASYNC
                    // GET PIN FROM USER

                    collectPinDeferral.Complete();
                });

                break;

            case DevicePairingKinds.ConfirmPinMatch:
                // We show the PIN here and the user responds with whether the PIN matches what they see
                // on the target device. Response comes back and we set it on the PinComparePairingRequestedData
                // then complete the deferral.
                var displayMessageDeferral = args.GetDeferral();

                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    // NOT IMPLEMENTED - Make ASYNC
                    // CONFIRM PIN MATCH

                    displayMessageDeferral.Complete();
                });

                break;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Called when custom pairing is initiated so that we can handle the custom ceremony
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private async void PairingRequestedHandler(
            DeviceInformationCustomPairing sender,
            DevicePairingRequestedEventArgs args)
        {
            // Save the args for use in ProvidePin case
            pairingRequestedHandlerArgs = args;

            // Save the deferral away and complete it where necessary.
            if (args.PairingKind != DevicePairingKinds.DisplayPin)
            {
                deferral = args.GetDeferral();
            }

            string confirmationMessage;

            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 Athens where there is no Windows Consent UX, you may want to provide your own confirmation.
            {
                confirmationMessage = string.Format(bluetoothConfirmOnlyFormatString, args.DeviceInformation.Name, args.DeviceInformation.Id);
                DisplayMessagePanelAsync(confirmationMessage, MessageType.InformationalMessage);
                // Accept the pairing which also completes the deferral
                AcceptPairing();
            }
            break;

            case DevicePairingKinds.DisplayPin:
                // We just show the PIN on this side. The ceremony is actually completed when the user enters the PIN
                // on the target device
            {
                confirmationMessage = string.Format(bluetoothDisplayPinFormatString, args.Pin);
                DisplayMessagePanelAsync(confirmationMessage, MessageType.OKMessage);
            }
            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.
                await MainPage.Current.UIThreadDispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                {
                    // PIN Entry
                    inProgressPairButton.Flyout = savedPairButtonFlyout;
                    inProgressPairButton.Flyout.ShowAt(inProgressPairButton);
                });

                break;

            case DevicePairingKinds.ConfirmPinMatch:
                // We show the PIN here and the user responds with whether the PIN matches what they see
                // on the target device. Response comes back and we set it on the PinComparePairingRequestedData
                // then complete the deferral.
            {
                confirmationMessage = string.Format(bluetoothConfirmPinMatchFormatString, args.Pin);
                DisplayMessagePanelAsync(confirmationMessage, MessageType.YesNoMessage);
            }
            break;
            }
        }
Beispiel #3
0
        public static async void HandlePairing(CoreDispatcher dispatcher, DevicePairingRequestedEventArgs args)
        {
            using (Deferral deferral = args.GetDeferral())
            {
                switch (args.PairingKind)
                {
                case DevicePairingKinds.DisplayPin:
                    await ShowPinToUserAsync(dispatcher, args.Pin);

                    args.Accept();
                    break;

                case DevicePairingKinds.ConfirmOnly:
                    args.Accept();
                    break;

                case DevicePairingKinds.ProvidePin:
                {
                    string pin = await GetPinFromUserAsync(dispatcher);

                    if (String.IsNullOrEmpty(pin))
                    {
                        args.Accept(pin);
                    }
                }
                break;
                }
            }
        }
        /// <summary>
        /// Called when a custom pairing is initiated so that we can handle its custom ceremony
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        public async void PairingRequestedHandlerAsync(DeviceInformationCustomPairing sender, DevicePairingRequestedEventArgs args)
        {
            LogService.Write((Enum.GetName(typeof(DevicePairingKinds), args.PairingKind)), LoggingLevel.Information);

            BluetoothDeviceInfo currentDevice = new BluetoothDeviceInfo(args.DeviceInformation);

            // Save the args for use in ProvidePin case
            _pairingRequestedArgs = args;

            // Save the deferral away and complete it where necessary.
            if (args.PairingKind != DevicePairingKinds.DisplayPin)
            {
                _deferral = args.GetDeferral();
            }

            switch (args.PairingKind)
            {
            // Windows itself will pop the confirmation dialog as part of "consent" depending on which operating OS is running
            case DevicePairingKinds.ConfirmOnly:
            {
                var confirmationMessage = string.Format(BluetoothConfirmOnlyText, args.DeviceInformation.Name, args.DeviceInformation.Id);
                if (await DisplayMessagePanel(confirmationMessage, MessageType.InformationalMessage))
                {
                    AcceptPairing();
                }
            }
            break;

            // We only show the PIN on this side. The ceremony is actually completed when the user enters the PIN on the target device.
            case DevicePairingKinds.DisplayPin:
            {
                var confirmationMessage = string.Format(BluetoothDisplayPINText, args.Pin);
                await DisplayMessagePanel(confirmationMessage, MessageType.OKMessage);
            }
            break;

            // A PIN may be shown on the target device and the user needs to enter the matching PIN on the originating device.
            case DevicePairingKinds.ProvidePin:
            {
                _inProgressPairButton.Flyout = _savedPairButtonFlyout;
                _inProgressPairButton.Flyout.ShowAt(_inProgressPairButton);
            }
            break;

            // We show the PIN here and the user responds with whether the PIN matches what is displayed on the target device.
            case DevicePairingKinds.ConfirmPinMatch:
            {
                var confirmationMessage = string.Format(BluetoothConfirmPINMatchText, args.Pin);
                if (await DisplayMessagePanel(confirmationMessage, MessageType.YesNoMessage))
                {
                    AcceptPairing();
                }
            }
            break;
            }
        }
Beispiel #5
0
        private async void CustomPairInfo_PairingRequested(DeviceInformationCustomPairing sender, DevicePairingRequestedEventArgs args)
        {
            Deferral PairDeferral = args.GetDeferral();

            try
            {
                PairConfirmaion = new TaskCompletionSource <bool>();

                switch (args.PairingKind)
                {
                case DevicePairingKinds.ConfirmPinMatch:
                {
                    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                        {
                            Tips.Text             = $"{Globalization.GetString("BluetoothUI_Tips_Text_5")}{Environment.NewLine}{args.Pin}";
                            Tips.Visibility       = Visibility.Visible;
                            PinConfirm.Visibility = Visibility.Visible;
                            PinRefuse.Visibility  = Visibility.Visible;
                        });

                    if (await PairConfirmaion.Task)
                    {
                        args.Accept(args.Pin);
                    }

                    break;
                }

                case DevicePairingKinds.ConfirmOnly:
                {
                    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                        {
                            Tips.Text             = Globalization.GetString("BluetoothUI_Tips_Text_6");
                            Tips.Visibility       = Visibility.Visible;
                            PinConfirm.Visibility = Visibility.Visible;
                            PinRefuse.Visibility  = Visibility.Visible;
                        });

                    if (await PairConfirmaion.Task)
                    {
                        args.Accept();
                    }

                    break;
                }
                }
            }
            catch (Exception ex)
            {
                LogTracer.Log(ex, $"An exception was threw in {nameof(CustomPairInfo_PairingRequested)}, pair with bluetooth failed");
            }
            finally
            {
                PairDeferral.Complete();
            }
        }
        private void OnPairRequestedAsyncCallback(
            DeviceInformationCustomPairing sender,
            DevicePairingRequestedEventArgs args)
        {
            this.log.Info("OnPairRequested", () => string.Format("Paring kind {0}", args.PairingKind.ToString()));

            BT_PairInfoRequest pairInfo = new BT_PairInfoRequest();

            pairInfo.DeviceName = args.DeviceInformation.Name;

            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.
                using (Windows.Foundation.Deferral collectPinDeferral = args.GetDeferral()) {
                    pairInfo.PinRequested = true;
                    if (this.BT_PairInfoRequested != null)
                    {
                        this.BT_PairInfoRequested(this, pairInfo);
                    }
                    else
                    {
                        this.log.Error(9999, "No subscriber to pin request");
                    }

                    this.log.Info("OnPairRequested",
                                  () => string.Format("Pin '{0}' Response:{1}",
                                                      pairInfo.Pin, pairInfo.Response));

                    if (pairInfo.Response)
                    {
                        // TODO Check for the result to see if you go ahead
                    }

                    if (!string.IsNullOrEmpty(pairInfo.Pin))
                    {
                        args.Accept(pairInfo.Pin);
                    }
                    // TODO - needs to be disposed
                    collectPinDeferral.Complete();
                };
                break;
            }
        }
Beispiel #7
0
        private void PairingRequestedHandler(DeviceInformationCustomPairing sender, DevicePairingRequestedEventArgs arguments)
        {
            switch (arguments.PairingKind)
            {
            case DevicePairingKinds.ConfirmOnly:
                arguments.Accept();
                break;

            case DevicePairingKinds.ProvidePin:
                var    collectPinDeferral = arguments.GetDeferral();
                string pin = "000000";
                arguments.Accept(pin);
                collectPinDeferral.Complete();
                break;
            }
        }
        private async void CustomPairInfo_PairingRequested(DeviceInformationCustomPairing sender, DevicePairingRequestedEventArgs args)
        {
            Deferral PairDeferral = args.GetDeferral();

            switch (args.PairingKind)
            {
            case DevicePairingKinds.ConfirmPinMatch:
            {
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                    {
                        Tips.Text             = "请确认PIN码与配对设备一致\r" + args.Pin;
                        Tips.Visibility       = Visibility.Visible;
                        PinConfirm.Visibility = Visibility.Visible;
                        PinRefuse.Visibility  = Visibility.Visible;
                    });

                break;
            }

            case DevicePairingKinds.ConfirmOnly:
            {
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                    {
                        Tips.Text             = "请确认是否与配对设备配对";
                        Tips.Visibility       = Visibility.Visible;
                        PinConfirm.Visibility = Visibility.Visible;
                        PinRefuse.Visibility  = Visibility.Visible;
                    });

                break;
            }
            }
            await Task.Run(() =>
            {
                PinLock.WaitOne();

                if (IsPinConfirm)
                {
                    args.Accept();
                }
            });

            PairDeferral.Complete();
        }
Beispiel #9
0
        private async void CustomPairInfo_PairingRequested(DeviceInformationCustomPairing sender, DevicePairingRequestedEventArgs args)
        {
            Deferral PairDeferral = args.GetDeferral();

            switch (args.PairingKind)
            {
            case DevicePairingKinds.ConfirmPinMatch:
            {
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                    {
                        Tips.Text             = $"{Globalization.GetString("BluetoothUI_Tips_Text_5")}{Environment.NewLine}{args.Pin}";
                        Tips.Visibility       = Visibility.Visible;
                        PinConfirm.Visibility = Visibility.Visible;
                        PinRefuse.Visibility  = Visibility.Visible;
                    });

                break;
            }

            case DevicePairingKinds.ConfirmOnly:
            {
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                    {
                        Tips.Text             = Globalization.GetString("BluetoothUI_Tips_Text_6");
                        Tips.Visibility       = Visibility.Visible;
                        PinConfirm.Visibility = Visibility.Visible;
                        PinRefuse.Visibility  = Visibility.Visible;
                    });

                break;
            }
            }
            await Task.Run(() =>
            {
                PinLock.WaitOne();

                if (IsPinConfirm)
                {
                    args.Accept();
                }
            }).ConfigureAwait(false);

            PairDeferral.Complete();
        }
Beispiel #10
0
        private async 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' where there is no Windows Consent UX, you may want to provide your own confirmation.
                args.Accept();
                break;

            case DevicePairingKinds.DisplayPin:
                // We just show the PIN on this side. The ceremony is actually completed when the user enters the PIN
                // on the target device. We automatically except here since we can't really "cancel" the operation
                // from this side.
                args.Accept();

                // No need for a deferral since we don't need any decision from the user
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    ShowPairingPanel(
                        "Please enter this PIN on the device you are pairing with: " + args.Pin,
                        args.PairingKind);
                });

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

                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
                {
                    string pin = await GetPinFromUserAsync();
                    if (!string.IsNullOrEmpty(pin))
                    {
                        args.Accept(pin);
                    }

                    collectPinDeferral.Complete();
                });

                break;

            case DevicePairingKinds.ConfirmPinMatch:
                // We show the PIN here and the user responds with whether the PIN matches what they see
                // on the target device. Response comes back and we set it on the PinComparePairingRequestedData
                // then complete the deferral.
                var displayMessageDeferral = args.GetDeferral();

                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
                {
                    bool accept = await GetUserConfirmationAsync(args.Pin);
                    if (accept)
                    {
                        args.Accept();
                    }

                    displayMessageDeferral.Complete();
                });

                break;
            }
        }
Beispiel #11
0
        /// <summary>
        /// Called when custom pairing is initiated so that we can handle the custom ceremony
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private async void PairingRequestedHandler(
            DeviceInformationCustomPairing sender,
            DevicePairingRequestedEventArgs args)
        {
            // Save the args for use in ProvidePin case
            pairingRequestedHandlerArgs = args;

            // Save the deferral away and complete it where necessary.
            if (args.PairingKind != DevicePairingKinds.DisplayPin)
            {
                deferral = args.GetDeferral();
            }

            string confirmationMessage;

            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 Athens where there is no Windows Consent UX, you may want to provide your own confirmation.
                    {
                        confirmationMessage = string.Format(bluetoothConfirmOnlyFormatString, args.DeviceInformation.Name, args.DeviceInformation.Id);
                        DisplayMessagePanel(confirmationMessage, MessageType.InformationalMessage);
                        // Accept the pairing which also completes the deferral
                        AcceptPairing();
                    }
                    break;

                case DevicePairingKinds.DisplayPin:
                    // We just show the PIN on this side. The ceremony is actually completed when the user enters the PIN
                    // on the target device
                    {
                        confirmationMessage = string.Format(bluetoothDisplayPinFormatString, args.Pin);
                        DisplayMessagePanel(confirmationMessage, MessageType.OKMessage);
                    }
                    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.
                    await MainPage.Current.UIThreadDispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                    {
                        // PIN Entry
                        inProgressPairButton.Flyout = savedPairButtonFlyout;
                        inProgressPairButton.Flyout.ShowAt(inProgressPairButton);
                    });
                    break;

                case DevicePairingKinds.ConfirmPinMatch:
                    // We show the PIN here and the user responds with whether the PIN matches what they see
                    // on the target device. Response comes back and we set it on the PinComparePairingRequestedData
                    // then complete the deferral.
                    {
                        confirmationMessage = string.Format(bluetoothConfirmPinMatchFormatString, args.Pin);
                        DisplayMessagePanel(confirmationMessage, MessageType.YesNoMessage);
                    }
                    break;
            }
        }
        private async 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' where there is no Windows Consent UX, you may want to provide your own confirmation.
                    args.Accept();
                    break;

                case DevicePairingKinds.DisplayPin:
                    // We just show the PIN on this side. The ceremony is actually completed when the user enters the PIN
                    // on the target device. We automatically except here since we can't really "cancel" the operation
                    // from this side.
                    args.Accept();

                    // No need for a deferral since we don't need any decision from the user
                    await rootPage.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                    {
                        ShowPairingPanel(
                            "Please enter this PIN on the device you are pairing with: " + args.Pin,
                            args.PairingKind);

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

                    await rootPage.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
                    {
                        string pin = await GetPinFromUserAsync();
                        if (!string.IsNullOrEmpty(pin))
                        {
                            args.Accept(pin);
                        }

                        collectPinDeferral.Complete();
                    });
                    break;

                case DevicePairingKinds.ConfirmPinMatch:
                    // We show the PIN here and the user responds with whether the PIN matches what they see
                    // on the target device. Response comes back and we set it on the PinComparePairingRequestedData
                    // then complete the deferral.
                    var displayMessageDeferral = args.GetDeferral();

                    await rootPage.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
                    {
                        bool accept = await GetUserConfirmationAsync(args.Pin);
                        if (accept)
                        {
                            args.Accept();
                        }

                        displayMessageDeferral.Complete();
                    });
                    break;
            }
        }