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

macOS 12+: Accept service UUIDs when scanning for devices #88

Closed
wants to merge 1 commit into from
Closed

macOS 12+: Accept service UUIDs when scanning for devices #88

wants to merge 1 commit into from

Conversation

mplewis
Copy link

@mplewis mplewis commented Dec 26, 2021

Issue

In macOS 12 Monterey, developers have been reporting issues with CoreBluetooth device discovery. This issue manifests as a BLE scan returning no device results. This currently affects lots of BLE projects that use CoreBluetooth under the hood, such as Python Bleak.

Fix

The fix for this issue is to provide a non-empty list of service UUIDs to scanForPeripheralsWithServices:options:.

More context

This issue (feature?) has not yet been officially acknowledged by Apple. In the meantime, other projects are working to add user ergonomics around this issue. Bleak warns users who do not provide a list of advertised service UUIDs that MacOS will not return any results for their scan.

To serve macOS users, I would like to add support for tinygo Bluetooth to take a list of advertised service UUIDs when starting to scan. This is a first attempt at adding support for this to the Darwin adapter.

I know this breaks the interface and will not compile, but I'd like to start a conversation with this first draft of code. I'm totally OK with maintainers editing this PR however they like. I'm not familiar with this codebase and want to help fit this fix into tinygo – how can I help?

@aykevl
Copy link
Member

aykevl commented Dec 30, 2021

You bring up an interesting issue. Here are some potentially similar issues:

  • If we ever want to support WebBluetooth, we would also have to pass the filter parameters to the Scan method too.
  • Some BLE stacks may prefer to filter early for performance reasons.

So I do think it is useful to provide filter parameters to the Scan method. However:

  • It should be the same interface across all platforms. We can't have MacOS diverge here, that breaks the whole idea of having an easy to use cross platform BLE package.
  • I think it would be better to provide a generic filter parameter, not just a list of service UUIDs.

My proposal would be to add a filter parameter, something similar to WebBluetooth:

func (a *Adapter) ScanFilter(filters []ScanFilter) (ScanResult, error) { ... }

type ScanFilter struct {
    Services []UUID // device must have all services given in this list
    // might add more filters in the future
}

For most platforms, it could simply call a.Scan and filter the devices according to these filter rules. For MacOS, this could be implemented in a way that it flattens the list of service UUIDs passed to a.cm.Scan and then filters devices like on other platforms.

For reference: https://developer.mozilla.org/en-US/docs/Web/API/Bluetooth/requestDevice

@mplewis
Copy link
Author

mplewis commented Dec 30, 2021

Thanks for your feedback. I like the approach you suggest here. I will try to implement a filter parameter like the one above.

@quentinlesceller
Copy link

Seems like macOS 12.3 will fix this bug. See hbldh/bleak#753.

@mplewis
Copy link
Author

mplewis commented Feb 10, 2022

Fantastic. I think then we can ignore this PR and simply not implement this change for now.

@mhaberler
Copy link

upgrading to MacOS Monterey 12.3 Public Beta5 fixed this for me

@mplewis mplewis closed this Mar 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants