21

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<>?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
kwagjj
  • 807
  • 1
  • 13
  • 23

3 Answers3

42

To await an IAsyncOperation, you need two things:

  • A reference to C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll
  • A reference to 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.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Kevin Gosse
  • 38,392
  • 3
  • 78
  • 94
22

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);
Marek
  • 1,688
  • 1
  • 29
  • 47
1

For a '.NET Standard 2.0' Library project, add these Nugets:

juwens
  • 3,729
  • 4
  • 31
  • 39