Closed jeremyherbert closed 1 year ago
If you use BleakScanner.find_by_name()
instead of BleakScanner.discover()
it will return as soon as a device is found and have a better chance of connecting before the device disappears.
I have tried that with no luck unfortunately. I have also tried Ubuntu 22.04 with bluez 5.64 and I get the same result. Interestingly, I have tried adding TemporaryTimeout=30
to the bluetooth configuration and it doesn't keep the devices alive...
I worked out what this was after digging through the source code of bluez. The device is deleted immediately if it does not advertise the connectable flag in the advertising data (and indeed this device was not setting it).
Glad you figured it out and thanks for following up.
In case anyone finds this later, the problem is fixed in bluez 5.65 and later.
@jeremyherbert Would you mind pointing me to a process to upgrade to bluez 5.65 (or 5.66) from 5.55? I am using bullseye Raspbian and 5.66 from debian bookworm requires a newer libc6 library and I do not want to upgrade that 'unofficially', as I am afraid it could break my system or generate all sorts of other dependency issues.
I tried to download the bluez source code (5.55-3.1+rpt2) and apply your patch (not the one from 5.66, but your original one). However, I am dispairing trying to compile it then with dpkg-buildpackage. Do you see an easier way? Or did you upgrade the whole OS when going from 5.55 to 5.65?
Ok. I am one step further. I could compile and install, but I still get the same error, as before, that the device is not found. I will dig into the patch a bit more, tomorrow. It could not be applied 100%. A pointer for others: I needed to install the following to make bluez compile
sudo apt install libglib2.0-dev libdbus-1-dev elfutils libudev-dev libical-dev libreadline-dev libdw-dev libjson-c-dev libasound2-dev
libell-dev
There were some differences in the patch...will get back to it tomorrow, but probably getting there...
Sorry, I just did it on Ubuntu 20.04 / 22.04 and the version from the git repo worked fine as-is. I have not tried on rpi.
In terms of my own patch, I did the following on Ubuntu (I guess it should be the same on debian?)
sudo apt install libbluetooth-dev
sudo apt build-dep bluez
sudo apt build-dep linux linux-image-$(uname -r)
sudo apt source bluez
<enter bluez dir and edit bluez source code>
dpkg-buildpackage -rfakeroot -uc -b
sudo dpkg -i bluez_<the version you built>.deb
Just an update in case someone else gets here to find a solution. I was not successful in adapting the code from 5.55. I tried to implement the official patch from 5.64 to 5.65 (https://git.kernel.org/pub/scm/bluetooth/bluez.git/commit/?id=3fb4e1cca2eaddc413d1ebed9d862084351efdbc) I could only do this only partially, as there is almost 2 years between 5.55 and 5.65 and the changes have been substantial. It did not change anything after I installed it to my system. I ended up using pygatt, instead, which also has issues (gatttool is deprecated), but for the time being, I need to rely on it, until a bullseye testing package with 5.65 or higher comes out. Thanks for everyone here for the support!
I was able to upgrade from bluez 5.55 to 5.66 on Raspberry Pi OS Bullseye by following the guide at https://gist.github.com/israveri/2d3fef451b2e0ce89f7290eda6366b14 and just changing the version of bluez that I downloaded
I was able to upgrade from bluez 5.55 to 5.66 on Raspberry Pi OS Bullseye by following the guide at https://gist.github.com/israveri/2d3fef451b2e0ce89f7290eda6366b14 and just changing the version of bluez that I downloaded
Since that gist is no longer available, here are the steps to upgrade from Bluez 5.55 to 5.66 on Raspberry Pi OS Bullseye. This might be useful for anyone else who stumbles across it while looking for an answer, like I did. This is based on a version of the gist from Wayback Machine and adds a missing dependency.
# Install dependancies
sudo apt update -y
sudo apt install libdbus-1-dev libglib2.0-dev libudev-dev libical-dev libreadline-dev -y
sudo apt install -y python3-docutils
# Download and extract Bluez
wget www.kernel.org/pub/linux/bluetooth/bluez-5.66.tar.xz
tar xvf bluez-5.66.tar.xz && cd bluez-5.66
# Build and Install
./configure --prefix=/usr --mandir=/usr/share/man --sysconfdir=/etc --localstatedir=/var --enable-experimental
make -j4
sudo make install
# Reboot Raspberry Pi
sudo reboot
# Check current version
bluetooth-ctl -v
I built and installed a newer bluez (version 5.66) on my Bullseye Debian system. Still receiving the same error.
This issue has been closed for a long time. So if anyone is getting the same error and would like help, I would suggest starting a new discussion thread and include as much logs as possible (see troubleshooting page in docs).
bluetoothctl -v
) in case of Linux: 5.53 (also tested on 5.55 with no change)Description
I am trying to connect to a xiaomi mijia device running this custom firmware: https://github.com/pvvx/ATC_MiThermometer - I have multiple devices and am able to connect to all devices fine with
bluetoothctl
, but in bleak only some devices can connect. For the non-working devices I otherwise get the error:I have changed the bluez autoconnect interval to 22 seconds. Although I am using a Nordic Semi USB-HCI BLE adapter (using Zephyr), I have tried this also on a raspberry pi 4 using the native BLE device and it has the same behaviour.
What I Did
Connecting to the device using
bluetoothctl
works fine usingconnect A4:C1:38:C5:62:B4
.In bleak, I am just trying to connect and enumerate the services on the device. I am using this script:
This is the output from the script (the
client.connect
line fails immediately, not after 30 seconds):Logs
I have
bluetoothctl
running in the background so I can see the scan results. While bleak is scanning, it shows what you would expect (I have truncated the results):However, as soon as the scan stops, it prints the following:
in the
bluetoothd
log, I see the following when the scan is stopped:In
btmon
this is what shows up when the scan is occurring:There isn't anything interesting when the scan terminates however (other than the fact that the connection is never attempted):