Mausy5043 / bluepy3

A Python3 interface to Bluetooth LE on Linux.
https://pypi.org/project/bluepy3
GNU Affero General Public License v3.0
17 stars 1 forks source link

[LibreELEC Linux 6.9.5, bluez 5.76] Failed to connect to peripheral #35

Closed emanuel4you closed 2 months ago

emanuel4you commented 3 months ago

Hello, maybe someone can help me or know more about the topic I've build bluepy3 modules in LibreElec toolcain. First I've used bluez-5.72 src, for building bluepy3, later 5.70, 5.66 - with same results :-(

I have got this issues on amlogic s922x, raspberry pi5 (current LibreElec). I have got this issues on amlogic s922x - pip3 install (current armbian Ubuntu builds).

On Raspbian debian (bluez-5.66, kernel 6.6.y) it works normal.

what can I do?

see log:

LibreELEC:~/.kodi/addons/script.module.bluepy3/lib/bluepy3 # uname -a
Linux LibreELEC 6.9.5 #1 SMP PREEMPT Sun Jun 23 21:17:04 UTC 2024 aarch64 GNU/Linux
LibreELEC:~/.kodi/addons/script.module.bluepy3/lib/bluepy3 # ls /lib | grep blue
bluetooth
bluez
libbluetooth.so
libbluetooth.so.3
libbluetooth.so.3.19.14
LibreELEC:~/.kodi/addons/script.module.bluepy3/lib/bluepy3 # python btle.py 00:09:34:b2:ca:2b public
00002a00-0000-1000-8000-00805f9b34fb
Connecting to: 00:09:34:b2:ca:2b, address type: public
    -btle- Running /storage/.kodi/addons/script.module.bluepy3/lib/bluepy3/bluepy3-helper
    -btle- Sent:   conn 00:09:34:b2:ca:2b public
    -btle- Got:    # bluepy3-helper.c version undefined built at 16:25:38 on Aug  5 2024
    -btle- Got:    rsp=$stat; state=$tryconn; dst=00:09:34:b2:ca:2b; mtu=h0; sec=low
    -btle- Got:    rsp=$stat; state=$disc; mtu=h0; sec=low
    -btle- Stopping /storage/.kodi/addons/script.module.bluepy3/lib/bluepy3/bluepy3-helper
*** -btle-  Failed to connect. (5)

    -btle- Running /storage/.kodi/addons/script.module.bluepy3/lib/bluepy3/bluepy3-helper
    -btle- Sent:   conn 00:09:34:b2:ca:2b public
    -btle- Got:    # bluepy3-helper.c version undefined built at 16:25:38 on Aug  5 2024
    -btle- Got:    rsp=$stat; state=$tryconn; dst=00:09:34:b2:ca:2b; mtu=h0; sec=low
    -btle- Got:    rsp=$stat; state=$disc; mtu=h0; sec=low
    -btle- Stopping /storage/.kodi/addons/script.module.bluepy3/lib/bluepy3/bluepy3-helper
*** -btle-  Failed to connect. (4)

    -btle- Running /storage/.kodi/addons/script.module.bluepy3/lib/bluepy3/bluepy3-helper
    -btle- Sent:   conn 00:09:34:b2:ca:2b public
    -btle- Got:    # bluepy3-helper.c version undefined built at 16:25:38 on Aug  5 2024
    -btle- Got:    rsp=$stat; state=$tryconn; dst=00:09:34:b2:ca:2b; mtu=h0; sec=low
    -btle- Got:    rsp=$stat; state=$disc; mtu=h0; sec=low
    -btle- Stopping /storage/.kodi/addons/script.module.bluepy3/lib/bluepy3/bluepy3-helper
*** -btle-  Failed to connect. (3)

    -btle- Running /storage/.kodi/addons/script.module.bluepy3/lib/bluepy3/bluepy3-helper
    -btle- Sent:   conn 00:09:34:b2:ca:2b public
    -btle- Got:    # bluepy3-helper.c version undefined built at 16:25:38 on Aug  5 2024
    -btle- Got:    rsp=$stat; state=$tryconn; dst=00:09:34:b2:ca:2b; mtu=h0; sec=low
    -btle- Got:    rsp=$stat; state=$disc; mtu=h0; sec=low
    -btle- Stopping /storage/.kodi/addons/script.module.bluepy3/lib/bluepy3/bluepy3-helper
*** -btle-  Failed to connect. (2)

    -btle- Running /storage/.kodi/addons/script.module.bluepy3/lib/bluepy3/bluepy3-helper
    -btle- Sent:   conn 00:09:34:b2:ca:2b public
    -btle- Got:    # bluepy3-helper.c version undefined built at 16:25:38 on Aug  5 2024
    -btle- Got:    rsp=$stat; state=$tryconn; dst=00:09:34:b2:ca:2b; mtu=h0; sec=low
    -btle- Got:    rsp=$stat; state=$disc; mtu=h0; sec=low
    -btle- Stopping /storage/.kodi/addons/script.module.bluepy3/lib/bluepy3/bluepy3-helper
*** -btle-  Failed to connect. (1)

Traceback (most recent call last):
  File "/storage/.kodi/addons/script.module.bluepy3/lib/bluepy3/btle.py", line 1076, in <module>
    conn = Peripheral(my_device_address)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/storage/.kodi/addons/script.module.bluepy3/lib/bluepy3/btle.py", line 625, in __init__
    self._connect(addr, addrType, iface, timeout)
  File "/storage/.kodi/addons/script.module.bluepy3/lib/bluepy3/btle.py", line 710, in _connect
    raise BTLEConnectError(
BTLEConnectError: (btle) Failed to connect to peripheral 00:09:34:b2:ca:2b, addr type: public, interface None, timeout=32.1
LibreELEC:~/.kodi/addons/script.module.bluepy3/lib/bluepy3 # 
LibreELEC:~/.kodi/addons/script.module.bluepy3/lib/bluepy3 # 
LibreELEC:~/.kodi/addons/script.module.bluepy3/lib/bluepy3 # 
LibreELEC:~/.kodi/addons/script.module.bluepy3/lib/bluepy3 # hcitool lescan
LE Scan ...
00:09:34:B2:CA:2B DM Remote
00:09:34:B2:CA:2B DM Remote Advanced
00:09:34:B2:CA:2B DM Remote
00:09:34:B2:CA:2B DM Remote Advanced
^CLibreELEC:~/.kodi/addons/script.module.bluepy3/lib/bluepy3 # bluetoothctl
[bluetooth]# scan le
[bluetooth]# SetDiscoveryFilter success
[bluetooth]# hci0 type 6 discovering on
[bluetooth]# Discovery started
[bluetooth]# [CHG] Controller 43:54:A2:00:1F:AC Discovering: yes
[bluetooth]# [NEW] Device 00:09:34:B2:CA:2B DM Remote Advanced
[bluetooth]# [CHG] Device 00:09:34:B2:CA:2B RSSI: 0xffffffb4 (-76)
[bluetooth]# [CHG] Device 00:09:34:B2:CA:2B RSSI: 0xffffffb1 (-79)
[bluetooth]# [CHG] Device 00:09:34:B2:CA:2B RSSI: 0xffffffaa (-86)
[bluetooth]# [CHG] Device 00:09:34:B2:CA:2B RSSI: 0xffffffac (-84)
[bluetooth]# [CHG] Device 00:09:34:B2:CA:2B RSSI: 0xffffffb4 (-76)
[bluetooth]# [CHG] Device 00:09:34:B2:CA:2B RSSI: 0xffffffaf (-81)
[bluetooth]# [CHG] Device 00:09:34:B2:CA:2B ManufacturerData.Key: 0x06c4 (1732)
[bluetooth]# [CHG] Device 00:09:34:B2:CA:2B ManufacturerData.Value:
[bluetooth]#   00 00 00 00 00 00                                ......          
[CHG] Device 00:09:34:B2:CA:2B RSSI: 0xffffffb0 (-80)
[CHG] Device 00:09:34:B2:CA:2B RSSI: 0xffffffae (-82)
[bluetooth]# scan off
[bluetooth]# hci0 type 6 discovering off
[bluetooth]# Discovery stopped
[bluetooth]# [CHG] Device 00:09:34:B2:CA:2B RSSI is nil
[bluet00:09:34:B2:CA:2B9:34:B2:CA:2B
[bluetooth]# [CHG] Device 00:09:34:B2:CA:2B Trusted: yes
[bluetooth]# Changing 00:09:34:B2:CA:2B trust succeeded
[blue00:09:34:B2:CA:2B9:34:B2:CA:2B
Attempting to pair with 00:09:34:B2:CA:2B
[bluetooth]# hci0 00:09:34:B2:CA:2B type LE Public connected eir_len 30
[CHG] Device 00:09:34:B2:CA:2B Connected: yes
[DM Remote Advanced]# hci0 00:09:34:B2:CA:2B type LE Public disconnected with reason 1
[DM Remote Advanced]# Failed to pair: org.bluez.Error.AuthenticationTimeout
[CHG] Device 00:09:34:B2:CA:2B Connected: no
[bluetooth]# 

LibreELEC:~/.kodi/addons/script.module.bluepy3/lib/bluepy3 # dmesg | grep Bl
[    0.300044] Block layer SCSI generic (bsg) driver version 0.4 loaded (major 244)
[    8.249019] Bluetooth: Core ver 2.22
[    8.249173] Bluetooth: HCI device and connection manager initialized
[    8.249476] Bluetooth: HCI socket layer initialized
[    8.249494] Bluetooth: L2CAP socket layer initialized
[    8.249845] Bluetooth: SCO socket layer initialized
[    8.487391] Bluetooth: HCI UART driver ver 2.3
[    8.487417] Bluetooth: HCI UART protocol H4 registered
[    8.487540] Bluetooth: HCI UART protocol Three-wire (H5) registered
[    8.487791] Bluetooth: HCI UART protocol Broadcom registered
[    8.487840] Bluetooth: HCI UART protocol QCA registered
[    8.886316] Bluetooth: hci0: BCM: chip id 101
[    8.887349] Bluetooth: hci0: BCM: features 0x2f
[    8.888876] Bluetooth: hci0: BCM4354A2
[    8.888889] Bluetooth: hci0: BCM4356A2 (001.003.015) build 0000
[    8.937416] Bluetooth: hci0: BCM4356A2 'brcm/BCM4356A2.hcd' Patch
[    9.838655] Bluetooth: hci0: BCM: features 0x2f
[    9.840383] Bluetooth: hci0: BCM4356 37.4MHz AMPAK AP6356-0055
[    9.840407] Bluetooth: hci0: BCM4356A2 (001.003.015) build 0266
[    9.861862] Bluetooth: MGMT ver 1.22
Mausy5043 commented 3 months ago

I'm not familiar with the AMLOGIC s922x, nor with LibreElec or its toolchain. So can't help you with that. Nevertheless, it is remarkable that the bluepy3-helper was built without version information.

Could you please post the output from this command: dt=$(date '+%Y-%m-%d %H:%M'); helpermaker --list; journalctl --since "${dt}" NOTE: It will also build the bluepy3-helper as designed.

Are you trying this on different hardware platforms or on different OS's? Please post details on each combination of hardware (make, model and CPU) and OS and Python version.

Did bluepy3 come with LibreELEC pre-installed?
And could you confirm that your BT-modem supports BLE?

emanuel4you commented 3 months ago

Are you trying this on different hardware platforms or on different OS's?

yes, raspberrypi5 and dreambox two. In armbian (on s922X) I've found the issue is Python 3.11 (import error Self) but armbian is not importen for me. I need the BLE Remote in LibreElec.

Did bluepy3 come with LibreELEC pre-installed?

no, I've made the package. GNU Make 4.3 (update added VERSION_STRING):

PKG_NAME="bluepy3"
PKG_VERSION="2.12.1"
PKG_ARCH="arm aarch64"
PKG_SHA256="f38de9f0f2dff0f367826edaaf85a09e152343ab80d08d6b215a5629ec1aa544"
PKG_LICENSE="MIT"
PKG_SITE="https://pypi.org/project/bluepy3"
PKG_URL="https://github.com/Mausy5043/bluepy3/archive/refs/tags/v${PKG_VERSION}.tar.gz"
PKG_DEPENDS_TARGET="toolchain Python3 distutilscross:host"
PKG_SHORTDESC="connect to ble devices."
PKG_LONGDESC="This is a Python3 library to allow communication with Bluetooth Low Energy devices on Linux."
PKG_TOOLCHAIN="manual"

pre_configure_target() {
  export PYTHONXCPREFIX="$SYSROOT_PREFIX/usr"
  export LDSHARED="$CC -shared"
  export CFLAGS="$CFLAGS -fcommon"
  export CPPFLAGS="$TARGET_CPPFLAGS -I${SYSROOT_PREFIX}/usr/include/$PKG_PYTHON_VERSION"
  sed -i 's!5.70!5.76!'g bluepy3/Makefile
  sed -i 's!/tmp/bluez!./bluez!'g bluepy3/Makefile
  BUILD_VERSION="${PKG_VERSION}-5.76"
  echo "#define VERSION_STRING \"${BUILD_VERSION}\"" > bluepy3/version.h
}

make_target() {
  cd bluepy3
  make
}

And could you confirm that your BT-modem supports BLE?

yes, both raspberrypi5 and dreambox two/one s922X do support BLE and do run bluepy3 under other os.

original raspberry pi5 OS:

python btle.py 00:09:34:b2:ca:2b public
00002a00-0000-1000-8000-00805f9b34fb
Connecting to: 00:09:34:b2:ca:2b, address type: public
    -btle- Running /usr/local/lib/python3.11/dist-packages/bluepy3/bluepy3-helper
    -btle- Sent:   conn 00:09:34:b2:ca:2b public
    -btle- Got:    # bluepy3-helper.c version 2.12.1-5.66 built at 16:42:22 on Aug  4 2024
    -btle- Got:    rsp=$stat; state=$tryconn; dst=00:09:34:b2:ca:2b; mtu=h0; sec=low
    -btle- Got:    rsp=$stat; state=$conn; dst=00:09:34:b2:ca:2b; mtu=h0; sec=low
    -btle- Succesfully connected.
    -btle- Sent:   disc
    -btle- Got:    rsp=$stat; state=$conn; dst=00:09:34:b2:ca:2b; mtu=hF7; sec=low
    -btle- Updated MTU: 247
    -btle- Stopping /usr/local/lib/python3.11/dist-packages/bluepy3/bluepy3-helper
emanuel@raspberrypi:/usr/local/lib/python3.11/dist-packages/bluepy3 $

Could you please post the output from this command: dt=$(date '+%Y-%m-%d %H:%M'); helpermaker --list; journalctl --since "${dt}"

No, because LibreELEC is ro OS, with out make. I've added version string in my build package script for the toolchain but as expected, it does not make a difference.

LibreELEC:~/.kodi/addons/script.module.bluepy3/lib/bluepy3 # python btle.py 00:09:34:b2:ca:2b public
00002a00-0000-1000-8000-00805f9b34fb
Connecting to: 00:09:34:b2:ca:2b, address type: public
    -btle- Running /storage/.kodi/addons/script.module.bluepy3/lib/bluepy3/bluepy3-helper
    -btle- Sent:   conn 00:09:34:b2:ca:2b public
    -btle- Got:    # bluepy3-helper.c version 2.12.1-5.76 built at 13:51:09 on Aug  7 2024
    -btle- Got:    rsp=$stat; state=$tryconn; dst=00:09:34:b2:ca:2b; mtu=h0; sec=low
    -btle- Got:    rsp=$stat; state=$disc; mtu=h0; sec=low
    -btle- Stopping /storage/.kodi/addons/script.module.bluepy3/lib/bluepy3/bluepy3-helper
*** -btle-  Failed to connect. (5)
...

*** -btle-  Failed to connect. (1)

Traceback (most recent call last):
  File "/storage/.kodi/addons/script.module.bluepy3/lib/bluepy3/btle.py", line 1076, in <module>
    conn = Peripheral(my_device_address)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/storage/.kodi/addons/script.module.bluepy3/lib/bluepy3/btle.py", line 625, in __init__
    self._connect(addr, addrType, iface, timeout)
  File "/storage/.kodi/addons/script.module.bluepy3/lib/bluepy3/btle.py", line 710, in _connect
    raise BTLEConnectError(
BTLEConnectError: (btle) Failed to connect to peripheral 00:09:34:b2:ca:2b, addr type: public, interface None, timeout=32.1
LibreELEC:~/.kodi/addons/script.module.bluepy3/lib/bluepy3 # 

installed files:

LibreELEC:~/.kodi/addons/script.module.bluepy3/lib/bluepy3 # ls
__init__.py       bluepy3-helper    btle.py           helpermaker.py
blescan.py        bluepy3-helper.c  get_services.py   uuids.json
LibreELEC:~/.kodi/addons/script.module.bluepy3/lib/bluepy3 # 

EDIT: fixed quoting for readability

Mausy5043 commented 3 months ago

Are you trying this on different hardware platforms or on different OS's?

yes, raspberrypi5 and dreambox two. In armbian (on s922X) I've found the issue is Python 3.11 (import error Self) but armbian is not importen for me. I need the BLE Remote in LibreElec.

Okay. Let's park the issue with the RPi5/Armbian then. Feel free to create a separate issue for that if you want, but let's focus on the Dreambox/LibreElec issue.

Did bluepy3 come with LibreELEC pre-installed?

no, I've made the package. [...]

Could you please post the output from this command:[...]

I need the version of bluetoothctl that's installed. It would have been in the output from the helpermaker.py --list command.

No, because LibreELEC is ro OS, with out make.

.... so you built a custom executable on a different OS, packaged it for LibreElec and that doesn't work? Why would you do that? Does LibreElec not have native bluetooth support?

I've added version string in my build package script for the toolchain but as expected, it does not make a difference.

Indeed, I do also not expect that the versionstring would make any difference. However, building the helper using the helpermaker.py script ensures that it is built with the correct tools and the correct version of the BlueZ sourcetree. And the versionstring is an indicator of that the right buildprocess was used. As you have built it differently there will be additional unknowns that could contribute to the problem.

emanuel4you commented 3 months ago

Okay. Let's park the issue with the RPi5/Armbian then. Feel free to create a separate issue for that if you want, but let's focus on the Dreambox/LibreElec issue.

Let's park Dreambox/Armbian issues. (I do not build armbian for RPI) and let's focus on LibreElec (Dreambox and RPI), because they got the same os, arch and that issue.

I need the version of bluetoothctl that's installed. It would have been in the output from the helpermaker.py --list command.

As you can see in the first post abouve the issue is the same using "bluetoothctl" in terminal. Connection cound not be established. And that is the main problem! Here the version:

LibreELEC (community): devel-20240623195447-d897522 (AMLGX.aarch64)
LibreELEC:~ # bluetoothctl -v
bluetoothctl: 5.76
LibreELEC:~ #

.... so you built a custom executable on a different OS, packaged it for LibreElec and that doesn't work? Why would you do that? Does LibreElec not have native bluetooth support?

yes, inside the LE toolchain (docker). I've also built the Python module "evdev" for my driver, and it works as expected. So I thing the "bluepy3" was also built well. I think the problem comes from further down. Either it's the bluez version or the kernel itself. LibreElec has got a bluetooth scanner, but it do not find the Dreambox BLE Remote.

Building "bluepy3" in LE toolchain, brings up only one error while building: in lines: https://github.com/Mausy5043/bluepy3/blob/devel/bluepy3/Makefile#L35 https://github.com/Mausy5043/bluepy3/blob/devel/bluepy3/Makefile#L36 there are two times "$(IMPORT_SRCS)" and the toolchain do exec this two times. So there is a error msg: "bluez src already exists" I can can patch this lines later - but that's not the problem, just unpleasant to build.

EDIT: fixed quoting for readability (again)

Mausy5043 commented 3 months ago

Building "bluepy3" in LE toolchain, brings up only one error while building: in lines: https://github.com/Mausy5043/bluepy3/blob/devel/bluepy3/Makefile#L35 https://github.com/Mausy5043/bluepy3/blob/devel/bluepy3/Makefile#L36 there are two times "$(IMPORT_SRCS)" and the toolchain do exec this two times. So there is a error msg: "bluez src already exists" I can can patch this lines later - but that's not the problem, just unpleasant to build.

There is nothing wrong with the Makefile. Line 35 has the target name and lists the prerequisites. Line 36 is the command to be executed. Your toolchain seems to be defective.

Ref.: https://www.gnu.org/software/make/manual/html_node/Rule-Introduction.html

Mausy5043 commented 3 months ago

yes, inside the LE toolchain (docker). I've also built the Python module "evdev" for my driver, and it works as expected. So I thing the "bluepy3" was also built well. I think the problem comes from further down. Either it's the bluez version or the kernel itself. LibreElec has got a bluetooth scanner, but it do not find the Dreambox BLE Remote.

If built against the BlueZ sourcetree for v5.76 it should work. This indeed doesn't seem to be caused by bluepy3.

Mausy5043 commented 3 months ago

Tip: you can make DEBUGGING=1 ... to enable the display of debugging info from the helper and the BT stack. Maybe that gives some info about what is going on.

Mausy5043 commented 2 months ago

Closing this issue assuming this is resolved with the latest release. Please feel free to re-open if you wish to add additional info.