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

Is passive scanning implemented in the Linux backend? #606

Closed
koenvervloesem opened this issue Jul 25, 2021 · 7 comments · Fixed by #884
Closed

Is passive scanning implemented in the Linux backend? #606

koenvervloesem opened this issue Jul 25, 2021 · 7 comments · Fixed by #884
Labels
Backend: BlueZ Issues and PRs relating to the BlueZ backend enhancement New feature or request

Comments

@koenvervloesem
Copy link
Contributor

  • bleak version: 0.12.1
  • Python version: 3.8.10
  • Operating System: Ubuntu 20.04.2 LTS
  • BlueZ version (bluetoothctl -v) in case of Linux: 5.53

Description

I wanted to do a passive scan with Bleak. Issue #500 seemed to suggest that this is possible with the keyword argument scanning_mode='passive' to the BleakScanner constructor. So I added this argument while initializing the BleakScanner. However, Wireshark still shows me SCAN_REQ packets coming from my computer and SCAN_RSP packets as replies from my BLE devices, which suggests that Bleak is still doing an active scan.

What I Did

My Minimal Workable Example is:

import asyncio
from bleak import BleakScanner


async def run():
    devices = await BleakScanner(scanning_mode='passive').discover()
    for device in devices:
        print(device)


loop = asyncio.get_event_loop()
loop.run_until_complete(run())
@dlech
Copy link
Collaborator

dlech commented Jul 25, 2021

I think Windows is the only OS that has an API to select passive scanning.

@koenvervloesem
Copy link
Contributor Author

You're right, I don't see any way to choose passive scanning in the BlueZ D-Bus Adapter API description, so apparently this isn't possible (yet).

@elupus
Copy link
Contributor

elupus commented Oct 13, 2021

Is there any upstream issue on this? I wish i could run passive scan. I suspect my constant active scans crash my device.

@dlech
Copy link
Collaborator

dlech commented Oct 14, 2021

There is a fairly new advertisement monitor API which requires BlueZ >= 5.56 and Linux kernel >= 5.10. It should handle passive scanning, if I understand correctly.

@bdraco
Copy link
Contributor

bdraco commented Jul 7, 2022

Greetings

Home Assistant is currently overhauling our Bluetooth support and we are looking for a passive scanning option. Our current recommendation is to use bleak for new integrations. Thanks for that!

If this feature is viable soon we would want to only support bleak going forward, however if it’s something far future we likely need to consider supporting an additional Bluetooth library as well which we would really like to avoid.

If there is anything we can do to help move this forward, please let me know what is needed. Thank you!

@dlech
Copy link
Collaborator

dlech commented Jul 7, 2022

If a company that uses Bleak want to sponsor me to take a week off of work, I can have a go at it.

@balloob
Copy link

balloob commented Jul 7, 2022

I sent you an email 👍

dlech added a commit that referenced this issue Jul 13, 2022
This introduces a new global D-Bus manager and uses it for the
BleakScanner in the BlueZ backend. This is done in preparation for
using the new BlueZ advertisement monitor APIs (#606) and also partially
fixes leaking open file descriptors for D-Bus sockets (#805, #875).
dlech added a commit that referenced this issue Jul 13, 2022
This introduces a new global D-Bus manager and uses it for the
BleakScanner in the BlueZ backend. This is done in preparation for
using the new BlueZ advertisement monitor APIs (#606) and also partially
fixes leaking open file descriptors for D-Bus sockets (#805, #875).
dlech added a commit that referenced this issue Jul 13, 2022
This introduces a new global D-Bus manager and uses it for the
BleakScanner in the BlueZ backend. This is done in preparation for
using the new BlueZ advertisement monitor APIs (#606) and also partially
fixes leaking open file descriptors for D-Bus sockets (#805, #875).
dlech added a commit that referenced this issue Jul 13, 2022
This introduces a new global D-Bus manager and uses it for the
BleakScanner in the BlueZ backend. This is done in preparation for
using the new BlueZ advertisement monitor APIs (#606) and also partially
fixes leaking open file descriptors for D-Bus sockets (#805, #875).
@dlech dlech added enhancement New feature or request Backend: BlueZ Issues and PRs relating to the BlueZ backend labels Jul 13, 2022
dlech added a commit that referenced this issue Jul 13, 2022
This introduces a new global D-Bus manager and uses it for the
BleakScanner in the BlueZ backend. This is done in preparation for
using the new BlueZ advertisement monitor APIs (#606) and also partially
fixes leaking open file descriptors for D-Bus sockets (#805, #875).
dlech added a commit that referenced this issue Jul 29, 2022
Added
-----

* Added new ``assigned_numbers`` module and ``AdvertisementDataType`` enum.
* Added new ``bluez`` kwarg to ``BleakScanner`` in BlueZ backend.
* Added support for passive scanning in the BlueZ backend. Fixes #606.
* Added option to use cached services, characteristics and descriptors in WinRT backend. Fixes #686.
* Added ``PendingDeprecationWarning`` to use of ``address_type`` as keyword argument. It will be moved into the
  ``winrt`` keyword instead according to #623.
* Added better error message when adapter is not present in BlueZ backend. Fixes #889.

Changed
-------

* Add ``py.typed`` file so mypy discovers Bleak's type annotations.
* UUID descriptions updated to 2022-03-16 assigned numbers document.
* Replace use of deprecated ``asyncio.get_event_loop()`` in Android backend.
* Adjust default timeout for ``read_gatt_char()`` with CoreBluetooth to 10s. Merged #891.
* ``BleakScanner()`` args ``detection_callback`` and ``service_uuids`` are no longer keyword-only.
* ``BleakScanner()`` arg ``scanning_mode`` is no longer Windows-only and is no longer keyword-only.
* All ``BleakScanner()`` instances in BlueZ backend now use common D-Bus object manager.
* Deprecated ``filters`` kwarg in ``BleakScanner`` in BlueZ backend.
* BlueZ version is now checked on first connection instead of import to avoid import side effects. Merged #907.

Fixed
-----

* Documentation fixes.
* On empty characteristic description from WinRT, use the lookup table instead of returning empty string.
* Fixed detection of first advertisement in BlueZ backend. Merged #903.
* Fixed performance issues in BlueZ backend caused by calling "GetManagedObjects" each time a
  ``BleakScanner`` scans or ``BleakClient`` is connected. Fixes #500.
* Fixed not handling "InterfacesRemoved" in ``BleakClient`` in BlueZ backend. Fixes #882.
* Fixed leaking D-Bus socket file descriptors in BlueZ backend. Fixes #805.
@dlech dlech mentioned this issue Jul 29, 2022
dlech added a commit that referenced this issue Jul 29, 2022
Added
-----

* Added new ``assigned_numbers`` module and ``AdvertisementDataType`` enum.
* Added new ``bluez`` kwarg to ``BleakScanner`` in BlueZ backend.
* Added support for passive scanning in the BlueZ backend. Fixes #606.
* Added option to use cached services, characteristics and descriptors in WinRT backend. Fixes #686.
* Added ``PendingDeprecationWarning`` to use of ``address_type`` as keyword argument. It will be moved into the
  ``winrt`` keyword instead according to #623.
* Added better error message when adapter is not present in BlueZ backend. Fixes #889.

Changed
-------

* Add ``py.typed`` file so mypy discovers Bleak's type annotations.
* UUID descriptions updated to 2022-03-16 assigned numbers document.
* Replace use of deprecated ``asyncio.get_event_loop()`` in Android backend.
* Adjust default timeout for ``read_gatt_char()`` with CoreBluetooth to 10s. Merged #891.
* ``BleakScanner()`` args ``detection_callback`` and ``service_uuids`` are no longer keyword-only.
* ``BleakScanner()`` arg ``scanning_mode`` is no longer Windows-only and is no longer keyword-only.
* All ``BleakScanner()`` instances in BlueZ backend now use common D-Bus object manager.
* Deprecated ``filters`` kwarg in ``BleakScanner`` in BlueZ backend.
* BlueZ version is now checked on first connection instead of import to avoid import side effects. Merged #907.

Fixed
-----

* Documentation fixes.
* On empty characteristic description from WinRT, use the lookup table instead of returning empty string.
* Fixed detection of first advertisement in BlueZ backend. Merged #903.
* Fixed performance issues in BlueZ backend caused by calling "GetManagedObjects" each time a
  ``BleakScanner`` scans or ``BleakClient`` is connected. Fixes #500.
* Fixed not handling "InterfacesRemoved" in ``BleakClient`` in BlueZ backend. Fixes #882.
* Fixed leaking D-Bus socket file descriptors in BlueZ backend. Fixes #805.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backend: BlueZ Issues and PRs relating to the BlueZ backend enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants