无法在UWP应用程序中发现蓝牙设备,但在WPF应用程序中可用 [英] Unable to Discover Bluetooth Devices in UWP application but works in WPF application

查看:86
本文介绍了无法在UWP应用程序中发现蓝牙设备,但在WPF应用程序中可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码使用 32feet.NET 库扫描蓝牙设备.

I am trying scan the Bluetooth devices using 32feet.NET library using the following code.

private void Button_Click(object sender, RoutedEventArgs e)
{
    BluetoothClient client = new BluetoothClient();
    foreach (BluetoothDeviceInfo bdi in client.DiscoverDevices())
    {
        System.Diagnostics.Debug.WriteLine(bdi.DeviceName + " " + bdi.DeviceAddress);
    }
}

上面的代码返回WPF应用程序中可用的蓝牙设备的列表.但是,当我在UWP应用程序中使用相同的代码时,出现以下异常.

The above code returns list of available Bluetooth devices in WPF application. But when I use the same code in UWP application I am getting the following exception.

System.Runtime.InteropServices.COMException:'在意外时间调用了一个方法.

当我尝试根据此处给出的建议使用异步/等待时,我不是能够在集合中使用异步/等待.因此,我使用了Task.FromResult方法,如下所示,然后该异常也没有解决.

When I tried to use the async/await as per the suggestion given here to fix this exception, I am not able to use async/await in collections. So I used Task.FromResult method as below then also the exception is not fixed.

private async void Button_Click(object sender, RoutedEventArgs e)
{
    BluetoothClient client = new BluetoothClient();
    var dev = await Task.FromResult(client.DiscoverDevices());
            
    foreach (BluetoothDeviceInfo bdi in dev)
    {
      System.Diagnostics.Debug.WriteLine(bdi.DeviceName + " " + bdi.DeviceAddress);
    }
 }

有人可以帮我为什么相同的代码在WPF应用程序中起作用而在UWP应用程序中不起作用吗?

Can someone help me why the same code works in WPF application but not in UWP application?

推荐答案

无法在UWP应用程序中发现蓝牙设备,但可以在WPF应用程序中工作

Unable to Discover Bluetooth Devices in UWP application but works in WPF application

恐怕第三方库不支持UWP平台,如果您想在UWP平台中发现蓝牙设备,建议您使用

I'm afraid the third part library does not support UWP platform, if you want to discover Bluetooth device in UWP platform, we suggest you use DeviceWatcher class to enumerate all Bluetooth device.

此处是您的官方代码示例可以参考,此处是正式文档.

Here is official code sample that you could refer and here is official document.

这篇关于无法在UWP应用程序中发现蓝牙设备,但在WPF应用程序中可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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