Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

access denied error in windows backend due to GattServicesChanged event #1061

Closed
dlech opened this issue Oct 3, 2022 · 1 comment · Fixed by #1062
Closed

access denied error in windows backend due to GattServicesChanged event #1061

dlech opened this issue Oct 3, 2022 · 1 comment · Fixed by #1062
Labels
Backend: WinRT Issues or PRs relating to the WinRT backend bug Something isn't working

Comments

@dlech
Copy link
Collaborator

dlech commented Oct 3, 2022

I had a very similar problem while writing data to a Nordic UART service. I had many access denies errors when I started with bleak version 0.13 and after updating to 0.15 it change more to "The object is already closed" or "A method was called at an unexpected time" (don't know the exact error messages as my system reported them in German)
The problem for me was that the device kept updating the service list during service discovery. I added a gatt_services_changed listener to the WinRT client and started over with service discovery if it is was currently running or started a new discovery before accessing the services list the next time.
The documentation of the GattServicesChanged event (https://docs.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.bluetoothledevice.gattserviceschanged) says:

This event is raised when the remote device changes its services, or an unpaired device is disconnecting. All services are cleared because unpaired device services can't be cached between connections.
The object parameter in this event is null for every event that is raised.
In your handler for this event, do the following in order to get the services available. Call BluetoothLEDevice.GetGattServicesAsync on the BluetoothLEDevice that's passed to your handler. When calling GetGattServicesAsync, use the Cached option. This can be used to refresh the list of services and re-initialize the services on the device if they are gone.

For my device I get the event 11 times during the first service discovery and if I reload them after I got the event everything works as expected.
I can open a PR in the next days with a suggestion how to deal with this issue.

Originally posted by @jochenjagers in #849 (comment)

@dlech dlech added bug Something isn't working Backend: WinRT Issues or PRs relating to the WinRT backend labels Oct 3, 2022
@dlech
Copy link
Collaborator Author

dlech commented Oct 4, 2022

This seems like a tricky one to handle. We can't know if a device has a Services Changed characteristic until services have been resolved. And we can't know when Windows decides to enable indications on this characteristic. If we get the event during get_services(), then we can cancel the GetGattServicesAsync call and try again. But we don't currently have a nice way to handle this if we get the signal after get_services() returns.

dlech added a commit that referenced this issue Oct 4, 2022
…nt occurs

We can get an access denied error for services returned before a
services changed event. This handles services changed events that are
received while attempting to get services. Hopefully, this is the only
time this event is received in most cases.

Fixes #1061.
dlech added a commit that referenced this issue Oct 4, 2022
…nt occurs

We can get an access denied error for services returned before a
services changed event. This handles services changed events that are
received while attempting to get services. Hopefully, this is the only
time this event is received in most cases.

Fixes #1061.
dlech added a commit that referenced this issue Oct 13, 2022
…nt occurs

We can get an access denied error for services returned before a
services changed event. This handles services changed events that are
received while attempting to get services. Hopefully, this is the only
time this event is received in most cases.

Fixes #1061.
@dlech dlech mentioned this issue Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backend: WinRT Issues or PRs relating to the WinRT backend bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant