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.