Got CS4036 error 'IAsyncOperation<BluetoothLEDevice>' does not contain a definition for 'GetAwaiter' ....

Maurizio Bonelli 21 Reputation points
2022-11-27T08:39:55.993+00:00

Hello,
I am getting this error but I do not know how to recover it.

Error CS4036 'IAsyncOperation<BluetoothLEDevice>' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'IAsyncOperation<BluetoothLEDevice>' could be found (are you missing a using directive for 'System'?)

the code is

using System;  
using System.IO;  
using System.Collections;  
using System.Collections.Generic;  
using System.Data;  
using System.Data.SqlClient;  
using System.Runtime.InteropServices.WindowsRuntime;  
using System.Text;  
using System.Threading;  
using System.Threading.Tasks;  
using Windows.Devices.Enumeration.Pnp;  
using Windows.Foundation;  
using Windows.Devices.Bluetooth;  
using Windows.Devices.Bluetooth.GenericAttributeProfile;  
using Windows.Devices.Enumeration;  
using Windows.Storage.Streams;  
  
......  
static BluetoothLEDevice device;  
static DeviceWatcher watcher;  
static string deviceName;  
........  
async static void DeviceAdded(DeviceWatcher sender, DeviceInformation deviceInfo)  
{  
    // If the random device matches our device name, connect to it.  
    //Console.WriteLine("Device found: " + deviceInfo.Name);  
    if (deviceInfo.Name.CompareTo(deviceName) == 0)  
    {  
        // Get the bluetooth object and save it. This function will connect to the device  
       device = await BluetoothLEDevice.FromIdAsync(deviceInfo.Id);  
        Console.WriteLine("Connected to " + device.Name);  

        isDeviceConnected = true;  
    }  
}  

  

and the error is got at the line 13 "device = await ..... "

There are other points in the code with similar code with GattService or GattCharacteristic or .WriteValueWithResultAsync which are getting the same error.

The code I posted is a void routine, all the others are Task.

What do I have to do to correct my code?

Thanks a lot
Maurizio

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,610 questions
{count} votes

Accepted answer
  1. Jack J Jun 24,491 Reputation points Microsoft Vendor
    2022-11-29T06:34:55.823+00:00

    @Maurizio Bonelli , Welcome to Microsoft Q&A, based on my research, I could solve the above error.

    First, please install Nuget-Package System.Runtime.WindowsRuntime.

    Second, please add Windows.winmd from the following path:

    C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.19041.0  
    

    Like the following:

    265004-image.png

    Third, please rebuild your project and you will see the project is built successfully.

    Hope my solution could help you.

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    4 people found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Castorix31 83,101 Reputation points
    2022-11-27T12:57:14.15+00:00

    Have you added a reference to System.Runtime.WindowsRuntime.dll ?
    (you can see threads like Call WinRT API from Desktop App (On Windows 10)

    0 comments No comments

  2. Maurizio Bonelli 21 Reputation points
    2022-11-28T01:52:14.133+00:00

    @Castorix31

    Thanks for reply, but it appears I do not have that System.Runtime.WindowsRuntime.dll in my system, neither in .NETFramwork/4.8 (which is the one I am using for this code) nor in any others.
    In the ..../NET/4.5 there is a System.Runtime.WindowsRuntime.xml but I do not think it is the same, also because I am using .NETFramework.

    The code is an example got from https://github.com/SiliconLabs/bluetooth_stack_features/blob/master/firmware_upgrade/bluetooth_ota_from_windows/src/Program.cs
    and I will modify for my needing after it will work.... and I am not so skilled in async methods....

    thanks again
    Maurizio