UWP 未配对的蓝牙设备 [英] UWP Unpaired Paired Bluetooth devices

查看:32
本文介绍了UWP 未配对的蓝牙设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Visual Studio 2015 中使用 C# 进行开发
运行 Windows IoT Core 的 Raspberry PI 2 设备.

对于我的应用程序,我需要配对和取消配对蓝牙设备.
我可以获得已配对/未配对/所有蓝牙设备的列表吗?
就像在
的蓝牙页面上可以看到的一样内置管理网站(

然后你就可以调用UnPairAsync:

await i.Pairing.UnpairAsync();

旧版本不支持 取消配对异步.

I am developing in Visual Studio 2015 in C# for
a Raspberry PI 2 device running Windows IoT Core.

For my application I need to pair and unpair Bluetooth devices.
Can I get a list with paired / unpaired / all Bluetooth devices?
Like what can be seen on the Bluetooth page of the
built-in management website (http://[deviceip]:8080/bluetooth.htm)

I found a example (https://github.com/Microsoft/Windows-universal-samples),
but this is to much for me!

For now I just want to get a list with paired / unpaired Bluetooth devices

解决方案

In order to find devices (Bluetooth or otherwise) you need a selector
which can be used to tell the DeviceWatcher the type of device to search for.

These selectors are basically strings identifying the type of device,
and the UWP framework provides some of them through methods on various classes.

//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);

From the samples on GitHub:

Currently Bluetooth APIs don't provide a selector to get ALL devices that are both paired and non-paired. Typically you wouldn't need this for common scenarios, but it's convenient to demonstrate the various sample scenarios.

Why we wouldn't typically need this is beyond me, but they do provide a selector which can be used to find both paired and unpaired devices:

var selector = 
        "System.Devices.Aep.ProtocolId:="{e0cbf06c-cd8b-4647-bb8a-263b43f0f974}"";

Once you have this selector you need to create an instance
of the DeviceWatcher class using a method on the DeviceInformation class:

var deviceWatcher = DeviceInformation.CreateWatcher(selector, 
                       null, DeviceInformationKind.AssociationEndpoint);

Finally you have to hook up the events so you get notified of changes:

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 };

Notice that in the Completed handler I stopped the DeviceWatcher
so it enters the Stopped state and can be started again.

Once you have the DeviceInformation you can pair as follows:

var pairingResult = 
    await i.Pairing.PairAsync(DevicePairingProtectionLevel.Encryption);

As for unpairing, you need to make sure your project targets Build 10586
or any later version in the project properties windows:

Then you'll be able to call UnPairAsync:

await i.Pairing.UnpairAsync();

Older builds do not support UnpairAsync.

这篇关于UWP 未配对的蓝牙设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆