c# - 无需用户输入密码的无头 UWP 蓝牙配对
问题描述
我目前正在构建一个在 Windows 10 IoT Core 上运行的无头 UWP 应用程序。我需要能够通过蓝牙 (RFCOMM) 将移动设备连接到信息亭以收集数据。我需要能够从移动设备启动配对。
我已经尝试过所有UWP 蓝牙示例应用程序,但主要是在尝试使用Device Enumeration and Pairing C# 示例- 特别是Scenario 9 - Custom Device Pairing。我可以使用引脚成功配对到有头 UWP 应用程序,但我无法成功配对到无头 UWP 应用程序 - 当我尝试启动配对时,我得到“失败”结果或“身份验证超时”结果。
如何在没有 UI 的情况下与无头 UWP 应用配对来检索 pin?对于每次配对尝试,我可以使用具有硬编码引脚而不是随机引脚的 UWP 应用程序,但我不确定这是否可能?
解决方案
您可以尝试使用以下代码。当有传入的配对请求时,将调用 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;
推荐阅读
- alibaba-cloud - 查询阿里巴巴云计费API BOA
- ruby - ruby:如何避免对类名进行硬编码?
- excel - 每次从具有绿色背景且 value = 0 的 Range("B1 : B5") 单元格计数时,添加 range("B6") 的值
- c - atomic_load 和 __atomic_load 的区别?
- html - 加载后如何缩放/调整图像大小?
- c - C程序接收TCP数据流
- reactjs - 在渲染外部使用阿波罗客户端变异的问题
- linux - 当我尝试传递 2 个变量时,将未知选项设置为 `s
- ios - Flutter 登录苹果失败
- python - 在 holoviews 条形图中突出显示(选择)条形