Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
595 views
in Technique[技术] by (71.8m points)

c# - FromBluetoothAddressAsync IAsyncOperation does not contain a definition for 'GetAwaiter' error

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To await an IAsyncOperation, you need two things:

  • A reference to C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETCorev4.5System.Runtime.WindowsRuntime.dll
  • A reference to C:Program Files (x86)Windows Kits10UnionMetadataFacadeWindows.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...