简体   繁体   English

如何在 WPF 中扫描蓝牙设备?

[英]How can I scan for bluetooth devices in a WPF?

I need to get a list of bluetooth devices in pairing mode.我需要在配对模式下获取蓝牙设备列表。 UWP documentation shows a clear way of doing this; UWP 文档显示了一种清晰的方法; but, not for WPF.但是,不适用于 WPF。 I am using this in a WPF async method-我在 WPF 异步方法中使用它-

var devices = await 
              DeviceInformation.FindAllAsync(                
                                             RfcommDeviceService.GetDeviceSelector(
                                                  RfcommServiceId.SerialPort));

        foreach (var device in devices)
        {
            listbox.Items.Add(device);
        }

I am getting does not contain a definition for 'GetAwaiter' exception.我收到的不包含“GetAwaiter”异常的定义。

How can I get all bluetooth devices in pairing mode, preferably gamepads or controllers?如何让所有蓝牙设备处于配对模式,最好是游戏手柄或控制器?

DeviceInformation.FindAllAsync does not return a task, and can't be awaited. DeviceInformation.FindAllAsync不返回任务,也无法等待。

However you can call .AsTask() and await that.但是,您可以调用.AsTask()await它。

var asyncOp = DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));

var devices = await asyncOp.AsTask();

Note: I haven't tested this.注意:我还没有测试过这个。

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

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