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

GattCommunicationStatus.Unreachable in BleakClientDotNet.get_services() #60

Closed
dlech opened this issue Apr 13, 2019 · 4 comments
Closed
Assignees
Labels
Backend: pythonnet Issues or PRs relating to the .NET/pythonnet backend

Comments

@dlech
Copy link
Collaborator

dlech commented Apr 13, 2019

  • bleak version: b658f52
  • Python version: 3.6.8
  • Operating System: Windows 10 1809

Description

Testing out the develop branch. BleakClientDotNet.connect() throws BleakDotNetTaskError because GetGattServicesAsync() in BleakClientDotNet.get_services() returns GattCommunicationStatus.Unreachable.

This seems to be a timing issue. For some reason, GetGattServicesAsync returns before it actually connects to the device. If I add a delay before calling GetGattServicesAsync, it works more reliably. It also seems to work if I add a retry instead of a delay.

            async def GetGattServicesAsync():
                return await wrap_IAsyncOperation(
                    IAsyncOperation[GattDeviceServicesResult](
                        self._requester.GetGattServicesAsync()
                    ),
                    return_type=GattDeviceServicesResult,
                    loop=self.loop,
                )
            
            services_result = await GetGattServicesAsync()
            
            if services_result == GattCommunicationStatus.Unreachable:
                # one retry because Windows is goofy
                services_result = await GetGattServicesAsync()

            if services_result.Status != GattCommunicationStatus.Success:
                raise BleakDotNetTaskError("Could not get GATT services.")

This thread lead me to this thread which got me wondering if this behavior has something to do with needing a single threaded apartment on Windows.

@dlech
Copy link
Collaborator Author

dlech commented Apr 14, 2019

After some more testing, the single retry method doesn't always work. It would probably be better with multiple retries and a timeout.

@hbldh
Copy link
Owner

hbldh commented Apr 18, 2019

Might this be an effect of the BluetoothLEAdvertisementWatcher discovery change? Or maybe even the FromBluetoothAddressAsync change? Have you experienced the same problems prior to this change?

I did notice that during the discovery phase and reading of manufacturer data, my test device stopped blinking (which it does to signal that it is possible to connect to). Does Windows create a connection during the reading of the manufacturer data which interferes with the services detecting later on?

@hbldh hbldh self-assigned this Jun 24, 2019
@hbldh hbldh added the Backend: pythonnet Issues or PRs relating to the .NET/pythonnet backend label Jun 24, 2019
@zyv
Copy link

zyv commented Sep 15, 2019

I think one of my library users experiences the same problem - he reported that if he just comments out the exception (I'm not using discovered services anyways) - everything works.

@hbldh
Copy link
Owner

hbldh commented Jun 2, 2020

I think this is solved in later versions. Will close for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backend: pythonnet Issues or PRs relating to the .NET/pythonnet backend
Projects
None yet
Development

No branches or pull requests

3 participants