【发布时间】:2026-01-04 07:30:01
【问题描述】:
我正在开发一个简单的 BLE UWP。我一直指的是“Windows UWP connect to BLE device after discovery”,在 Visual Studio 2017 中工作。
我的核心代码是:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Threading;
using Windows.Devices.Bluetooth.Advertisement;
using Windows.Devices.Bluetooth;
using Windows.Devices;
using Windows.Foundation;
using Windows;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
private BluetoothLEAdvertisementWatcher watcher;
public Form1()
{
InitializeComponent();
watcher = new BluetoothLEAdvertisementWatcher();
watcher.Received += OnAdvertisementReceived;
}
private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
{
var dev = await BluetoothLEDevice.FromBluetoothAddressAsync(eventArgs.BluetoothAddress);
}
}
排队
var dev = await BluetoothLEDevice.FromBluetoothAddressAsync(eventArgs.BluetoothAddress)
它给出了错误:
IAsyncOperation<Bluetooth> does not contain a definition for
'GetAwaiter' and the best extension method overload
'windowsRuntimeSystemExtensions.GetAwaiter(IAsyncAction)' requires a
receiver of type 'IAsyncAction'
我尝试添加对System.Runtime、System.Runtime.WindowsRuntime 和Windows 的引用,但仍然出现此错误。
从我的搜索来看,原因似乎是FromBluetoothAddressAsync方法应该返回一个Task。
从“BluetoothLEDevice Class”,我可以检查FromBluetoothAddressAsync 方法有这个签名:
public static IAsyncOperation<BluetoothLEDevice> FromBluetoothAddressAsync(
UInt64 bluetoothAddress,
BluetoothAddressType bluetoothAddressType
)
这意味着它返回IAsyncOperation<BluetoothLEDevice>。在我看来,这似乎足以被视为Task<>。
问题是由于链接断开导致IAsyncOperation<> 被识别为Task<> 的孩子吗?
【问题讨论】:
标签: c# bluetooth async-await