UWP Unpaired Paired Bluetooth devices

偶尔善良 提交于 2019-12-08 03:36:47

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/35956624/uwp-unpaired-paired-bluetooth-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!