Bluetooth raspberry pi 3 fails after some time and does not recover

Since when I’m doing a hciconfig -a every minute, it did not fail, in 11 hours straight.
Weird. Too early to correlate though.

Ok, time for an update, both methods detected the failure.
The error occurred at 6:49, detected by hciconfig -a | grep Manufacturer at 6:51 and by the watchdog at 6:55.

This was the good news.

The bad news is that my script did not recover fully the Bluetooth correctly.
As a result, the graph of my temperature is better, as the sensor is unavailable but as it did not recover, it is missing for a long time.

So I’ll work on the script part and find a solution!

EDIT: I’ve a new script, I’ll put it to the test now.

Ok, I am at the point of giving up. I cannot use the hciconfig (as it is not installed on RPi 3).
I see a failure (in the log) and something that looks like recovery and using bluetoothctl, it looks like BT is seeing a number of devices.
But using a BLE scanner (on my mobile), I see mostly the same devices, but no RPi 3 (even after bluetoothctl discoverable on) and RPi3 does not see my device.
As my mobile is not one of the devices as shown using bluetoothctl, the debug logs do not indicate if/how/why it is no longer tracked.

So I will try to use the bluetooth LE tracker and see what happens, but I doubt if that will make a difference

I have it on mine.
Just install the package bluez with apt or apt-get

It is seing my iPhone but I had to go in the Bluetooth settings to scan for new devices before it was in the list of the bluetoothctl scan on.but all this is lost after a while anyway. Even if I did a trust on my phone’s bt address.

To restart my bluetooth on RPI3b+ I set up a shell command.

shell_command:
  bluetoothctl_off: bluetoothctl power off
  bluetoothctl_on: bluetoothctl power on
  
alias: Bluetoothctl Power Restart
sequence:
  - service: shell_command.bluetoothctl_off
    data: {}
  - delay:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
  - service: shell_command.bluetoothctl_on
    data: {}
mode: single 

Then run it from this script

Interesting approach. what do you use as a trigger for the script?

@Spiro, I think I tried that but I always had my logs flooded with these kinds of lines:

Bluetooth management socket connection lost: [Errno 9] Bad file descriptor
10:22:50 AM – (WARNING) runner.py - message first occurred at January 5, 2023 at 9:32:20 PM and shows up 386 times

Bluetooth adapter hci0 [xx:yy:zz:aa:bb:cc] could not be reset due to timeout
10:22:50 AM – (WARNING) components/bluetooth/util.py - message first occurred at January 5, 2023 at 9:32:20 PM and shows up 386 times

hci0 (xx:yy:zz:aa:bb:cc): Error stopping scanner: [org.bluez.Error.InProgress] Operation already in progress
10:22:45 AM – (ERROR) bluetooth - message first occurred at January 5, 2023 at 9:30:17 PM and shows up 387 times

Right now, my script is the following:

shell_command:
  restart_bluetooth: >
    sudo /bin/hciconfig hci0 down
    sudo systemctl stop bluetooth
    sudo systemctl stop hciuart
    sudo /sbin/rmmod btusb
    sudo /sbin/modprobe btusb
    sudo systemctl start hciuart 2>/dev/null
    sudo systemctl start bluetooth
    sleep 5
    sudo /bin/hciconfig hci0 up

I did it manually in a shell command prompt. It seems to work well. I hope it will be ok from HA, it should be. Fingers crossed.

I’ll try your bluetoothctl power off/on next time, I’m disabling my script for now and send myself a notification to try it.

As simple trigger for now, you can do a time pattern trigger, like every hour.

trigger:
  - platform: time_pattern
    hours: /1

There will always be time later to refine the trigger

To be honest my bluetooth rarely goes down. If it were a problem I’d add it to a script to be run by the watch man automation above. Occasionally I run it . Usually when I have been messing with bluetooth devices.

Ok, I will stop for now. Thanks for your help and suggestions.

I used an old mobile and installed a bluetooth scanner. I can’t find my current mobile with this (and vice versa, as they are both motorola’s).
My mobile is picked up by other equipment (like headphones and Hue lights), but for some reason it is not visible in the scanner. Even with discovery active. Might be the same reason why RPi3 does not show it with a ‘bluetoothctl’ command.
My insight in the functioning of HA BT has increased, but I am lost in Motorola’s BT implementation.

I can’t manage what I can’t see.

Sorry to hear that you give up (at least for now).
I have to admit that the BT stack of the RPi 3 is crap.
It doesn’t detect my amazon echo dot
Or, sometimes, it is after a looooooong time (ran systemctl scan on for 2 days for it to show up)

I suspect people have too much metal in their cases for cooling which may affect BT range.

@Spiro: range was not the issue, I held the scanner next to my mobile. I suspect the BT implementation by Motorola has some items why the scanner does not pick it up as a BT, while other BT equipment can (still) pair with it.
But that is definitely beyond my knowledge and capabiltiy to figure out.

@Olivier1974: It is not the end, I will definitely try again when I have replaced my mobile (which might take a year or 2). I found out that HA (and smart home in general) is addictive. About 1,5 year ago, I had just 1 light to figure out HA for fun and wondered what else to do with it (and why). When I installed shutters, I also installed shelly switches for them and that brought on the fever. It is still no necessity, but it fun to play with.

Thanks again guys!

Apologies if this has been mentioned, is this the built-in BT on the RPi?

I wonder if this exhibits the same problem with a USB BT dongle.

For me, yes, it is the built in one.
I was using an ESPHome ESP32 explicitly as BT relay as HA was not BT friendly when I started.
Now that HA is BT friendly, I moved to the internal BT. But this is not reliable.

I believe it is the same internal BT of the Pi for @spijkers09, to confirm.

Nevertheless, the packages used internaly or by external dongles are the same: bluetooth pi-bluetooth and bluez (optional blueman if you’re using the desktop environment, which I dont)

internal BT for me as wel (RPi3)

Hello! I have a similar issue. I have updated OS from 7.6 to 9.4, and core to 2023.1.6 (don’t remember what version it was before an update). And my bluetooth integration has stopped working.
image
When I enable passive scanning then the error disappears, but bluetooth still doesn’t work.
bluetoothctl show returns power: no, any command like power on, scan on returns org.Bluez.Error.NotReady/Busy. I don’t know what’s going on, can’t find any idea to fix it. I also have RPi b+ with built-in bluetooth.

Same issue here, I have updated my Pi3 for Pi4 and it does not see my iPhone as well while my old setting on Rpi3 is still accurate. Did setup a ESP32 for the BT detection however get a lot of devices in HA as Known Devices.
Would like to know what the best solution would be or the best script in ESP32, still looking for it

And same issue here too. It satarted after updated core to version 2023.2.2. I use RPi 4 with built-in-bluetooth.