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

CoreBluetooth fixes #227

Merged
merged 9 commits into from
Jun 30, 2020
Merged

CoreBluetooth fixes #227

merged 9 commits into from
Jun 30, 2020

Conversation

dlech
Copy link
Collaborator

@dlech dlech commented Jun 26, 2020

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.

dlech added 9 commits June 25, 2020 20:22
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.
@hbldh
Copy link
Owner

hbldh commented Jun 26, 2020

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?

@hbldh
Copy link
Owner

hbldh commented Jun 26, 2020

Ah, saw the note in one of the commits. Strike the question. Thank you!

@hbldh hbldh self-assigned this Jun 30, 2020
@hbldh hbldh added Backend: Core Bluetooth Issues and PRs relating to the Core Bluetooth backend enhancement New feature or request labels Jun 30, 2020
@hbldh hbldh added this to the Version 0.7.0 milestone Jun 30, 2020
@hbldh
Copy link
Owner

hbldh commented Jun 30, 2020

This looks terrific. Will merge and use it as basis for more work. Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backend: Core Bluetooth Issues and PRs relating to the Core Bluetooth backend enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants