0

I'm trying to get a BluetoothDevice from its address. When I try this:

// type of e is BluetoothLEAdvertisementReceivedEventArgs
BluetoothDevice device = BluetoothDevice.FromBluetoothAddressAsync(e.BluetoothAddress); 

I get this error:

Cannot implicitly convert type "Windows.Foundation.IAsyncOperation<Windows.Devices.Bluetooth.BluetoothDevice>" to "Windows.Devices.Bluetooth.BluetoothDevice"

In a similar post someone recommended to use a async method with await. So I tried this:

private async void NewDeviceDetected(BluetoothLEAdvertisementReceivedEventArgs e)
{
   if (InvokeRequired)
   {
      this.Invoke(new Action(() => NewDeviceDetected(e)));
      return;
   }

   //Get Device
   BluetoothDevice device = await BluetoothDevice.FromBluetoothAddressAsync(e.BluetoothAddress);
}

but now I'm getting this error:

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

I am using System;

Ottxr
  • 153
  • 3
  • 13
  • 1
    Well - your code looks fine, you should indeed use await - like the docs say: https://learn.microsoft.com/en-us/windows/uwp/threading-async/call-asynchronous-apis-in-csharp-or-visual-basic#return-types-and-results-of-asynchronous-apis . Have you checked this SO out? See if that is the issue: https://stackoverflow.com/questions/44099401/frombluetoothaddressasync-iasyncoperation-does-not-contain-a-definition-for-get – sommmen Oct 22 '20 at 08:14
  • @sommmen I was missing a reference to WindowsRuntime.dll . Thanks for the help! – Ottxr Oct 22 '20 at 08:28
  • ^^ `IAsyncOperation` doesn't have it's own `GetAwaiter()` method; it comes as an extension in WindowsRuntime.dll – Johnathan Barclay Oct 22 '20 at 08:39

0 Answers0