简体   繁体   English

如何在Windows 10 UWP中丢失连接后以编程方式连接到配对的蓝牙设备

[英]How to programmatically connect to paired Bluetooth device once connection is lost in Windows 10 UWP

I have a windows 10 UWP app that is able to pair with a bluetooth LE device programmatically. 我有一个Windows 10 UWP应用程序,能够以编程方式与蓝牙LE设备配对。 Once the pairing is successful, a connection to the device is also established. 配对成功后,还会建立与设备的连接。

If at some point, the device gets disconnected, I am not able to read any of the GattCharacteristics from the LE device. 如果在某些时候,设备断开连接,我无法从LE设备读取任何GattCharacteristics。 I'm able to check if the connection is present or not but I'm unable to re-establish the connection. 我能够检查连接是否存在,但我无法重新建立连接。

DeviceInformation deviceInfo = await DeviceInformation.CreateFromIdAsync("deviceId", "additionalProperties", "DeviceInformationKind");

if(deviceInfo.ConnectionStatus != BluetoothConnectionStatus.Connected) { // re-establish the connection }

Thanks. 谢谢。

The Problem 问题

The Bluetooth LE device is not storing the bonding information created during the pairing process. 蓝牙LE设备不存储在配对过程中创建的绑定信息。 Bonding information allows two previously paired devices to initiate new connections if they have become disconnected. 绑定信息允许两个先前配对的设备在断开连接时启动新连接。

The Windows 10 Solution Windows 10解决方案

Using the in-app pairing APIs , you can programmatically tell the system to pair with the Bluetooth LE device (it sounds like you are already doing this). 使用应用内配对API ,您可以通过编程方式告知系统与蓝牙LE设备配对(听起来您已经在这样做了)。 To work around the bonding problem described above, the DevicePairingProtectionLevel must be set to None . 要解决上述绑定问题,必须将DevicePairingProtectionLevel设置为None So your in-app pairing code could look like: 因此,您的应用内配对代码可能如下所示:

var result = await someDevice.Pairing.PairAsync(DevicePairingProtectionLevel.None);

Setting the DevicePairingProtectionLevel to None tells the system to ignore bonding information and just look for a matching device. DevicePairingProtectionLevel设置为None告诉系统忽略绑定信息并只查找匹配的设备。

The Peripheral Solution 外围解决方案

Alternatively, if you have access to the peripheral's firmware, you can set it to remember the bonding information. 或者,如果您可以访问外围设备的固件,则可以将其设置为记住绑定信息。 Then your current pairing calls on Windows 10 should work. 然后你在Windows 10上的当前配对调用应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM