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

No results when scanning on MacOS 12 #635

Closed
peterloron opened this issue Sep 8, 2021 · 55 comments · Fixed by #692
Closed

No results when scanning on MacOS 12 #635

peterloron opened this issue Sep 8, 2021 · 55 comments · Fixed by #692
Labels
Backend: Core Bluetooth Issues and PRs relating to the Core Bluetooth backend

Comments

@peterloron
Copy link

  • bleak version: 0.12.1
  • Python version: 3.9.6
  • Operating System: MacOS 12 Beta (Monterey)

Description

I installed bleak via pip, and used the scanning example from the docs. When the code is run, no devices are found. I can run a different BLE scanner and see many devices. Running as root doesn't change anything.

Happy to help investigate, but I'm not sure where to go from here.

@dlech
Copy link
Collaborator

dlech commented Sep 8, 2021

Happy to help investigate, but I'm not sure where to go from here.

https://bleak.readthedocs.io/en/latest/troubleshooting.html

@dlech dlech added the Backend: Core Bluetooth Issues and PRs relating to the Core Bluetooth backend label Sep 8, 2021
@dlech
Copy link
Collaborator

dlech commented Sep 8, 2021

@peterloron
Copy link
Author

Hello @dlech. Apologies, I should have mentioned I did read the troubleshooting section, open issues, etc. I enabled the debug env var, but it did not show anything interesting:

2021-09-08 16:00:37,577 bleak.backends.corebluetooth.CentralManagerDelegate DEBUG: centralManagerDidUpdateState_
DEBUG:bleak.backends.corebluetooth.CentralManagerDelegate:centralManagerDidUpdateState_
2021-09-08 16:00:37,577 bleak.backends.corebluetooth.CentralManagerDelegate DEBUG: Bluetooth powered on
DEBUG:bleak.backends.corebluetooth.CentralManagerDelegate:Bluetooth powered on
2021-09-08 16:00:37,578 bleak.backends.corebluetooth.CentralManagerDelegate DEBUG: 'isScanning' changed
DEBUG:bleak.backends.corebluetooth.CentralManagerDelegate:'isScanning' changed
2021-09-08 16:00:42,581 bleak.backends.corebluetooth.CentralManagerDelegate DEBUG: 'isScanning' changed
DEBUG:bleak.backends.corebluetooth.CentralManagerDelegate:'isScanning' changed
2021-09-08 16:00:42,581 bleak.backends.corebluetooth.CentralManagerDelegate DEBUG: 'isScanning' changed
DEBUG:bleak.backends.corebluetooth.CentralManagerDelegate:'isScanning' changed
...

The code I am running is exactly the detection_callback.py code you pointed to.

I did a packet capture, but I'm unable to determine if there is any useful information in there. I can post it somewhere if it would be useful.

@dlech
Copy link
Collaborator

dlech commented Oct 6, 2021

I can look at the packet capture if you attach it to the issue (just put it in a .zip file).

@jonskywalker
Copy link

bleakMacOS.pklg.zip
Running into the same issue attached packet logger output if it helps. When enabling debug logging it's the same results reported above.
Bleak 0.13
Python 3.10
MacOS 12

@dlech
Copy link
Collaborator

dlech commented Oct 23, 2021

Thanks for the info. I'm able to reproduce the issue on my Mac as well now. I also found https://stackoverflow.com/q/69661809/1976323, so I don't think it is just Bleak. I would suggest filing a bug with Apple.

@dlech
Copy link
Collaborator

dlech commented Oct 23, 2021

This thread hints that there is a bug in macOS and they were able to work around it, but I don't think it is open source, so I don't know what the fix is. https://forums.zwift.com/t/macos-monterey-cannot-establish-bluetooth-connection/554193/27

@dlech
Copy link
Collaborator

dlech commented Oct 23, 2021

I tried this workaround but it doesn't seem to make a difference.

@gjw
Copy link

gjw commented Oct 27, 2021

Confirming this is still an issue in macOS 13

@michael-deng
Copy link

Just want to echo that I'm encountering the same issue in monterey.

@oamado
Copy link

oamado commented Nov 10, 2021

I see there are changes on https://github.com/ronaldoussoren/pyobjc, but not a new version since Jun 7, 2021. Probably this could help to solve the issue.

@dlech
Copy link
Collaborator

dlech commented Nov 10, 2021

What specific changes might be related to Bluetooth? I have already tried https://pypi.org/project/pyobjc/8.0b1/.

@dlech
Copy link
Collaborator

dlech commented Nov 12, 2021

PyObjC v8.0 is out now and it doesn't fix the problem

@kettenbach-it
Copy link

PyObjC v8.0 is out now and it doesn't fix the problem

I can conform that.

This really needs to be fixed! It's a showstopper for me

@dlech
Copy link
Collaborator

dlech commented Nov 22, 2021

Please report the issue to Apple using the Feedback Assistant app. I'm not sure what else we can do since the problem seems to come from an undocumented change in the OS. If enough people report the issue, maybe they will prioritize it.

@challiwill
Copy link

Can someone who understands this help me wrap my head around why I can still scan for devices using Chrome's Web Bluetooth, but can't with bleak? Is there somehow a different set of library functions exposed in c++? Is this a bug at the pyobjc level? I've been trying to read the chromium source code but must admit I'm struggling.

@VladislavSmolyanoy
Copy link

Can someone who understands this help me wrap my head around why I can still scan for devices using Chrome's Web Bluetooth, but can't with bleak? Is there somehow a different set of library functions exposed in c++? Is this a bug at the pyobjc level? I've been trying to read the chromium source code but must admit I'm struggling.

I am unfamiliar with the framework that Web Bluetooth uses but bleak is built on top of Core Bluetooth which is currently broken in macOS Monterey...

@kettenbach-it
Copy link

kettenbach-it commented Nov 23, 2021

Since tools such as "LightBlue", which I like to use to develop in the BLE environment, and others work perfectly, the fault - in my humble opinion - is more likely to be with the PyObjc Framework CoreBlueooth or even within Bleak.

Stupid question: I digged into the code "behind" BleakScanner.discover().
I see:

    @classmethod
    async def discover(cls, timeout=5.0, **kwargs) -> List[BLEDevice]:
        async with cls(**kwargs) as scanner:
            await asyncio.sleep(timeout)
            devices = scanner.discovered_devices
        return devices

and scanner.discovered_devicesis:

    @property
    @abc.abstractmethod
    def discovered_devices(self) -> List[BLEDevice]:
        raise NotImplementedError()

See

raise NotImplementedError()

How can this method ever return a list of devices? It has no return. However, no exception is raised for me either.

@kettenbach-it
Copy link

kettenbach-it commented Nov 23, 2021

Ah, sorry. Stupid me. It's abstract. I need to look in the respective implementation which is here:

def discovered_devices(self) -> List[BLEDevice]:

@dlech dlech closed this as completed in #692 Dec 7, 2021
@kettenbach-it
Copy link

Okay, thx!

mkoderer added a commit to mkoderer/bleak that referenced this issue Dec 30, 2021
On macos the service_uuids need to be filled. Main issue for
beginners is to retrieve some valid uuids. Instead of using an
external tool like nRF we could use predefined ones in bleak/uuids.

Implementation works with 128bit and 16bit UUIDs.

Related-to hbldh#635

Signed-off-by: Marc Koderer <[email protected]>
@mkoderer
Copy link
Contributor

I also tried the service filter trick that some reported but it does not work on my system

Can you share your raw advertising data (you can get this, for example, with the nRF Connect app for iOS or Android, or by logging Bluetooth packets on macOS). So far I've only tested with 128-bit UUIDs, so I wonder if we may need to make a tweak for 16-bit and 32-bit UUIDs.

The current implementation works also with 16-bit. For 16-bit the mac implementation will automatically extend it by the default 128-bit mask:

> python examples/detection_callback.py 0xFE07
2021-12-30 08:14:49,474 __main__ INFO: ... service_uuids=['0000fe07-0000-1000-8000-00805f9b34fb

@dlech
Copy link
Collaborator

dlech commented Dec 30, 2021

Thanks for sharing. I'm surprised it works even works from the command line. 👍

mkoderer added a commit to mkoderer/bleak that referenced this issue Dec 30, 2021
On macos the service_uuids need to be filled. Main issue for
beginners is to retrieve some valid uuids. Instead of using an
external tool like nRF we could use predefined ones in bleak/uuids.

Implementation works with 128bit and 16bit UUIDs.

Related-to hbldh#635

Signed-off-by: Marc Koderer <[email protected]>
mkoderer added a commit to mkoderer/bleak that referenced this issue Dec 30, 2021
On macos the service_uuids need to be filled. Main issue for
beginners is to retrieve some valid uuids. Instead of using an
external tool like nRF we could use predefined ones in bleak/uuids.

Implementation works with 128bit and 16bit UUIDs.

Related-to hbldh#635

Signed-off-by: Marc Koderer <[email protected]>
@mkoderer
Copy link
Contributor

Not sure if this is already known here but Bluetility UI works on my mac without any specific service uuids.
I have no idea what's actually the difference since the code looks quite similar:

https://github.com/jnross/Bluetility/blob/2c91f93ba0715c354d5cdedbbf868cc2d5160d40/Bluetility/Scanner.swift#L36

@dlech
Copy link
Collaborator

dlech commented Dec 30, 2021

I think the difference is that it is an app (and possibly in addition to that it requests permission) and is not a command line tool like Python. There are several other comments in this issue that also noticed this that may be of interest.

dlech added a commit that referenced this issue Jan 8, 2022
Added
-----

* Added ``service_uuids`` kwarg to  ``BleakScanner``. This can be used to work around issue of scanning not working on macOS 12. Fixes #230. Works around #635.
* Added UUIDs for LEGO Powered Up Smart Hubs.

Changed
-------

* Changed WinRT backend to use GATT session status instead of actual device connection status.
* Changed handling of scan response data on WinRT backend. Advertising data and scan response data is now combined in callbacks like other platforms.
* Updated ``bleak-winrt`` dependency to v1.1.0. Fixes #698.

Fixed
-----

* Fixed ``InvalidStateError`` in CoreBluetooth backend when read and notification of the same characteristic are used. Fixes #675.
* Fixed reading a characteristic on CoreBluetooth backend also triggers notification callback.
* Fixed in Linux, scanner callback not setting metadata parameters. Merged #715.
@dlech dlech mentioned this issue Jan 8, 2022
dlech added a commit that referenced this issue Jan 10, 2022
Added
-----

* Added ``service_uuids`` kwarg to  ``BleakScanner``. This can be used to work around issue of scanning not working on macOS 12. Fixes #230. Works around #635.
* Added UUIDs for LEGO Powered Up Smart Hubs.

Changed
-------

* Changed WinRT backend to use GATT session status instead of actual device connection status.
* Changed handling of scan response data on WinRT backend. Advertising data and scan response data is now combined in callbacks like other platforms.
* Updated ``bleak-winrt`` dependency to v1.1.0. Fixes #698.

Fixed
-----

* Fixed ``InvalidStateError`` in CoreBluetooth backend when read and notification of the same characteristic are used. Fixes #675.
* Fixed reading a characteristic on CoreBluetooth backend also triggers notification callback.
* Fixed in Linux, scanner callback not setting metadata parameters. Merged #715.
@dlech
Copy link
Collaborator

dlech commented Jan 10, 2022

FYI, I've added a summary of the findings of this issue with additional insights at #720 (comment).

@maximilian-kn
Copy link

Any updates on this issue? I'm currently experiencing problems trying to connect to a BLE device using a script with Bleak in Python.

@dlech
Copy link
Collaborator

dlech commented Sep 21, 2023

Apple fixed the bug in macOS 12.2. If you still have problems the best way to get help is to start a new discussion and include you code, error messages and logs.

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.