配对 BLE 设备 [英] Pair BLE device

查看:52
本文介绍了配对 BLE 设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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) 的一种方式.有用.当我单击一个按钮时,它会在列表框中显示我的 ble 设备.但是,我需要配对过程中的帮助.

I have this setup as a way to discover my BLE device (rfduino). It works. It shows my ble device on listbox when I click a button. However, I need help in the pairing process.

推荐答案

我已经给出了如何使用PairAsync"方法和PairingRequested"事件的事件处理程序的示例代码.

I have given the example code that how to use "PairAsync" method and the event handler for "PairingRequested" event.

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

这篇关于配对 BLE 设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆