question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Devices that do not include a service UUID in advertising data cannot be detected on macOS 12.0 to 12.2 unless running in app created by py2app

See original GitHub issue
  • bleak version: 0.14.0
  • Python version: all
  • Operating System: macOS 12.x

Description

This is basically the same issue as #635. That issue was closed because there is now a workaround available in Bleak v0.14.0 that works in most cases. However, there is one case that doesn’t have an acceptable workaround. If a device does not include a service UUID in the advertising data, then the advertising data from that device cannot be received in Bleak.

Call to action

This is a regression from previous versions of macOS. Please everyone report this issue to Apple using the Feedback Assistant app. If enough people report it, hopefully they will prioritize fixing it.

Workaround

This isn’t great, but it is possible to convert your Python script to an app using py2app to work around the issue. Here is an example setup.py that can be uses as a starting point:

from setuptools import setup

setup(
    app=["my_script.py"],
    setup_requires=["py2app"],
    options=dict(
        py2app=dict(
            plist=dict(
                NSBluetoothAlwaysUsageDescription="This app uses Bluetooth.",
            ),
        ),
    ),
)

Then run python setup.py py2app to build the .app.

If your script does not have a graphical user interface, you can run it with:

open -n -W --stdin $TTY --stdout $TTY --stderr $TTY dist/my_script.app

See man open for more info.

Control C doesn’t work to stop the program, but you can secondary-click on the icon that is created in the dock and force quit if needed.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
dlechcommented, Jan 30, 2022

Good news! It looks like Apple has fixed the issue in macOS 12.3 Beta (21E5196i).

6reactions
dlechcommented, Jan 10, 2022

I did a deep dive into the logs in Console.app (enabled debug and info messages and filtered on bluetoothd and python/my_script processes) over the weekend and I think I have a pretty good understanding of the issue at this point. Following the advice of @arthurbiancarelli in https://github.com/hbldh/bleak/issues/635#issuecomment-981593036, I was able to successfully get things to work (scanning without specifying service uuids) when running Bleak in an app built with py2app. I also found a workaround so that you don’t have to create a graphical user interface (I updated the first comment in this issue with the workaround).

My theory is that Apple is bringing macOS closer inline with iOS. So the comments in the CoreBluetooth docs about foreground and background scanning now apply to macOS as well.

In order for your Python script to be considered a foreground app, it has to be a .app with an icon in the dock (doesn’t necessarily have to have a visible window) and the app has to have a valid signature (done automatically by py2app).

The reason scripts that are run in a terminal window fail is because the Python Framework includes a Python.app. The bluetoothd daemon sees that the process running your script is the Python.app but the foreground app is the Terminal.app, so it considers your Python script as a background app and therefore background scanning rules apply.

The reason Python includes a Python.app is so that it can launch programs that use graphical user interfaces. So you might think if you just open a window (i.e. import turtle; turtle.showturtle()), then your app should be the foreground app and all is good. However, this is where the second rule comes in. Bluetoothd sees that your script isn’t part of the Python.app bundle and therefore considers it not properly signed and so you don’t get foreground app treatment.

Finally, the NSBluetoothAlwaysUsageDescription key in info.plist is unrelated to the foreground/background scanning issue. If your app has already been given permission to use Bluetooth in System Prefereneces, then NSBluetoothAlwaysUsageDescription doesn’t have any effect. However, if your app has not been given permission, then having NSBluetoothAlwaysUsageDescription in your info.plist will prevent a crash from SIGABRT when your application is run.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Core Bluetooth | Apple Developer Forums
I want my app to be able to delete paired Bluetooth devices. Because if the device is paired with iPhone, it cannot be...
Read more >
UUID of connected bluetooth device on MacOS
I have the MAC addresses of all connected devices (obtained running: system_profiler SPBluetoothDataType ) but not the UUIDs.
Read more >
2010-February.txt
Therefore, if you installed intltool before 2009-08-23, it will not show p5-pathtools as a dependent. If you reinstall intltool today (sudo port -n...
Read more >
All NetBSD Packages - Index of /
The following list contains all 18937 packages currently available in the NetBSD Packages Collection, sorted alphabetically. Please select an entry for more ...
Read more >
Open Source License - Workbench 3.6 GA
The VMware Product that includes this file does not necessarily use all the open source ... desktop-data-sle-12-5.3.1 >>> desktop-file-utils-0.22-8.2 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found