Search Unity

Build an application which uses Windows.Devices.Bluetooth.dll

Discussion in 'Windows' started by cflavio, Oct 5, 2018.

  1. cflavio

    cflavio

    Joined:
    Jul 2, 2013
    Posts:
    13
    Hi, I'm developing an application which uses Windows' BLE support. The application is really simple, it implements a class for using BLE (here is a part of the code):

    Code (CSharp):
    1. [...]
    2. using Windows.Devices.Bluetooth.Advertisement;
    3. using Windows.Devices.Enumeration;
    4. using Windows.Devices.Bluetooth;
    5. using Windows.Devices.Bluetooth.GenericAttributeProfile;
    6. using Windows.Storage.Streams;
    7.  
    8. [...]
    9. namespace Ble {
    10.  
    11.     class BleDevice {
    12.  
    13.         BluetoothLEDevice btdev;
    14.  
    15.         public BlebDevice(string addr, int freq=100, string logfile="") {
    16.             string[] requestedProperties = { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected" };
    17.             DeviceWatcher deviceWatcher = DeviceInformation.CreateWatcher(
    18.                 BluetoothLEDevice.GetDeviceSelectorFromPairingState(false),
    19.                 requestedProperties,
    20.                 DeviceInformationKind.AssociationEndpoint);
    21.             deviceWatcher.Added += DeviceWatcher_Added;
    22.             deviceWatcher.Updated += DeviceWatcher_Updated;
    23.             deviceWatcher.Removed += DeviceWatcher_Removed;
    24.             deviceWatcher.Start();
    25.  
    26.         private async void DeviceWatcher_Added(DeviceWatcher sender, DeviceInformation args) {
    27.             btdev = await BluetoothLEDevice.FromIdAsync(args.Id);
    28.             GattDeviceServicesResult result = await btdev.GetGattServicesAsync();
    29.             Guid customGuid = new Guid("xxx");
    30.             foreach (GattDeviceService service in result.Services) {
    31.                 if (customGuid == service.Uuid) {
    32.                     GattCharacteristicsResult cresult = await service.GetCharacteristicsAsync();
    33.                     GattCharacteristic ch = cresult.Characteristics[0];
    34.                     await ch.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
    35.                     ch.ValueChanged += Characteristic_ValueChanged;
    36.                 }
    37.             }
    38.         }
    39.  
    40.         void Characteristic_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args) {
    41.             DataReader reader = DataReader.FromBuffer(args.CharacteristicValue);
    42.             byte[] input = new byte[reader.UnconsumedBufferLength];
    43.             reader.ReadBytes(input);
    44.             [...]
    45.         }
    46.  
    47.         private async void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate args) { }
    48.  
    49.         private async void DeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate args) { }
    50.  
    51.     }
    52. }
    The project uses IL2CPP as scripting backend. I can create the UWP build successfully, and when I build the solution with Visual Studio, it correctly compiles. The problem is that I get several issues at runtime, and BLE is not working, e.g.

    Code (plain):
    1. onecoreuap\drivers\wdm\bluetooth\user\winrt\common\devquerydevpropprovider.cpp(297)\Windows.Devices.Bluetooth.dll!00007FFFA4CCB4F1: (caller: 00007FFFA4CCAFC6) ReturnHr(1) tid(974) 80070490 Element not found.
    2. onecoreuap\drivers\wdm\bluetooth\user\winrt\common\devquerydevpropprovider.cpp(131)\Windows.Devices.Bluetooth.dll!00007FFFA4CCB01A: (caller: 00007FFFA4CDEFE8) ReturnHr(3) tid(974) 80070490 Element not found.
    3. onecoreuap\drivers\wdm\bluetooth\user\winrt\device\bluetoothledevice.cpp(1417)\Windows.Devices.Bluetooth.dll!00007FFFA4CDF000: (caller: 00007FFFA4CE9591) LogHr(1) tid(974) 80070490 Element not found.
    4. onecoreuap\drivers\wdm\bluetooth\user\winrt\common\devquerydevpropprovider.cpp(297)\Windows.Devices.Bluetooth.dll!00007FFFA4CCB4F1: (caller: 00007FFFA4CCAFC6) ReturnHr(2) tid(56c) 80070490 Element not found.
    5. onecoreuap\drivers\wdm\bluetooth\user\winrt\common\devquerydevpropprovider.cpp(131)\Windows.Devices.Bluetooth.dll!00007FFFA4CCB01A: (caller: 00007FFFA4CDEFE8) ReturnHr(4) tid(56c) 80070490 Element not found.
    6. onecoreuap\drivers\wdm\bluetooth\user\winrt\device\bluetoothledevice.cpp(1417)\Windows.Devices.Bluetooth.dll!00007FFFA4CDF000: (caller: 00007FFFA4CE9591) LogHr(2) tid(56c) 80070490 Element not found.
    7. [...]
    8. onecoreuap\drivers\wdm\bluetooth\user\winrt\common\devquerydevpropprovider.cpp(297)\Windows.Devices.Bluetooth.dll!00007FFFA4CCB4F1: (caller: 00007FFFA4CCAFC6) ReturnHr(5) tid(2e84) 80070490 Element not found.
    9. onecoreuap\drivers\wdm\bluetooth\user\winrt\common\devquerydevpropprovider.cpp(131)\Windows.Devices.Bluetooth.dll!00007FFFA4CCB01A: (caller: 00007FFFA4CDEFE8) ReturnHr(6) tid(2e84) 80070490 Element not found.
    10. onecoreuap\drivers\wdm\bluetooth\user\winrt\device\bluetoothledevice.cpp(1417)\Windows.Devices.Bluetooth.dll!00007FFFA4CDF000: (caller: 00007FFFA4CE9591) LogHr(3) tid(2e84) 80070490 Element not found.
    11.  
    When I use the same code in isolation (i.e. in a Visual Studio project which doesn't use Unity) it correctly works: I can use BLE devices correctly.

    The entire log is attached. How can I get rid of these errors? Thank you very much!
     

    Attached Files:

  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    Can we get a bug report on this? It might be a bug in il2cpp somewhere.

    Another question: did you try stepping through the calls with a debugger (you can debug C# with il2cpp starting with 2018.2)? Did it get stuck on any of the calls?

    Did you enable Bluetooth capability in the manifest?

    If you enable native mode debugging in .NET UWP project where it works, do these errors show up too?
     
  3. cflavio

    cflavio

    Joined:
    Jul 2, 2013
    Posts:
    13
    Sure, I've reported it, thank you very much!
    Unfortunately, I've to write BLE instructions inside preprocessor directives #if !UNITY_EDITOR since they don't work in the editor. So, when I debug them, they're skipped.

    Yes.
    No, there aren't errors in the .NET project.

    Thank you!
     
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    What's the 7 digit case number that you got emailed after you reported it? Don't post the link, though.

    You can debug the player too. I didn't mean you should debug in the editor.
     
  5. cflavio

    cflavio

    Joined:
    Jul 2, 2013
    Posts:
    13
    1088764
    Oh, sorry. The problem is that when I try to attach the debugger I can see only Editor's process and not Player's one. In the project settings I've enabled: Development Build, Script Debugging and Wait For Managed Debugger (and actually I can see the waiting dialog when the application starts).
     
  6. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    Did you enable internet capabilities in the manifest (you need internet client, internet server and private networks)?
     
  7. cflavio

    cflavio

    Joined:
    Jul 2, 2013
    Posts:
    13
    Oh thanks, now I can debug it. Here is what happens. In my non-Unity application, when I debug its C# code, the debugger correctly process this instruction (doing a step by step debug):

    btdev = await BluetoothLEDevice.FromIdAsync(args.Id);


    and goes to the next instruction. When I debug the same line in the Unity application, Visual Studio shows the following error:

    Frame not in module. The current stack frame was not found in a loaded module.


    and I get the following error in the output:

    The thread 0x647b8980 has exited with code 0 (0x0).


    Thank you!
     
  8. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    We'll take a look at the bug report. And we're looking into why that would happen with the debugger - it seems related to await keyword. Sorry for inconvenience!
     
  9. LuGar

    LuGar

    Joined:
    Jun 13, 2019
    Posts:
    4
    Hey was it fixed, because it doesn't seem to work for me, unless it's something else that makes my thing fail.
     
  10. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    We could actually never reproduce the problem. We resolved the bug as non-reproducible and the original reporter never followed up on it. It worked fine on our end.

    What's failing for you?
     
  11. LuGar

    LuGar

    Joined:
    Jun 13, 2019
    Posts:
    4
    Hey, I think I found my issue, I was using "#if NETFX_CORE" around my code in order to make it work in the editor, but I guess since the backend is il2cpp it doesn't work for that, so i'm gonna try to replace my NETFX_CORE with WINDOWS_UWP like this dude suggested : https://forums.hololens.com/discussion/3217/enabling-ble-in-hololens-app and then I'll see if it works and I'll get back to you.
     
  12. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    Yeah, NETFX_CORE isn't defined on IL2CPP scripting backend as that define meant "reduced API surface for UWP", while IL2CPP has access to the full API surface.
     
  13. LuGar

    LuGar

    Joined:
    Jun 13, 2019
    Posts:
    4
    Yes thank you, the bluetooth now works.
     
  14. tanejankush

    tanejankush

    Joined:
    Nov 20, 2019
    Posts:
    1
    Hi

    I am facing the exact same issue in Hololens 1, Can you help me with that. Below is my code for accessing Bluetooth in Unity. I can build the project but after deploying it to Hololens 1 visual studio is giving me following errors.


    'Yamaha MR AVPRO.exe' (Win32): Loaded 'C:\Windows\System32\Windows.Devices.Bluetooth.dll'. Symbols loaded.
    'Yamaha MR AVPRO.exe' (Win32): Loaded 'C:\Windows\System32\Windows.Networking.dll'. Symbols loaded.
    'Yamaha MR AVPRO.exe' (Win32): Loaded 'C:\Windows\System32\FirewallAPI.dll'. Symbols loaded.
    'Yamaha MR AVPRO.exe' (Win32): Loaded 'C:\Windows\System32\fwbase.dll'. Symbols loaded.
    'Yamaha MR AVPRO.exe' (Win32): Loaded 'C:\Windows\System32\Windows.Internal.Bluetooth.dll'. Symbols loaded.
    'Yamaha MR AVPRO.exe' (Win32): Loaded 'C:\Windows\System32\deviceaccess.dll'. Symbols loaded.
    onecoreuap\drivers\wdm\bluetooth\user\winrt\common\devicehandle.cpp(117)\Windows.Devices.Bluetooth.dll!61C94C0D: (caller: 61C952C9) Exception(1) tid(10c0) 80070005 Access is denied.
    Exception thrown at 0x77062DE2 in Yamaha MR AVPRO.exe: Microsoft C++ exception: wil::ResultException at memory location 0x00E8E360.
    Exception thrown at 0x77062DE2 in Yamaha MR AVPRO.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000.
    onecoreuap\drivers\wdm\bluetooth\user\winrt\advertisement\bluetoothleadvertisementwatcher.cpp(1510)\Windows.Devices.Bluetooth.dll!61C73426: (caller: 61C74CC2) Exception(2) tid(10c0) 80070005 Access is denied.
    Exception thrown at 0x77062DE2 in Yamaha MR AVPRO.exe: Microsoft C++ exception: wil::ResultException at memory location 0x00E8E418.
    Exception thrown at 0x77062DE2 in Yamaha MR AVPRO.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000.
    The thread 0x1058 has exited with code 0 (0x0).
     

    Attached Files:

  15. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    It says access denied - did you enable the bluetooth capability?
     
  16. AnasEBarakat

    AnasEBarakat

    Joined:
    Jul 9, 2019
    Posts:
    4
    Hello,
    I am trying to create a game which uses a bluetooth device to capture the heart rate of the player. I have created a WPF library with the code needed to connect to the bluetooth device and receive the values, however, I am not sure how to use it in my Unity project.

    Since the library uses windows namespaces, iIhad to disable the dll for the editor and leave it only for WSAPlayer and standalone, now I am not sure how to reference the namespace of this library in my code and how to call the functions and receive values from it to be used in my project.

    I tried wrapping the code inside #if NETFX_CORE and #if ENABLE_WINMD_SUPPORT and even #if WINDOWS_UWP, how am I to debug if the connections and everything is working?

    Any help would be appreciated, thank you!
     
  17. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    You can attach a debugger and stepping through the code.

    Also, what do you mean "WPF library"? WPF isn't supported on UWP.
     
  18. Rickmc3280

    Rickmc3280

    Joined:
    Jun 28, 2014
    Posts:
    189
    Is there somewhere we can go to learn about accessing bluetooth information from the system ex Windows.Devices.Bluetooth within Unity? Seems a little crazy that Unity doesnt have something for talking to PC based bluetooth devices.
     
    faeg95 likes this.