首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >配对BLE设备

配对BLE设备
EN

Stack Overflow用户
提问于 2018-05-21 22:48:31
回答 1查看 232关注 0票数 0
代码语言:javascript
复制
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设备。但是,我在配对过程中需要帮助。

EN

回答 1

Stack Overflow用户

发布于 2018-07-30 11:59:41

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

代码语言:javascript
复制
    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
        {
        }
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50451355

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档