Парное устройство BLE - PullRequest
0 голосов
/ 21 мая 2018
var watcher = new BluetoothLEAdvertisementWatcher();
      watcher.ScanningMode = BluetoothLEScanningMode.Active;
      watcher.Received += OnAdvertisementReceived;
      watcher.Start();
    }

    #region BLE
    private void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
    {     

      if (items.Contains(eventArgs.Advertisement.LocalName) == false)
      {
        items.Add(eventArgs.Advertisement.LocalName); 
      }
    }

У меня есть эта настройка как способ обнаружить мое устройство BLE (rfduino).Оно работает.Когда я нажимаю кнопку, он показывает мое устройство в списке.Однако мне нужна помощь в процессе сопряжения.

1 Ответ

0 голосов
/ 30 июля 2018

Я привел пример кода, как использовать метод «PairAsync» и обработчик события для события «PairingRequested».

    public async Task Connect(BluetoothLEDevice leDevice, DevicePairingProtectionLevel pairingProtection)
    {
        try
        {
            if (leDevice != null)
            {
                DevicePairingKinds ceremoniesSelected = DevicePairingKinds.ConfirmOnly;

                DeviceInformationCustomPairing customPairing = leDevice.DeviceInformation.Pairing.Custom;
                customPairing.PairingRequested += CustomPairing_PairingRequested;
                DevicePairingResult result = await customPairing.PairAsync(ceremoniesSelected, pairingProtection);
                customPairing.PairingRequested -= CustomPairing_PairingRequested;
            }
        }
        catch
        {
        }
    }




    private void CustomPairing_PairingRequested(DeviceInformationCustomPairing sender, DevicePairingRequestedEventArgs args)
    {
        try
        {
            string deviceID = args.DeviceInformation.Id;

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

                    Task.Factory.StartNew(new Action(async () =>
                    {
                        await Task.Delay(2000);
                        BluetoothLEDevice bleDevice = await BluetoothLEDevice.FromIdAsync(deviceID);
                        // Write your making connection related code here
                    }));

                    break;
            }
        }
        catch
        {
        }
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...