首页 > 解决方案 > 无需用户输入密码的无头 UWP 蓝牙配对

问题描述

我目前正在构建一个在 Windows 10 IoT Core 上运行的无头 UWP 应用程序。我需要能够通过蓝牙 (RFCOMM) 将移动设备连接到信息亭以收集数据。我需要能够从移动设备启动配对。

我已经尝试过所有UWP 蓝牙示例应用程序,但主要是在尝试使用Device Enumeration and Pairing C# 示例- 特别是Scenario 9 - Custom Device Pairing。我可以使用引脚成功配对到有头 UWP 应用程序,但我无法成功配对到无头 UWP 应用程序 - 当我尝试启动配对时,我得到“失败”结果或“身份验证超时”结果。

如何在没有 UI 的情况下与无头 UWP 应用配对来检索 pin?对于每次配对尝试,我可以使用具有硬编码引脚而不是随机引脚的 UWP 应用程序,但我不确定这是否可能?

标签: c#uwpbluetoothheadlesswindows-10-iot-core

解决方案


您可以尝试使用以下代码。当有传入的配对请求时,将调用 handlerUpdater。

            handlerUpdated = new TypedEventHandler<DeviceWatcher, DeviceInformationUpdate>(async (watcher, deviceInfoUpdate) =>
            {
                // Since we have the collection databound to a UI element, we need to update the collection on the UI thread.
                await MainPage.Current.UIThreadDispatcher.RunAsync(CoreDispatcherPriority.Low, async () =>
                {
                    // Find the corresponding updated DeviceInformation in the collection and pass the update object
                    // to the Update method of the existing DeviceInformation. This automatically updates the object
                    // for us.
                    foreach (BluetoothDeviceInformationDisplay deviceInfoDisp in bluetoothDeviceObservableCollection)
                    {
                        if (deviceInfoDisp.Id == deviceInfoUpdate.Id)
                        {
                            if (deviceInfoDisp.DeviceInformation.Pairing.CanPair)
                            {
                                await deviceInfoDisp.DeviceInformation.Pairing.Custom.PairAsync(DevicePairingKinds.ConfirmOnly);
                            }

                            deviceInfoDisp.Update(deviceInfoUpdate);
                            break;
                        }
                    }
                });
            });
            deviceWatcher.Updated += handlerUpdated;

推荐阅读