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<>
?
IAsyncOperation does not contain a definition for 'GetAwaiter' and the best extension method overload 'windowsRuntimeSystemExtensions.GetAwaiter(IAsyncAction)' requires a receiver of type 'IAsyncAction'.
When you use methods that return IAsyncOperation<TResult> (with a TResult specific constraint) in your app code, you usually don't access the IAsyncOperation return value directly. That's because you almost always use the language-specific awaitable syntax.
It's not common to use IAsyncOperation<TResult> directly even if you don't use a language-specific awaitable syntax. Each of the languages has extension points that are generally easier to use than the Windows Runtime interface. JavaScript has WinJS.Promise, and the then/done syntax. .
'IEnumerable' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'IEnumerable' could be found (are you missing a using directive or an assembly reference?)
To await an IAsyncOperation
, you need two things:
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll
C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Facade\Windows.WinMD
If either reference is missing then it won't work. You can also use the UwpDesktop nuget package, that will do the work for you.
Note: specifically GetAwaiter
is extension in System
namespace that is available from those references (you still need using System;
- make sure you have not removed it from the file). The extension info is on MSDN - WindowsRuntimeSystemExtensions.GetAwaiter.
For some of the other UWP operations just add using System
:
using System;
//...
// example - getting file reference:
var file = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("myFile.txt);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With