Bluetooth UWP非配对配对蓝牙设备
我正在Visual Studio 2015中为Bluetooth UWP非配对配对蓝牙设备,bluetooth,raspberry-pi,uwp,windows-10-iot-core,Bluetooth,Raspberry Pi,Uwp,Windows 10 Iot Core,我正在Visual Studio 2015中为 运行Windows IoT Core的Raspberry PI 2设备 对于我的应用程序,我需要配对和取消配对蓝牙设备。 我可以获得配对/未配对/所有蓝牙设备的列表吗? 类似于在的蓝牙页面上看到的内容 内置管理网站() 我找到了一个例子(), 但这对我来说太重要了 目前,我只想获取配对/未配对蓝牙设备的列表为了查找设备(蓝牙或其他),您需要一个选择器 可用于告诉设备观察者要搜索的设备类型 这些选择器基本上是识别设备类型的字符串, UWP框架通过不同
运行Windows IoT Core的Raspberry PI 2设备 对于我的应用程序,我需要配对和取消配对蓝牙设备。
我可以获得配对/未配对/所有蓝牙设备的列表吗?
类似于在的蓝牙页面上看到的内容
内置管理网站() 我找到了一个例子(),
但这对我来说太重要了 目前,我只想获取配对/未配对蓝牙设备的列表为了查找设备(蓝牙或其他),您需要一个
选择器
可用于告诉设备观察者要搜索的设备类型 这些
选择器基本上是识别设备类型的字符串,
UWP框架通过不同类上的方法提供了其中一些
//Gets all paired Bluetooth devices
var selector = BluetoothDevice.GetDeviceSelector();
//Gets all paired Bluetooth devices (same as above as far as I can tell)
var selector = BluetoothDevice.GetDeviceSelectorFromPairingState(true);
//Gets all unpaired Bluetooth devices
var selector = BluetoothDevice.GetDeviceSelectorFromPairingState(false);
从GitHub上的示例中:
目前蓝牙API不提供获取所有设备的选择器
既成对又不成对的。通常你不需要这个
用于常见场景,但演示
各种示例场景
我无法理解为什么我们通常不需要它,但它们确实提供了
可用于查找配对和未配对设备的选择器:
var selector =
"System.Devices.Aep.ProtocolId:=\"{e0cbf06c-cd8b-4647-bb8a-263b43f0f974}\"";
拥有此选择器后,您需要创建一个实例
使用DeviceInformation
类上的方法对DeviceWatcher
类执行以下操作:
var deviceWatcher = DeviceInformation.CreateWatcher(selector,
null, DeviceInformationKind.AssociationEndpoint);
最后,您必须连接事件,以便收到更改通知:
deviceWatcher.Added += (s, i) => { //Handle the new device };
deviceWatcher.Updated += (s, i) => { //Handle the updated device };
deviceWatcher.Removed += (s, i) => { //Handle the removed device };
deviceWatcher.Completed += (s, a) => { s.Stop(); };
deviceWatcher.Stopped += (s, a) => { //Handle here };
请注意,在Completed
处理程序中,我停止了DeviceWatcher
因此,它进入停止
状态,可以再次启动
一旦有了设备信息
,您可以按如下方式配对:
var pairingResult =
await i.Pairing.PairAsync(DevicePairingProtectionLevel.Encryption);
至于取消配对,您需要确保您的项目目标是build10586
或“项目属性”窗口中的任何更高版本:
然后您将能够调用UnPairAsync
:
await i.Pairing.UnpairAsync();
较旧的版本不支持。开始时,这些示例可能会让人不知所措,我从他们那里学到了一些东西,并在下面进行了回答。我也对你的问题做了一些调整,请不要生气。