I am working on a simple BLE UWP. I've been referring to "Windows UWP connect to BLE device after discovery", working in Visual Studio 2017.
The core code I have is:
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);
}
}
In the line
var dev = await BluetoothLEDevice.FromBluetoothAddressAsync(eventArgs.BluetoothAddress)
it gives the error:
IAsyncOperation<Bluetooth> does not contain a definition for
'GetAwaiter' and the best extension method overload
'windowsRuntimeSystemExtensions.GetAwaiter(IAsyncAction)' requires a
receiver of type 'IAsyncAction'
I tried adding references to System.Runtime
, System.Runtime.WindowsRuntime
, and Windows
but this error still appears.
From my searching, the reason seems to be that the method FromBluetoothAddressAsync
should return a Task.
From "BluetoothLEDevice Class", I can check that FromBluetoothAddressAsync
method has this signature:
public static IAsyncOperation<BluetoothLEDevice> FromBluetoothAddressAsync(
UInt64 bluetoothAddress,
BluetoothAddressType bluetoothAddressType
)
which means that it returns IAsyncOperation<BluetoothLEDevice>
. The way I see it, this seems enough to be recognized as something as a Task<>
.
Is the problem due to a broken link which allows IAsyncOperation<>
to be recognized as a child of Task<>
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…