-
Notifications
You must be signed in to change notification settings - Fork 304
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
CoreBluetooth fixes #227
CoreBluetooth fixes #227
Conversation
The _central_manager_delegate_ready task doesn't appear to do anything useful (not used by anything) and it may never finish which can cause an error when an application exits. Task was destroyed but it is pending! task: <Task pending name='Task-2' coro=<Application._central_manager_delegate_ready() running at bleak/backends/corebluetooth/__init__.py:46>>
This replaces hard-coded numbers with constants from objc bindings
core bluetooth has to wait until centralManagerDidUpdateState_ is called and the state is CBManagerStatePoweredOn before any other bluetooth functions can be used. Since __init__ can't be async, this means we have to move the check to the other async entry points of discovery and scanner.
Apparently some things were removed from the CentralManagerDelegate that kept track of scanned peripherals. This replaces it with a new implementation in BleakScannerCoreBluetooth instead.
This replaces the NSRunLoop integration in the corebluetooth backend with a dispatch queue. This causes callbacks to be dispatched on a background thread instead of on the main dispatch queue on the main thread. `call_soon_threadsafe()` is used to synchronize the events with the event loop where the central manager was created. The NSRunLoop caused problems because it had to manually be called from the main thread. This left an asyncio task that had to be manually stopped at the end of a program to prevent errors about the still running task (issue: hbldh#111). The NSRunLoop implementation was also not very efficient since it was waking up the event loop every millisecond to check for events.
This removes the global Application() class in the corebluetooth backend. Instead, a new central manager is created for each scanner object. Since an instance of the `Application()` was created on module import, it could interfere with other code like introspection tools that don't actually want to run `bleak`. This also fixes running `bleak` in threads (hbldh#206).
The pyobjc package requires in all pyobjc-* packages, most of which we don't use. This fixes the requirements so that we only install the packages we are using.
This looks very good! Thank you, this will definitely improve the starting point of my bleak Tuesday next week. Does the use of the dispatch queue alleviate any of the issues in #206? Or is the matter of a shared cbapp / central manager still impeding with multi-thread use of bleak in iOS? |
Ah, saw the note in one of the commits. Strike the question. Thank you! |
This looks terrific. Will merge and use it as basis for more work. Thank you very much! |
This is some of the fixes I have been working on. If fixes several of the outstanding CoreBluetooth issues. I didn't rebase on #209 because that PR seems a bit unfinished.