How to enable python bluetooth sockets?

One of our products uses a combined Wifi/BT card to communicate with other devices. Therefore we are using bluez5 and we already evaluated that bluetooth itself is working fine. Now i want to send HCI commands within a python script. Starting with pyhton 3.3 the socket module can be used to communicate with bluetooth. However i am getting the error shown below… After some time i was able to track down the problem. During the bitbake process of the python3 module, the configure script searches for “bluetooth.h” which cannot be found at this time. Because of that the bluetooth socket feature is not compiled into python3. In fact, the mentioned header file is provided by “bluez5-dev”. When i try to add bluez5 as DEPENDS in the python3.5.3.bb recipe i am getting the error that “nativesdk-bluez5” cannot be build. So, the question is, how can i enable the bluetooth socket for python? Thanks in advance.

Python 3.5.3 (default, May 13 2020, 16:41:24) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> socket = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_RAW, socket.BTPROTO_HCI)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'socket' has no attribute 'AF_BLUETOOTH'

Hi @qojote

Thanks for writing to the Toradex Community!

Did you add libbluetooth-dev to your custom image recipe or local.conf?
Could you share your changes in local.conf?

Best regards,
Jaski

@jaski.tx Where should i add libbluetooth-dev? There is no such recipe, e.g. trying to bitbake it fails with “Nothing PROVIDES libbluetooth-dev”.

I have tried to force compiling the bluetooth socket by appending to “python3_3.5.5bb” and manipulate the EXTRA_OECONF parameter for ac_cv_header_bluetooth_bluetooth_h. This results in a broken socket module so that i get the error “No such module _socket” when importing socket…

Hi @jaski.tx
I have made only a few changes to my local.conf. Adding PACKAGES =+ " libbluetooth-dev " results in the error shown below.

Executing RunQueue Tasks
ERROR: cryptodev-linux-1.9-r0 do_packagedata: The recipe cryptodev-linux is trying to install files into a shared area when those files already exist. Those files and their manifest location are:
  /home/user/oe-core/build/tmp-glibc/pkgdata/apalis-tk1-mainline/runtime/libbluetooth-dev
    (matched in manifest-apalis-tk1-mainline-linux-libc-headers.packagedata)
Please verify which recipe should provide the above files.

The build has stopped, as continuing in this scenario WILL break things - if not now, possibly in the future (we've seen builds fail several months later). If the system knew how to recover from this automatically it would, however there are several different scenarios which can result in this and we don't know which one this is. It may be you have switched providers of something like virtual/kernel (e.g. from linux-yocto to linux-yocto-dev), in that case you need to execute the clean task for both recipes and it will resolve this error. It may be you changed DISTRO_FEATURES from systemd to udev or vice versa. Cleaning those recipes should again resolve this error, however switching DISTRO_FEATURES on an existing build directory is not supported - you should really clean out tmp and rebuild (reusing sstate should be safe). It could be the overlapping files detected are harmless in which case adding them to SSTATE_DUPWHITELIST may be the correct solution. It could also be your build is including two different conflicting versions of things (e.g. bluez 4 and bluez 5 and the correct solution for that would be to resolve the conflict. If in doubt, please ask on the mailing list, sharing the error and filelist above.
ERROR: cryptodev-linux-1.9-r0 do_packagedata: If the above message is too much, the simpler version is you're advised to wipe out tmp and rebuild (reusing sstate is fine). That will likely fix things in most (but not all) cases.
ERROR: cryptodev-linux-1.9-r0 do_packagedata: Function failed: sstate_task_postfunc
ERROR: Logfile of failure stored in: /home/user/oe-core/build/tmp-glibc/work/armv7at2hf-neon-angstrom-linux-gnueabi/cryptodev-linux/1.9-r0/temp/log.do_packagedata.20305
ERROR: Task (/home/user/oe-core/build/../layers/openembedded-core/meta/recipes-kernel/cryptodev/cryptodev-linux_1.9.bb:do_packagedata) failed with exit code '1'

Hi

It looks like the python and python3 recipe do not want to build with bluetooth socket support.
So in order to overcome this you would have to remove those configure statements for bluetooth.h. Additionally python needs to DEPEND on bluez, so that the header file is actually found by the configure script.

I would:

  • Revert whatever you did, e.g. the PACKAGES =+ " libbluetooth-dev " in local.conf and follow the advise in the error message to wipe the tmp dir.
    In our 2.8 BSP setup that would be rm -rf tmp-glibc ../deploy
  • Add a bbappend which adds bluez to DEPENDS and modifies the EXTRA_OECONF variable.

E.g. in your layer, add the file recipes-devtools/python/python3_3.5.3.bbappend with the following content:

DEPENDS_append = "${@bb.utils.contains('DISTRO_FEATURES','bluetooth bluez5','bluez5','bluez4',d)}"
EXTRA_OECONF_remove = "ac_cv_header_bluetooth_bluetooth_h=no ac_cv_header_bluetooth_h=no"

Note that for me this builds, i.e. bitbake python3 does not fail.

Max

Hi @max.tx ,
Thanks four your help. The suggested changes work like charm.

You are welcome. Thanks for your feedback.

Hi @max.tx ,

EDIT:
My append is slightly different :

DEPENDS_append = ’ bluez5’
EXTRA_OECONF_append = " ac_cv_header_bluetooth_bluetooth_h=yes"

This solution is not working anymore with yocto gatesgarth, issuing circular dependency error.
Output attached:
yocto_gatesgarth_loop_failure (76.6 KB)

Any idea how to fix this up?

Best Regards

On a second try, removing the line:
DEPENDS_append = ’ bluez5’

And keeping:
EXTRA_OECONF_append = " ac_cv_header_bluetooth_bluetooth_h=yes"

Leads to the error:
| ERROR: Execution of ‘/home/ffer/build-imx/build/tmp/work/cortexa53-crypto-poky-linux/python3/3.8.5-r0/temp/run.do_install.4820’ failed with exit code 1:
| Failed to build these modules:
| _socket _ssl

Hi @Fabiano.Ferronato

Could you start a new thread and provide all the Information to reproduce the issue?

Thanks and Best regards,
Jaski

Hi, just did it here:

Best Regards
Fabiano

Perfect Thanks.

Hi,

I have run into the same problem but with BSP 5.5 and the Verdin IMX8MM. I added the same bbappend (but for python 3.8.11) and edited the layer.conf to include bbappend files. This fix no longer seems to work as running bitbake tdx-reference-minimal-image produces the following output and error:

Loading cache: 100% |#############################################################################################################################| Time: 0:00:00
Loaded 3910 entries from dependency cache.
Parsing recipes: 100% |###########################################################################################################################| Time: 0:00:00
Parsing of 2747 .bb files complete (2746 cached, 1 parsed). 3910 targets, 240 skipped, 0 masked, 0 errors.
NOTE: Resolving any missing task queue dependencies
ERROR: Nothing PROVIDES 'python3-nativebluez5' (but /home/yocto/oe-core/build/../layers/openembedded-core/meta/recipes-devtools/python/python3_3.8.11.bb DEPENDS on or otherwise requires it). Close matches:
  python3-native
  python3-py-native
  python3-pybluez
NOTE: Runtime target 'python3' is unbuildable, removing...
Missing or unbuildable dependency chain was: ['python3', 'python3-nativebluez5']
ERROR: Required build target 'tdx-reference-minimal-image' has no buildable providers.
Missing or unbuildable dependency chain was: ['tdx-reference-minimal-image', 'python3', 'python3-nativebluez5']

Summary: There were 2 ERROR messages shown, returning a non-zero exit code.

The problem seems to be that it depends on python3-nativebluez5 instead of bluez5 but I was not able to figure out how to change that. I tried messing with the original oe-core/layers/openembedded-core/meta/recipes-devtools/python/python3_3.8.11.bb but always seems to append or prepend something. Clearly I’m missing some experience with Yocto to fix this.

Thanks for reading!
Ernest

For others finding this via Google:

I was able to get a working Python with bluetooth sockets build for now by adding tools-sdk to local.conf and adding zlib to the image. I then manually extracted libbluetooth-dev.deb and added the bluetooth header files to /usr/include/bluetooth/. This allows you to build python from source on the module itself with the correct bluetooth headers present.

2 Likes