M. Hartner
M. Hartner

Reputation: 440

BLE Using WinRT: Access Denied When Executing "GetCharacteristicsForUuidAsync()" for Write Characteristic

Maybe someone can help me out because I have a really tricky situation with Bluetooth LE using WinRT on Windows 10 (like supposed here: Bluetooth Low Energy in .Net (C#)).

I need BLE within a Win32 classic desktop application. Our code is running in a 32 Bit frame application using the .NET runtime (v4.0.30319, .NET Framework 4.6.2). I was able to manage all the other issues (strong naming some NuGet assemblies (Shiny.BluetoothLE), running BluetoothAdapter.GetRadioAsync() in a 64 Bit COM surrogate DLL when running on 64 Bit Windows) but now I am totally stuck with this and here’s where:

The problem occurrs when executing var result = await gattDeviceService.GetCharacteristicsForUuidAsync(uuid, BluetoothCacheMode.Cached); see here https://learn.microsoft.com/de-de/uwp/api/windows.devices.bluetooth.genericattributeprofile.gattdeviceservice.getcharacteristicsforuuidasync?view=winrt-20348. When getting the read characteristics, the result.Status is GattCommunicationStatus.Success and the result contains the desired characteristic. But of course I also need to get the write characteristic and I am ALWAYS getting GattCommunicationStatus.AccessDenied! Because of that the result contains NO characteristic.

Has anybody a clue why is that? I really need help here because I am kinda lost right now…

I also tried to set AccessPermission via registry like supposed here but no luck at all…

PS: I use Windows 10 SDK Kit Build 20348 and like stated above it is a C# .NET Framework 4.6.2 project and all our assemblies are strong named because of using GAC. If I am missing anything don’t hesitate to contact me.

Upvotes: 5

Views: 2173

Answers (3)

user9184864
user9184864

Reputation: 11

I know this is very late for answer but for anyone having similar problem as mine can try this.

As M.Hartner Mentioned above, .NET lets you call only once for getting characteristics. In my case I was watching for devices that are connecting and disconnecting. When a device is reconnected, I was getting access denied as .NET was thinking there is still link between Bluetooth devices. The correct way to solve this issue is by disposing the GattDeviceService when device is disconnected. So every time DeviceWatcher notifies that device has been removed, I will dispose the 'GattDeviceService' of that device.

 private void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate args);
 {
     var bleDevice= GetDeviceWithID(args.Id);
     bleDevice.gattDeviceService.Dispose();
 }

In your case if you are requesting GATT characteristics multiple times in same session(i.e. Device is still in connected state) then better option as you mentioned in answer is to maintain a local copy of characteristics.

Upvotes: 1

SjoCi
SjoCi

Reputation: 121

I may have found a way to get around the problem.

tl;dr: Dispose both the BluetoothLEDevice and the GattDeviceService you get while connecting! The system thinks it is still in use!

In my case (using Windows 10, but not an UWP application, calling WinRT libs) this happens: Only the first call gives me the attributes, then "Access Denied". After some time a single call will give a valid answer again, but it's pretty rare (in one case after 52 seconds, in another 21 seconds).

In this case I will try to make a solution when everytime I'm asking for data, I will build the BLE from ground up (device detection, looking for correct device, getting attributes), get the data and then destroy everything, then do it again everytime I need data from a device. This feels very wrong and could lead to more problems. The reasoning behind this that the first time I collect information, everything is accessible.

In fact I did not need a total rebuild! I just needed to dispose both the BluetoothLEDevice and the GattDeviceService from my connection. I got my device from

BluetoothLEDevice device = await BluetoothLEDevice.FromIdAsync(deviceInfo.Id);

And my service with

GattDeviceService service = await device.GetGattServicesForUuidAsync(serviceUUID, BluetoothCacheMode.Uncached);

After I disposed them, a new call of connection (from BluetoothLEDevice.FromIdAsync(deviceInfo.Id)) works again!

service.Dispose();
device.Dispose();

Upvotes: 3

M. Hartner
M. Hartner

Reputation: 440

For anyone who stumbles over the same stupid issue...Here's the solution:

On Windows, using .NET Framework 4.6.1+ and the WinRT libraries inside a Non-UWP application, you can only call ONCE for getting the characteristics, no matter if you call for all at once via gattDeviceService.GetCharacteristicsAsync(BluetoothCacheMode.Uncached) or for a specific one via gattDeviceService.GetCharacteristicsForUuidAsync(uuid, BluetoothCacheMode.Cached).

Any subsequent calls will fail with GattCommunicationStatus.AccessDenied... So my solution now is to retrieve all characteristics at once and filter them locally.

That did the trick! Anyways, this is so stupid...It wasted a lot of my time now! As it seems, I also do not need to set any AccessPermission via registry.

PS: I will call out to you, if I stumble over another tricky situation, just to let you guyz know.

Upvotes: 4

Related Questions