3

I am building an app for the hololens using Unity. I want to connect to a bluetooth device. I started building a plugin in Visual Studio which I could call from my Unity project to find and connect to a device using bluetooth. I used the documentation of Microsoft regarding this project: https://learn.microsoft.com/en-us/windows/uwp/devices-sensors/gatt-client

The query for nearby devices works like a charm. Upon calling this function the events are properly launched when a device is found or removed. But when trying to create the function to connect to a device using the BluetootLEDevice.FromIdAsync() I get the following error:

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

I am using the System reference though, so what am I doing wrong here? This is the code I'm using:

private static async void ConnectDevice(string deviceInfoID)
    {
        // Note: BluetoothLEDevice.FromIdAsync must be called from a UI thread because it may prompt for consent.
        BluetoothLEDevice bluetoothLeDevice = await BluetoothLEDevice.FromIdAsync(deviceInfoID);
    }
Erik
  • 65
  • 8
  • Did you miss the "using System" reference at the beginning of your class? – TheTanic May 15 '17 at 06:57
  • Nope, first line I wrote – Erik May 15 '17 at 07:06
  • 1
    Add those two references: `C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd` and `C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll`. Exact path depends on your OS and Framework version. – Manfred Radlwimmer May 15 '17 at 07:14
  • @ManfredRadlwimmer I can connect to a device now, but the next problem arrived. When I try to get the GattDeviceServicesResult using: GattDeviceServicesResult result = await connectedDevice.GetGattServicesAsync(); it says I am missing another using directive or assembly reference. Something I must add as well? – Erik May 15 '17 at 11:37
  • Uhm ... probably. What does it say **exactly**? You probably need to convert between `IAsyncOperation<>` and `Task<>` again or you are missing something else ... hard to say without the actual code. Is it VS that is complaining or is this already in Unity? – Manfred Radlwimmer May 15 '17 at 11:51
  • It's in Visual Studio. I try to do the following: private static async void RetrieveData() { GattDeviceServicesResult result = await bluetoothLeDevice.GetGattServicesAsync(); if (result.Status == GattCommunicationStatus.Success) { var services = result.Services; } } – Erik May 15 '17 at 11:54
  • Quick Question: Are you by any chance running this on Win10 with the Creators Update already installed? – Manfred Radlwimmer May 15 '17 at 11:55
  • I am using VS 2015 on Win10 but not with the Creators Update – Erik May 15 '17 at 11:58
  • Ok, because that causes a whole lot of additional issues (related: https://stackoverflow.com/questions/43568096/frombluetoothaddressasync-never-returns-on-windows-10-creators-update-in-wpf-app, maybe some of the code in that question can help). I don't have a BLE device handy atm, but I'll see if I can reproduce your issue later. – Manfred Radlwimmer May 15 '17 at 12:00
  • It's reproducable without a BLE device. I am just following the MSDN steps for reading a BLE device, but it it not that well documentated. – Erik May 15 '17 at 12:28
  • That last error might be because you use a different version (i.e. .Net Framework, WinRT, Standard, Core, etc.) of the API than the documented one (the members and functions of `BluetoothLEDevice` differ greatly between the different versions and frameworks). Have you tried something like `bluetoothLeDevice.GattServices` or `bluetoothLeDevice.GetGattService(serviceUuid)` instead? – Manfred Radlwimmer May 16 '17 at 15:49

1 Answers1

0

My answer is late, but I was struggling with the same problem, and the solution is to include this .dll "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETCore\v4.5\System.Runtime.WindowsRuntime.dll"

Andrey Mazur
  • 510
  • 4
  • 14